package com.avedia.ring.dao; /** * 響鈴詳細的一個工廠 * @author zyh */ import com.avedia.common.factory.AbstractFactory; public class MinuteRingFactory implements AbstractFactory<MinuteRingIf>{ private static String key="key"; public MinuteRingFactory(){ } private static MinuteRingFactory instance; public MinuteRingIf getInstance() { MinuteRingIf minuteRingInstance = new MinuteRingImpl(); return minuteRingInstance; } public static MinuteRingFactory getMinuteRingFactory(){ if(instance==null){ synchronized (key){ if (instance == null) { instance = new MinuteRingFactory(); } } } return instance; } } package com.avedia.ring.dao; /** * 響鈴詳細表接口中定義的一些方法 * @author zyh */ import java.util.List; import org.json.JSONArray; import com.avedia.ring.orm.ShrgSchScheduleInfoT; import com.avedia.ring.orm.ShrgSchScheduleManageT; public interface MinuteRingIf { public List<ShrgSchScheduleInfoT> getMinuteRing(long id); public boolean delMinuteRing(long id); public boolean delAll(ShrgSchScheduleInfoT ss); public boolean save(ShrgSchScheduleInfoT sh);//保存一個新的響鈴列表 public ShrgSchScheduleInfoT sel(long id);//根據LONGid找一個單一的對象 public boolean update(ShrgSchScheduleInfoT scheduleInfoT); public boolean delConnectTb(Long id); public JSONArray getJson(long id);//將我們的對象轉換成JSON格式 } package com.avedia.ring.dao; /** * 響鈴詳細接口的實現 * @author zyh */ import java.util.List; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.Transaction; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import org.junit.Test; import com.avedia.common.hibernate.HibernateEntityDAO; import com.avedia.ring.orm.ShrgSchScheduleInfoT; import com.avedia.ring.orm.ShrgSchScheduleManageT; public class MinuteRingImpl extends HibernateEntityDAO<ShrgSchScheduleInfoT> implements MinuteRingIf{ Session session = null; Transaction transaction = null; public List<ShrgSchScheduleInfoT> getMinuteRing(long id) { String hql = "from ShrgSchScheduleInfoT where scheduleManageId = ?"; session = getSession(); Query query = session.createQuery(hql); query.setParameter(0, id); List<ShrgSchScheduleInfoT> list = query.list(); return list; } /** * 刪除與id相等的對象 */ public boolean delMinuteRing(long id) { try{ super.delete(id); }catch(Exception ex){ return false; } return true; } /** * 刪除一個對象 */ public boolean delAll(ShrgSchScheduleInfoT scheduleInfoT) { try{ session = getSession(); transaction=session.beginTransaction(); transaction.begin(); session.delete(scheduleInfoT); transaction.commit(); }catch(Exception ex){ transaction.rollback(); return false; } return true; } /** * 保存一個對象 */ public boolean save(ShrgSchScheduleInfoT sh) { try{ super.save(sh); }catch(Exception ex){ return false; } return true; } /** * 根據ID查找一個對象 */ public ShrgSchScheduleInfoT sel(long id) { String hql = "FROM ShrgSchScheduleInfoT where id=?"; session = getSession(); Query query = session.createQuery(hql); query.setLong(0, id); return (ShrgSchScheduleInfoT) query.list().get(0); } /** * 修改對象 */ public boolean update(ShrgSchScheduleInfoT scheduleInfoT) { try{ session = getSession(); transaction=session.beginTransaction(); transaction.begin(); session.update(scheduleInfoT); transaction.commit(); }catch(Exception ex){ transaction.rollback(); return false; } return true; } /** * 主表刪除的時候會要條用這個方法 */ public boolean delConnectTb(Long id) { session = getSession(); transaction = session.beginTransaction(); String hql = "delete ShrgSchScheduleInfoT where scheduleManageId=?"; Query query = session.createQuery(hql); query.setLong(0, id); try{ query.executeUpdate(); transaction.commit(); }catch(Exception ex){ transaction.rollback(); return false; } return true; } /** * 將我們的對象轉成JSON對象 */ public JSONArray getJson(long id) { JSONArray jsons = new JSONArray(); JSONObject jo = null; List<ShrgSchScheduleInfoT> list = MinuteRingFactory.getMinuteRingFactory().getInstance().getMinuteRing(id); for (ShrgSchScheduleInfoT shrgSchScheduleInfoT : list) { jo = new JSONObject(); try { jo.put("name", shrgSchScheduleInfoT.getName()); jo.put("classStartTime", shrgSchScheduleInfoT.getClassStartTime()); jo.put("classOverTime", shrgSchScheduleInfoT.getClassOverTime()); jo.put("classRing", shrgSchScheduleInfoT.getClassRing()); jsons.put(jo); } catch (JSONException e) { e.printStackTrace(); } } return jsons; } @Test public void getJson(){ JSONArray jsons = new JSONArray(); JSONObject jo = null; List<ShrgSchScheduleInfoT> list = MinuteRingFactory.getMinuteRingFactory().getInstance().getMinuteRing(2); for (ShrgSchScheduleInfoT shrgSchScheduleInfoT : list) { jo = new JSONObject(); try { jo.put("name", shrgSchScheduleInfoT.getName()); jo.put("classStartTime", shrgSchScheduleInfoT.getClassStartTime()); jo.put("classOverTime", shrgSchScheduleInfoT.getClassOverTime()); jo.put("classRing", shrgSchScheduleInfoT.getClassRing()); jsons.put(jo); } catch (JSONException e) { e.printStackTrace(); } } System.out.println(jsons); } }
package com.avedia.ring.struts.form;
import java.sql.Timestamp; import java.util.ArrayList; import java.util.List;
import org.apache.struts.action.ActionForm; import org.apache.struts.upload.FormFile;
public class ShrgSchScheduleInfoTForm extends ActionForm { private static final long serialVersionUID = 1L; private Long id; private String name; private Long scheduleManageId; private String classStartTime; private String classOverTime; private String classRing; private Integer createdBy; private Timestamp creationDate; private Integer lastUpdateBy; private Timestamp lastUpdateDate; /** * struts1文件上傳用的是調用的是專門上傳的接口FormFile, * 單文件上傳只需在Form中給一個FormFile類型的屬性, * 多文件上傳時,struts在頁面端將多個文件封裝成一個數組,轉給Form類時轉換成List * 所以我們在頁面上使用數組形式對文件進行提交,在Form類中用List對文件進行接收。 * * 在這裏我們要注意的是我們FORM的表單那個NAME屬性,必須是file[]這個形式的,而且 * NAME值是不能一樣的 * */ private List<FormFile> files = new ArrayList<FormFile>();
public FormFile getFile(int i) { return files.get(i); }
public void setFile(int i,FormFile file) { files.add(file); } public List getFiles(){ return files; } /*private FormFile file;//單個文件上傳文件所用到的屬性 // Constructors
*//** default constructor *//*
// Property accessors public FormFile getFile() { return file; } public void setFile(FormFile file) { this.file = file; }*/ public Long getId() { return this.id; }
public ShrgSchScheduleInfoTForm() { super(); // TODO Auto-generated constructor stub }
public ShrgSchScheduleInfoTForm(Long id, String name, Long scheduleManageId, String classStartTime, String classOverTime, String classRing, Integer createdBy, Timestamp creationDate, Integer lastUpdateBy, Timestamp lastUpdateDate) { super(); this.id = id; this.name = name; this.scheduleManageId = scheduleManageId; this.classStartTime = classStartTime; this.classOverTime = classOverTime; this.classRing = classRing; this.createdBy = createdBy; this.creationDate = creationDate; this.lastUpdateBy = lastUpdateBy; this.lastUpdateDate = lastUpdateDate; }
public void setId(Long id) { this.id = id; }
public String getName() { return this.name; }
public void setName(String name) { this.name = name; }
public Long getScheduleManageId() { return this.scheduleManageId; }
public void setScheduleManageId(Long scheduleManageId) { this.scheduleManageId = scheduleManageId; }
public String getClassStartTime() { return this.classStartTime; }
public void setClassStartTime(String classStartTime) { this.classStartTime = classStartTime; }
public String getClassOverTime() { return this.classOverTime; }
public void setClassOverTime(String classOverTime) { this.classOverTime = classOverTime; }
public String getClassRing() { return this.classRing; }
public void setClassRing(String classRing) { this.classRing = classRing; }
public Integer getCreatedBy() { return this.createdBy; }
public void setCreatedBy(Integer createdBy) { this.createdBy = createdBy; }
public Timestamp getCreationDate() { return this.creationDate; }
public void setCreationDate(Timestamp creationDate) { this.creationDate = creationDate; }
public Integer getLastUpdateBy() { return this.lastUpdateBy; }
public void setLastUpdateBy(Integer lastUpdateBy) { this.lastUpdateBy = lastUpdateBy; }
public Timestamp getLastUpdateDate() { return this.lastUpdateDate; }
public void setLastUpdateDate(Timestamp lastUpdateDate) { this.lastUpdateDate = lastUpdateDate; }
}
<%@ page language="java" pageEncoding="UTF-8"%> <%@ page import="java.util.*,com.avedia.common.page.*"%> <%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%> <%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%> <%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic"%> <%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html:html lang="true"> <head> <html:base /> <META http-equiv="Content-Type" content="text/html; charset=UTF-8"> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <link rel="stylesheet" href="../css/combined.css" type="text/css"> <link href="<bean:message key='common.css.stye'/>" rel="stylesheet" type="text/css" /> <script language="javascript" src="../js/utils.js"></script> <script language="javascript" src="../js/utilTools.js"></script> <script language="javascript" src="../js/additional.js"></script> <script language="javascript" src="../js/time.js"></script> <script language="javascript" src="<bean:message key='delete.js.choose'/>"></script> <script type="text/javascript" src="../js/jquery-1.2.3.min.js"></script> <% response.setHeader("Prama", "No-cache"); response.setHeader("Cache-Control", "no-cache"); response.setDateHeader("Expires", 0); %> <script type="text/javascript"> /* 表格的全選 ING..... */ function checkAllBox() { var check_Box = document.getElementById("check_Box"); var check_role_Box = document.getElementsByName("minuteRingListAll"); // alert(check_role_Box); if (check_Box.checked) { for (var i = 0; i < check_role_Box.length; i++) { check_role_Box[i].checked = true; } } else { for (var i = 0; i < check_role_Box.length; i++) { check_role_Box[i].checked = false; } } }; /* 一個簡單的刪除, RELAX..... */ function btndel() { var length = $("#ta tr").length; alert(length); var form = document.commonForm; var array = document.getElementsByName("minuteRingListAll"); var id = ""; for (var i = 0; i < array.length; i++) { if (array[i].checked) { if (array[i].value == "") { } id += array[i].value + ","; } } alert(id); if (id != "") { id = id.substring(0, id.length - 1); var idd = document.getElementById("hidden_scheduleManageId").value; form.action = '/mbs/ring/getMinuteRing.do?do=delMinuteRing&idd=' + idd + '&id=' + id; form.submit(); } else { alert('請選擇你要刪除的行'); } }; /* 一個用FORM表單進行提交的例子, RELAX..... */ function saveTab() { var id = document.getElementById("hidden_scheduleManageId").value; alert(id); var form = document.commonForm; alert($("#ta tr").length); form.action = '/mbs/ring/getMinuteRing.do?do=saveUpdate&id=' + id; form.submit(); }; /* 我們給我們的表單增添行數 ING....*/ function btnAddRow() { /* <div align="left"><input type="checkbox" name="minuteRingListAll" id="minuteRingListAll" ></div> */ var length = $("#ta tr").length; var trHTML = "<tr id=row"+length+"><td>" + "<input type='checkbox' name='minuteRingListAll' value=\"${ring.id }\"/></td>" + "<td><input type=\"text\" name=\"name\" value=\"22\" /></td>" + "<td><input type=\"text\" name=\"classStartTime\" value=\"22\" /></td>" + "<td><input type=\"text\" name=\"classOverTime\" value=\"22\" /></td>" + "<td><input type=\"file\" name=\"file["+length+"]\" value=\"22\" /></td>" + "</tr>"; $("#ta").append(trHTML);//在最後開始插入數據 /* $("table tr:eq(2)").after(tr); }); 在第三行插入數據*/ }; /*我們給我的數據進行全部的保存 ING...... */ function saveAll() { var mainId = document.getElementById("hidden_scheduleManageId").value; var tableData = new Array(); var content = ""; $("#ta tr").each(function(trindex, tritem) { tableData[trindex] = new Array(); if (trindex != 0) { $(tritem).find("td>input").each(function(tdindex, tditem) { //tableData[trindex][tdindex] = $(tditem).text(); if(tditem.value.length==0){ content += 'y' + ","; }else{ content += $.trim(tditem.value) + ","; //content += tditem.value + ","; } }); content = content.substr(0, content.length - 1); content += ";"; } }); if (content.length > 1) { var path = '/mbs/ring/getMinuteRing.do?do=save'; $.ajax({ type : "POST", url : path, /* "content="+content+"id="+mainId, 不行*/ data : { "id" : mainId, "content" : content }, async : false, dataType : "json", success : function(data) { window.location.href = '/mbs/ring/getMinuteRing.do?do=getMinuteRing&id=' + mainId; alert(data.name); //window.location.reload(true);在這裏不建議去用,但是這裏用了也不行的 PAST } }); } } /* 我們刪除我們真實與虛假的行 ING...... */ function delRow() { var form = document.commonForm; var array = document.getElementsByName("minuteRingListAll"); var id = ""; for (var i = 0; i < array.length; i++) { if (array[i].checked) { id += array[i].value + ","; } } if (id != "") { id = id.substring(0, id.length - 1); var hidden_scheduleManageId = document .getElementById("hidden_scheduleManageId").value; } else { alert('請選擇你要刪除的行'); } //刪除選擇的行 //alert($("#tb_data").html()); var path = '/mbs/ring/getMinuteRing.do?do=delRow'; $.ajax({ cache : false, type : "POST", url : path, //把表單數據發送到ajax.jsp data : { "id" : id }, //要發送的是ajaxFrm表單中的數據 async : true, dataType : "text", error : function(request) { alert("發送請求失敗!"); }, success : function(data) { $("input:checkbox[name=minuteRingListAll]:checked").parent() .parent().remove(); } }); } /* 上傳上課鈴聲 */ function upLoadClassRing(){ var form = document.commonForm; form.action = "/mbs/ring/getMinuteRing.do?do=upLoadClassRing"; form.submit(); } /* 發送到終端 */ function sendTb(){ alert("11111"); } </script> </head> <body> <form action="/mbs/ring/getMinuteRing.do?do=delMinuteRing" method="post" name="commonForm" enctype="multipart/form-data"> <TABLE width=600 border="0" cellpadding="0" align="center"> <TBODY> <TR> <TD vAlign=bottom noWrap><input type="button" value="發送" οnclick="sendTb()" /></TD> <TD noWrap> <%-- <bean:message key="dept.view.title"/> --%> 詳細鈴表 </TD> <TD vAlign=bottom noWrap><input type="button" value="添加" οnclick="btnAddRow()" /></TD> <!-- <TD vAlign=bottom noWrap><input type="button" value="test" οnclick="Edit()" /></TD> --> <TD vAlign=bottom noWrap><input type="button" id="edit" value="刪除" οnclick="delRow()" /></TD> <TD vAlign=bottom noWrap> <!-- <input type="button" value="刪除" onClick="doDelete('/mbs/getRingList.do?do=delRingList',document.commonForm); return false;"/> --> <!-- <input type="button" value="刪除" onClick="btndel()" /> --> <input type="button" value="保存" onClick="saveAll()" /></TD> <TD vAlign=bottom noWrap><input type="button" οnclick="javascript:history.back();" value="返回"></TD> <!-- <TD vAlign=bottom noWrap><input type="button" οnclick="saveTab()" value="保存"></TD> --> </TR> </TBODY> </TABLE> <center> <div id="tb_data"> <input type="hidden" name="scheduleManageId" value=${id} id="hidden_scheduleManageId"> <table width="800" id="ta" border="1" cellpadding="3" cellspacing="1" bordercolor="#B6B5B5"> <tr> <td scope="col" style="text_align:left;"><input type="checkbox" name="check_Box" id="check_Box" οnclick="checkAllBox()"></td> <td scope="col">課節</td> <td scope="col">上課時間</td> <td scope="col">下課時間</td> <td scope="col"><input type="button" value="上課鈴聲" οnclick="upLoadClassRing()"></td> <!-- <th scope="col">創建人</th> <th scope="col">創建時間</th> <th scope="col">最後修改人</th> <th scope="col">最後修改時間</th> --> </tr> <logic:iterate id="ring" indexId="indexid" name="minuteRing" scope="request"> <%-- <input type="hidden" name="scheduleManageId" value=${ring.scheduleManageId } id="hidden_scheduleManageId"> --%> <input type="hidden" name="hidden" value=${ring.id } id="hidden"> <tr> <td><input type="checkbox" name="minuteRingListAll" id="minuteRingListAll" value="${ring.id }"></td> <%-- <td>${ring.name }</td> <td>${ring.classStartTime }</td> <td>${ring.classOverTime }"</td> <td>${ring.classRing }"</td> --%> <td><input type="text" name="name" value="${ring.name }" /></td> <td><input type="text" name="classStartTime" value="${ring.classStartTime }" οnclick="_SetTime(this)" readonly="readonly"/></td> <td><input type="text" name="classOverTime" value="${ring.classOverTime }" οnclick="_SetTime(this)" readonly="readonly"/></td> <td><input type="file" name="file[${ring.id }]" /></td> <%-- <th>${ring.createdBy }</th> <th>${ring.creationDate }</th> <th>${ring.lastUpdateBy }</th> <th>${ring.lastUpdateDate }</th> --%> </tr> </logic:iterate> </table> </div> </center> </form> </body> </html:html>