package com.action;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.RequestAware;
import com.model.RevenueReport;
import com.opensymphony.xwork2.Action;
import com.service.BusinessService;
public class GridViewAction implements Action , RequestAware {
private List<RevenueReport> aaData;
private Map<String, Object> request;
public List<RevenueReport> getAaData() {
return aaData;
}
public void setAaData(List<RevenueReport> aaData) {
this.aaData = aaData;
}
public String execute() throws Exception {
HttpServletRequest request = ServletActionContext.getRequest();
String type = request.getParameter("type");
System.out.println("type:" + type);
aaData = BusinessService.getCompanyList();
return SUCCESS;
}
public void setRequest(Map<String, Object> arg0) {
this.request = arg0;
}
}
模型类:
package com.model;
public class RevenueReport {
private String company;
private String country;
private String year;
private String revenue;
public String getCompany() {
return company;
}
public void setCompany(String company) {
this.company = company;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public String getYear() {
return year;
}
public void setYear(String year) {
this.year = year;
}
public String getRevenue() {
return revenue;
}
public void setRevenue(String revenue) {
this.revenue = revenue;
}
@Override
public String toString() {
return "RevenueReport [company=" + company + ", country=" + country
+ ", year=" + year + ", revenue=" + revenue + "]";
}
public RevenueReport(String company, String country, String year,
String revenue) {
super();
this.company = company;
this.country = country;
this.year = year;
this.revenue = revenue;
}
}
服务类DAO
package com.service;
import java.util.LinkedList;
import java.util.List;
import com.model.*;
public class BusinessService {
public static List<RevenueReport> getCompanyList(){
List<RevenueReport> listofCompany = new LinkedList<RevenueReport>();
for(int i = 0; i < 100; i++){
RevenueReport report = new RevenueReport("company" + i, "country" + i, "year"+i, "revenue" + i);
listofCompany.add(report);
}
return listofCompany;
}
}
struts2.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 name="default" extends="json-default">
<action name="dataTableAction" class="com.action.GridViewAction">
<result type="json"></result>
</action>
</package>
</struts>
JSP 文件:
<%@ 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">
-->
<link href="./css/jquery-ui.css" rel="stylesheet"/>
<script src="./js/jquery.js"></script>
<script src="./js/jquery.dataTables.js"></script>
<script>
$(document).ready(
function (){
$("#example").dataTable({
"sPaginationType":"full_numbers",
"bProcessing":false,
"bServerSide":false,
"sAjaxSource":"dataTableAction?type='FTK'",
"bJqueryUI":true,
"aoColumns":[{"mData":"company"},
{"mData":"country"},
{"mData":"year"},
{"mData":"revenue"}]
});
}
);
</script>
</head>
<body id="dt_example">
<div id="container">
<div>
<table id="example" class="jqueryDataTable">
<thead>
<tr>
<th>Company</th>
<th>county</th>
<th>year</th>
<th>revenue</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
</div>
</div>
This is my JSP page. <br>
</body>
</html>