Hibernate+Struts2+jsp 修改用户信息
在用户列表页面点击修改,进入修改页面
修改薪酬为555,点击提交,重新跳回该页面
修改成功
关键代码如下
基层的代码,这里增加了一个根据用户id查询的方法
dao层
//修改 public USer modify( int user_id,USer user) { init() ; //获取对象 USer us = (USer)se.get(USer.class, user_id) ; us.setBrithday(user.getBrithday()); us.setMoney(user.getMoney()); us.setName(user.getName()); us.setPassword(user.getPassword()); us.setUser_id(user.getUser_id()); //se.saveOrUpdate(us); destory(); return us; } //查询单个 public USer selectu( int user_id) { init() ; //获取对象 USer us = (USer)se.get(USer.class, user_id) ; destory(); return us; }
用户列表页面 ,点击修改通过Action(如果页面在webcotent下可不用Action)跳转到修改页面
<%@page import="com.hanqi.utility.USer"%> <%@page import="java.util.List"%> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <% List<USer> list = (List<USer>)request.getAttribute("selectuser") ; for(USer us : list ) { out.print(us + "<a href='deleteuser?user_id="+us.getUser_id()+"'> 【删除】</a>" +"<a href='modify?userid="+us.getUser_id()+"'> 【修改】</a>" +"<br>") ; } %> </body> </html>
修改页面代码如下,点击提交进入Action并调用Action类的方法执行操作
1 <%@page import="com.hanqi.service.UserService"%> 2 <%@page import="com.hanqi.utility.USer"%> 3 <%@ page language="java" contentType="text/html; charset=UTF-8" 4 pageEncoding="UTF-8"%> 5 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 6 <html> 7 <head> 8 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 9 <title>Insert title here</title> 10 </head> 11 <body> 12 <% 13 int user_id = Integer.parseInt(request.getAttribute("lll").toString()); 14 15 USer us = new UserService().hah(user_id) ; 16 //out.print(us) ; 17 %> 18 <form action="modifyuser.action" method="post"> 19 id<input type="text" value="<%out.print(user_id) ;%>"name="us.user_id" readonly><br><br> 20 姓名<input type="text" name="us.name" value="<%out.print(us.getName()) ;%>"><br><br> 21 密码<input type="password" name="us.password" value="<%out.print(us.getPassword()) ;%>"><br><br> 22 生日<input type="text" name="us.brithday" value="<%out.print(us.getBrithday()) ;%>"><br><br> 23 薪酬<input type="text" name="us.money" value="<%out.print(us.getMoney()) ;%>"><br><br> 24 <input type="submit" value="提交" ><br> 25 </body> 26 </html>
Action类关键代码如下
1 //修改用户 2 public String modifyuser() 3 { 4 String rtn = "fail" ; 5 6 try 7 { 8 //获取user_id 9 //得到原生request 10 HttpServletRequest hsr = ServletActionContext.getRequest() ; 11 12 String userid = hsr.getParameter("us.user_id") ; 13 14 System.out.println(userid); 15 16 int user_id = Integer.parseInt(userid) ; 17 18 new UserService().modify(user_id, us); ; 19 20 rtn = "success" ; 21 22 }catch(Exception e) 23 { 24 e.printStackTrace(); 25 } 26 27 return rtn; 28 } 29 30 //查询单个 31 public String selectu() 32 { 33 String rtn = "fail" ; 34 35 try 36 { 37 //获取user_id 38 //得到原生request 39 HttpServletRequest hsr = ServletActionContext.getRequest() ; 40 41 String useid = hsr.getParameter("userid") ; 42 43 int user_id = Integer.parseInt(useid) ; 44 45 /* 46 * 将从用户列表页面获取的id 放入request 47 * 目的:为了在修改页面获取当前行的用户id 48 */ 49 hsr.setAttribute("lll", user_id); 50 51 //执行删除 52 new UserService().hah(user_id); 53 54 rtn = "success" ; 55 56 }catch(Exception e) 57 { 58 e.printStackTrace(); 59 } 60 61 return rtn; 62 }
Struts的配置代码如下:
1 <!-- 修改 --> 2 <action name="modifyuser" class="com.hanqi.action.USerAction" method="modifyuser"> 3 4 <result name="fail">/WEB-INF/pages/fail.jsp</result> 5 6 <result name="success" type="redirectAction">selectUSer</result> 7 8 </action> 9 10 <!-- 查询单个 --> 11 <action name="modify" class="com.hanqi.action.USerAction" method="selectu"> 12 13 <result>/WEB-INF/pages/modify.jsp</result> 14 15 </action>
此时完成