Hibernate+Struts2+jsp 修改用户信息

时间:2022-03-23 04:54:41

Hibernate+Struts2+jsp   修改用户信息

在用户列表页面点击修改,进入修改页面

Hibernate+Struts2+jsp   修改用户信息

修改薪酬为555,点击提交,重新跳回该页面

Hibernate+Struts2+jsp   修改用户信息

修改成功

关键代码如下

基层的代码,这里增加了一个根据用户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类的方法执行操作

 <%@page import="com.hanqi.service.UserService"%>
<%@page import="com.hanqi.utility.USer"%>
<%@ 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>
<%
int user_id = Integer.parseInt(request.getAttribute("lll").toString()); USer us = new UserService().hah(user_id) ;
//out.print(us) ;
%>
<form action="modifyuser.action" method="post">
id<input type="text" value="<%out.print(user_id) ;%>"name="us.user_id" readonly><br><br>
姓名<input type="text" name="us.name" value="<%out.print(us.getName()) ;%>"><br><br>
密码<input type="password" name="us.password" value="<%out.print(us.getPassword()) ;%>"><br><br>
生日<input type="text" name="us.brithday" value="<%out.print(us.getBrithday()) ;%>"><br><br>
薪酬<input type="text" name="us.money" value="<%out.print(us.getMoney()) ;%>"><br><br>
<input type="submit" value="提交" ><br>
</body>
</html>

Action类关键代码如下

 //修改用户
public String modifyuser()
{
String rtn = "fail" ; try
{
//获取user_id
//得到原生request
HttpServletRequest hsr = ServletActionContext.getRequest() ; String userid = hsr.getParameter("us.user_id") ; System.out.println(userid); int user_id = Integer.parseInt(userid) ; new UserService().modify(user_id, us); ; rtn = "success" ; }catch(Exception e)
{
e.printStackTrace();
} return rtn;
} //查询单个
public String selectu()
{
String rtn = "fail" ; try
{
//获取user_id
//得到原生request
HttpServletRequest hsr = ServletActionContext.getRequest() ; String useid = hsr.getParameter("userid") ; int user_id = Integer.parseInt(useid) ; /*
* 将从用户列表页面获取的id 放入request
* 目的:为了在修改页面获取当前行的用户id
*/
hsr.setAttribute("lll", user_id); //执行删除
new UserService().hah(user_id); rtn = "success" ; }catch(Exception e)
{
e.printStackTrace();
} return rtn;
}

Struts的配置代码如下:

 <!-- 修改 -->
<action name="modifyuser" class="com.hanqi.action.USerAction" method="modifyuser"> <result name="fail">/WEB-INF/pages/fail.jsp</result> <result name="success" type="redirectAction">selectUSer</result> </action> <!-- 查询单个 -->
<action name="modify" class="com.hanqi.action.USerAction" method="selectu"> <result>/WEB-INF/pages/modify.jsp</result> </action>

此时完成