1.首先用get的方法传递一个页数过去
2.通过Struts2跳转到Action
3.通过request接受主页面index传过的页数,此时页数是1,
然后调用service层的方法获取DAO层分页查询的方法
service
DAO
然后在放回到action层,重新把页数以及list集合里的user对象内容用request写进去,传的下个页面
Struts
接受user对象便利显示在页面
接收页数,并用get方法重写页数进行传递,形成方法链
注意:修改和删除,在重定向action的时候也把页数传递过去
具体代码:
<%@ 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>
<a href="addUser">添加用户</a> <br><br>
<a href="selectUser?yeshu=1">用户列表</a>
</body>
</html>
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd"> <struts>
<!-- 覆盖默认的过滤的扩展名 -->
<constant name="struts.action.extension" value="do,action,,"></constant> <!-- 定义包 -->
<package name="text" extends="struts-default" > <action name="addUser">
<result>/WEB-INF/pages/addUser.jsp</result>
</action> <!--保存user -->
<action name="saveUser" class="com.hanqi.action.UserAction" method="saveUser"> <result name="fail">/WEB-INF/pages/fail.jsp</result>
<result name="success">/WEB-INF/pages/success.jsp</result>
</action> <!-- 查询用户/分页 -->
<action name="selectUser" class="com.hanqi.action.UserAction" method="selectUser">
<result name="fail">/WEB-INF/pages/fail.jsp</result>
<result name="success">/WEB-INF/pages/selectUser.jsp</result>
</action> <!-- 删除 -->
<action name="deleteUser" class="com.hanqi.action.UserAction" method="deleteUser">
<result name="fail">/WEB-INF/pages/fail.jsp</result>
<result name="success" type="redirectAction">selectUser?yeshu=1</result>
</action> <!-- 修改 ,跳转到修改页面-->
<action name="updateUser" >
<result>/WEB-INF/pages/updateUser.jsp</result>
<result name="fail">/WEB-INF/pages/fail.jsp</result>
</action>
<!-- 修改输入 -->
<action name="updateUser2" class="com.hanqi.action.UserAction" method="updateUser">
<result name="fail">/WEB-INF/pages/fail.jsp</result>
<result name="success" type="redirectAction">selectUser?yeshu=1</result>
</action> </package> </struts>
package com.hanqi.action; import java.util.List;
import java.util.Map; import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest; import org.apache.struts2.ServletActionContext; import com.hanqi.entity.User;
import com.hanqi.service.UserService;
import com.opensymphony.xwork2.ActionContext; public class UserAction { //域模型方式
private User user; public User getUser() {
return user;
} public void setUser(User user) {
this.user = user;
} //处理保存user的方法
public String saveUser()
{
String rtn="fail"; System.out.println(user); try
{
//调用业务逻辑层Service(模型层)
User u1 = new UserService().add(user); //使用request转发到下一个页面去
ActionContext ac = ActionContext.getContext();
Map<String, Object> mo = (Map<String, Object>)ac.get("request");
mo.put("user", u1); rtn="success";
}
catch(Exception e)
{
e.printStackTrace();
}
return rtn;
} //查询用户
public String selectUser()
{
String rtn ="fail";
try
{
//调用查询的方法 //得到原生 的request
HttpServletRequest hsr = ServletActionContext.getRequest();
int yeshu = Integer.parseInt(hsr.getParameter("yeshu"));//接收index传过来的yeshu List<User> lu = new UserService().getAll( yeshu); hsr.setAttribute("yeshu", yeshu);//重新添加request传过的页数,改变页码(页码链)
hsr.setAttribute("userlist", lu);
rtn="success";
}
catch(Exception e)
{
e.printStackTrace();
}
return rtn; } //删除用户
public String deleteUser()
{
String rtn ="fail";
try
{
//获取userid
//得到原生 的request
HttpServletRequest hsr = ServletActionContext.getRequest();
String uid = hsr.getParameter("userid");
//执行删除
new UserService().delete(Integer.parseInt(uid)); rtn="success";
}
catch(Exception e)
{
e.printStackTrace();
} return rtn;
} //修改用户 public String updateUser()
{
String rtn ="fail"; try
{ //获取userid
//得到原生 的request
HttpServletRequest hsr = ServletActionContext.getRequest();
String userid = hsr.getParameter("user.userID");//通过request获取页面传过的id //调用业务逻辑层Service(模型层)
int userID = Integer.parseInt(userid) ;//吧页面传过来的userid转换成DAO层的int型进行对应
//System.out.println(userID);
new UserService().update(userID, user); rtn="success";
}
catch(Exception e)
{
e.printStackTrace();
} return rtn;
} //查询单个
public String selectOneUser()
{
String rtn ="fail";
try
{
//调用查询单个的方法
//获取userid
//得到原生 的request
HttpServletRequest hsr = ServletActionContext.getRequest();
String userid = hsr.getParameter("user.userID");
int userID = Integer.parseInt(userid);
//将从用户列表页面获取的id 放入request,目的:为了在修改页面获取当前行的用户id hsr.setAttribute("userid", userID);
new UserService().getOntUser(userID);
rtn="success";
}
catch(Exception e)
{
e.printStackTrace();
} return rtn;
} }
package com.hanqi.service; import java.util.List; import com.hanqi.dao.UserDAO;
import com.hanqi.entity.User; public class UserService { //添加User
public User add(User user)
{
return new UserDAO().insert(user);
} //查询全表
public List<User> getAll(int yeshu)
{
//return new UserDAO().getAll(yeshu);
return new UserDAO().getFen(yeshu);//分页查询
} //删除
public void delete(int userID)
{
new UserDAO().delete(userID);
} //修改
public void update(int userID, User user)
{
new UserDAO().updateUser(userID, user);
}
//查询单个
public User getOntUser(int userID)
{
return new UserDAO().getUser(userID);
}
}
package com.hanqi.dao; import java.util.ArrayList;
import java.util.List; import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry; import com.hanqi.entity.User; public class UserDAO {
private Configuration cfg =null;
private ServiceRegistry sr =null;
private SessionFactory sf =null;
private Session se =null;
private Transaction tr =null;
//构造方法
public UserDAO()
{
//初始化Hibernate
cfg= new Configuration().configure();//获取配置文件
sr= new StandardServiceRegistryBuilder()//注册
.applySettings(cfg.getProperties())
.build();
} //
private void init()
{
sf = cfg.buildSessionFactory(sr);
se =sf.openSession();
tr = se.beginTransaction();
}
private void destory()
{
tr.commit();
se.close();
sf.close();
} //保存user
public User insert(User user)//添加
{ init();
se.save(user);
destory();
return user; } //查询列表
public List<User> getAll(int yeshu)
{
List<User> rtn = new ArrayList<>();
init();
//rtn = se.createQuery("from User").list(); //类名
//设置开始行号;页码=2
//(页码-1)*每页行数
rtn=se.createQuery("from User order by userID").setMaxResults(3)
.setFirstResult((yeshu-1)*2).list();
destory();
return rtn;
} //删除
public void delete(int userID)
{
init();
//获取对象
User u = (User)se.get(User.class, userID);
se.delete(u); destory();
} //修改
public void updateUser(int userID, User user)
{
init();
//获取对象
User u = (User)se.get(User.class, userID);//通过session获取userID的信息
u.setUserId(userID);
u.setUserName(user.getUserName());
u.setMoney(user.getMoney());
u.setBirthday(user.getBirthday()); destory(); }
//查询单条记录
public User getUser (int userID)
{
User rtn = new User();
init();
rtn = (User)se.get(User.class, userID); destory();
return rtn; } //分页查询
public List<User> getFen(int yeshu)
{
init();
List<User> lu = new ArrayList<>();
//设置开始行号;页码=2
//(页码-1)*每页行数
lu=se.createQuery("from User order by userID").setMaxResults(3)
.setFirstResult((yeshu-1)*3).list();
destory();
return lu;
} }
<%@page import="com.hanqi.entity.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>查询所有</title>
</head>
<body>
<%
//接收action请求的查询方法传过的集合
List<User> lu = (List<User>)request.getAttribute("userlist"); for(User u : lu)
{
out.print(u+"【<a href='updateUser?userid="+u.getUserId()+
"'>修改</a>】"+"【<a href='deleteUser?userid="+u.getUserId()+"'>删除</a>】 <br>");
} %>
<br>
<%
int yeshu = Integer.parseInt(request.getAttribute("yeshu").toString());
if(lu.size()==0)
{
out.print("页码超出范围");
response.setHeader("refresh", "3;url=selectUser?yeshu=1");
}
else
{
%> <a href="selectUser?yeshu=<%=yeshu-1 %> ">上一页</a>
<a href="selectUser?yeshu=<%=yeshu+1 %> ">下一页</a>
<%
}
%>
</body>
</html>
1修改
删除