private int nowPage; public int getNowPage() { return nowPage; } public void setNowPage(int nowPage) { this.nowPage = nowPage; }
public String showEmployee(){
List<Employee> list = EmployeeService.getEmployeeByPage(nowPage); this.getSession().put("list", list); return "success"; }
} package com.zzab.bean;public class Employee { private int id; private String name; private String phone; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } public Employee() { super(); // TODO Auto-generated constructor stub } public Employee(int id, String name, String phone) { super(); this.id = id; this.name = name; this.phone = phone; } }package com.zzab.service;import java.util.ArrayList; import java.util.List;import com.zzab.bean.Employee;public class EmployeeService {
public static List<Employee> getEmployees(){ List<Employee> employees = new ArrayList<Employee>(); for (int i = 0; i < 20; i++) { Employee employee = new Employee(i, "张三" + i, "135" + i); employees.add(employee); } return employees; }
public static List<Employee> getEmployeeByPage(int nowPage){ List<Employee> employees = getEmployees(); List<Employee> list = new ArrayList<Employee>(); int index = 0; for (int i = (nowPage * 10); i < employees.size(); i++) {
现在就是用的location<a href="javascript:void(0);" onclick="location.replace('customerSerch.do?method=backCustomerXx')">
后台存储信息的方式可能不对,一般存储到session中 ,就不会出现这种情况了.
我看你的问题的感觉是,似乎所有用户都在对一个信息进行操作....
package com.zzab.action;import java.util.Map;import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletResponse;import org.apache.struts2.ServletActionContext;import com.opensymphony.xwork2.ActionContext;public class BaseAction {
public Map<String,Object> getRequest(){
return (Map)ActionContext.getContext().get("request");
}
public Map<String,Object> getApplication(){
return (Map)ActionContext.getContext().getApplication();
}
public Map<String,Object> getSession(){
return (Map)ActionContext.getContext().getSession();
}
public HttpServletResponse getResponse(){
HttpServletResponse response = ServletActionContext.getResponse();
response.setCharacterEncoding("uft-8");
response.setContentType("text/html;charset=utf-8");
return response;
}
}package com.zzab.action;import java.util.List;import com.zzab.bean.Employee;
import com.zzab.service.EmployeeService;public class EmployeeAction extends BaseAction {
private int nowPage; public int getNowPage() {
return nowPage;
} public void setNowPage(int nowPage) {
this.nowPage = nowPage;
}
public String showEmployee(){
List<Employee> list = EmployeeService.getEmployeeByPage(nowPage);
this.getSession().put("list", list);
return "success";
}
}
package com.zzab.bean;public class Employee {
private int id;
private String name;
private String phone;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public Employee() {
super();
// TODO Auto-generated constructor stub
}
public Employee(int id, String name, String phone) {
super();
this.id = id;
this.name = name;
this.phone = phone;
}
}package com.zzab.service;import java.util.ArrayList;
import java.util.List;import com.zzab.bean.Employee;public class EmployeeService {
public static List<Employee> getEmployees(){
List<Employee> employees = new ArrayList<Employee>();
for (int i = 0; i < 20; i++) {
Employee employee = new Employee(i, "张三" + i, "135" + i);
employees.add(employee);
}
return employees;
}
public static List<Employee> getEmployeeByPage(int nowPage){
List<Employee> employees = getEmployees();
List<Employee> list = new ArrayList<Employee>();
int index = 0;
for (int i = (nowPage * 10); i < employees.size(); i++) {
if(index == 10){
break;
}
list.add(employees.get(i));
index ++;
}
return list;
}
}
stru.xml文件的内容<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts><package namespace="/test" name="default" extends="struts-default">
<action name="show" class="com.zzab.action.EmployeeAction" >
<result name="success">/employee.jsp</result>
</action>
</package>
</struts> <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'employee.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
--> </head>
<body>
<table>
<tr>
<td>id</td>
<td>name</td>
<td>phone</td>
<td>action</td>
</tr>
<s:iterator var="employee" value="#attr.list">
<tr>
<td>${employee.id }</td>
<td>${employee.name }</td>
<td>${employee.phone }</td>
<td></td>
</tr>
</s:iterator>
</table>
<a href="test/show!showEmployee?nowPage=${nowPage-1 }">上一页</a>
<a href="test/show!showEmployee?nowPage=${nowPage+1 }">下一页</a>
</body>
</html><%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<a href="test/show!showEmployee">ccc</a>
</body>
</html>这就是基本的全部了...web.xml就不用我发了吧 汗
然后 将hashmap 存储 到session中当需要返回原来页面,调用返回原页面所操作的servlet 取得session中的相应信息,根据获取信息,再次查询出最初信息,并给JSP页面。PS 我将分页 操作 专门做到一个类中的。感谢 RainOnl !