//----------------事务(修改、删除、保存) Session s=getSession(); Transaction t=s.beginTransaction(); getSession().delete(persistentInstance); t.commit(); //查询语句 public List<Cls> i = new ArrayList<Cls>(); public String execute(){ ClsDAO clsDAO = new ClsDAO(); i=clsDAO.findAll(); return "1"; } //删除语句
StuDAO In=new StuDAO(); i=In.findstuid(stu_id); In.delete(i.get(0)); //添加语句 public String stu_name; public Integer stu_age; public Integer cls_id; public String execute(){ ClsDAO clsDAO = new ClsDAO(); Cls cls = clsDAO.findById(cls_id); StuDAO stuDAO = new StuDAO(); Stu stu = new Stu(cls,stu_name,stu_age); stuDAO.save(stu); return "1"; } //修改语句 public String stu_name; public Integer stu_age; public Integer cls_id; public Integer stu_id; public String execute(){ ClsDAO clsDAO=new ClsDAO(); Cls cls=clsDAO.findById(cls_id); Stu stu=new Stu(cls,stu_name,stu_age,stu_id);//自己写个参数包含主键ID的构造函数 StuDAO studentsDAO=new StuDAO(); studentsDAO.merge(stu); return "1"; } //DAO里面学生表按照班级ID查询 public List findClsid(Integer cls_id) { String queryString = "from Stu where cls_id = ‘"+cls_id+"‘"; Query queryObject = getSession().createQuery(queryString); return queryObject.list(); } //JS 方式 伪网页直接访问action,用于无触发情况下,需要调用后台访问数据库取得实时数据的做法 <script type="text/javascript"> window.location.href="all.action"; </script>
//S标签,遍历List数组,生成表格等数据表 <%@ taglib prefix="s" uri="/struts-tags"%> <s:iterator value="i"> <s:property value="stuId"/> </s:iterator>
//struts2 <constant name="struts.i18n.encoding" value="utf-8" /> <package name="denglu" extends=" struts-default"> <action name="all" class="com.all"> <result name="1">/all.jsp</result> </action> <action name="up_save" class="com.up_save"> <result name="1" type="redirectAction">all</result> </action> </package>
a标签返回上一步
<a href="javascript:history.go(-1)">返回</a>
JS表单不能为空检测
<script type="text/javascript"> function myCheck() { for(var i=0;i<document.form1.elements.length-1;i++) { if(document.form1.elements[i].value=="") { document.getElementById("showResult").innerHTML="请输入学生完整信息!"; document.form1.elements[i].focus(); return false; } } return true; } </script> <form name="form1" action="add.action" method="post" onSubmit="return myCheck()"></form> <span id="showResult" style="color: red;"></span>
原文:http://www.cnblogs.com/lw1234/p/4571251.html