Struts2-修改数据
1 <body> 2 用户信息:<br><br> 3 <% 4 List<User> lu = (List<User>)request.getAttribute("userlist"); 5 %> 6 <table> 7 <tr> 8 <th>用户名</th> 9 <th>姓名</th> 10 <th>性别</th> 11 <th>生日</th> 12 <th>操作</th> 13 </tr> 14 <% 15 for(User u:lu){ 16 int a = u.getUserid(); 17 String b = u.getUsername(); 18 String c = u.getUsersex(); 19 Date d = u.getUserbirthday(); 20 %> 21 <tr> 22 <td><%out.print(a); %></td> 23 <td><%out.print(b); %></td> 24 <td><%out.print(c); %></td> 25 <td><%out.print(d); %></td> 26 <td><a href="deleteUser?userid=<%out.print(a); %>">删除</a>/ 27 <a href="updateUser?userid=<%out.print(a); %>">修改</a></td> 28 </tr> 29 <% 30 } 31 %> 32 </table> 33 </body>
1 <body> 2 <% 3 User u = (User)request.getAttribute("user"); 4 out.print(u); 5 %> 6 <form action="delete" method="post"> 7 用户名:<input type="text" value="<%out.print(u.getUserid());%>" name="user.userid"><br><br> 8 姓名:<input type="text" value="<%out.print(u.getUsername());%>" name="user.username"><br><br> 9 性别:<input type="text" value="<%out.print(u.getUsersex());%>" name="user.usersex"><br><br> 10 生日:<input type="text" value="<%out.print(u.getUserbirthday());%>" name="user.userbirthday"><br><br> 11 <input type="submit" value="提交"> 12 </form> 13 14 </body>
1 <!-- 修改用户 --> 2 <action name="updateUser" class="com.hq.action.UserAction" method="selectDan"> 3 <result>WEB-INF/pages/delete.jsp</result> 4 <result name="fail">WEB-INF/pages/fail.jsp</result> 5 </action> 6 <action name="delete" class="com.hq.action.UserAction" method="updateDan"> 7 <result type="redirectAction">selectUser</result> 8 <result name="fail">WEB-INF/pages/fail.jsp</result> 9 </action>
1 public String selectDan(){ 2 String rtn = "fail"; 3 try{ 4 HttpServletRequest hsr = ServletActionContext.getRequest(); 5 String uid = hsr.getParameter("userid"); 6 int userid = Integer.parseInt(uid); 7 User u = new UserService().selectDan(userid); 8 hsr.setAttribute("user", u); 9 rtn = "success"; 10 }catch(Exception e){ 11 e.printStackTrace(); 12 } 13 return rtn; 14 } 15 public String updateDan(){ 16 String rtn ="fail"; 17 try{ 18 u1 = new UserService().updateUser(user); 19 rtn = "success"; 20 }catch(Exception e){ 21 e.printStackTrace(); 22 } 23 return rtn; 24 }
1 // 修改 2 public User updateUser(User user){ 3 return new UserDao().update(user); 4 } 5 // 单查 6 public User selectDan(int userid){ 7 return new UserDao().selectUser(userid); 8 }
public User update(User user){ init(); User u = (User)se.get(User.class, user.getUserid()); u.setUsername(user.getUsername()); u.setUsersex(user.getUsersex()); u.setUserbirthday(user.getUserbirthday()); desory(); return u; }