81,092
社区成员
发帖
与我相关
我的任务
分享
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;
}
}
<?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>