struts2 整合 jQuery-easyUI之datagrid小例子

2018-09-12 13:32 更新

运行结果:

jsp:

  1. <%@ page language="java" pageEncoding="UTF-8"%>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
  3. "http://www.w3.org/TR/html4/loose.dtd">
  4. <html>
  5. <head>
  6. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  7. <title>easyUi-dataGrid</title>
  8. <link rel="stylesheet" type="text/css" href="easyUi/easyui.css">
  9. <script type="text/javascript" src="js/jquery-1.8.0.min.js"></script>
  10. <script type="text/javascript" src="js/jquery.easyui.min.js"></script>
  11. <script type="text/javascript">
  12. var xmlhttp;
  13. function verify(){
  14. if(window.XMLHttpRequest){
  15. xmlhttp = new XMLHttpRequest();
  16. if(xmlhttp.overrideMimeType){
  17. xmlhttp.overrideMimeType("text/xml");
  18. }
  19. }else if(window.ActiveXObject){
  20. var activerxName = ["MSXML2.XMLHTTP","Microsoft.XMLHTTP"];
  21. for(var i=0;i<activerxName.length;i++){
  22. try{
  23. xmlhttp = new ActiveXObject(activerxName[i]);
  24. break;
  25. }catch(e){
  26. }
  27. }
  28. }
  29. if(!xmlhttp){
  30. alert("XMLHttpRequest对象创建失败");
  31. return;
  32. }
  33. xmlhttp.onreadystatechange = callback;
  34. xmlhttp.open("POST","studentAction.action?page=1&rows=5","true");
  35. xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
  36. xmlhttp.send();
  37. }
  38. function callback(){
  39. if(xmlhttp.readyState == 4){
  40. if(xmlhttp.status == 200){
  41. var oBook = eval('(' + xmlhttp.responseText + ')');
  42. $.messager.alert('test jsonData',xmlhttp.responseText);
  43. }
  44. }
  45. }
  46. $(function(){
  47. $("#tt").datagrid({
  48. title:'DataGrid例子',
  49. width:600,
  50. height:400,
  51. pageSize:5,
  52. pageList:[5,10,15,20],
  53. nowrap:false,
  54. striped:true,
  55. collapsible:true,
  56. url:'studentAction.action',
  57. loadMsg:'数据加载中...',
  58. sortName:'code',
  59. sortOrder:'desc',
  60. remoteSort:false,
  61. pagination :true,//可以不要下面的 注释部分 要显示中文 可以easyui-lang-zh_CN.js
  62. frozenColumns:[[
  63. {field:'ck',checkbox:true}
  64. ]],
  65. columns:[[
  66. {title:'学号',field:'id',width:'50',rowspan:2,align:'center'},
  67. {title:'姓名',field:'name',width:'60',rowspan:2,align:'center'},
  68. {title:'性别',field:'sex',width:'50',rowspan:2,align:'center'},
  69. {title:'年龄',field:'age',width:'50',rowspan:2,align:'center'},
  70. {title:'出生日期',field:'birthday',width:'120',rowspan:2,align:'center'},
  71. {title:'班级',field:'className',width:'100',rowspan:2,align:'center'}
  72. ]],
  73. pagination:true,
  74. rownumbers:true
  75. });
  76.    /* $('#tt').datagrid('getPager').pagination({
  77. displayMsg:'当前显示从{from}到{to}共{total}记录',
  78. onBeforeRefresh:function(pageNumber, pageSize){
  79. $(this).pagination('loading');
  80. //alert('pageNumber:'+pageNumber+',pageSize:'+pageSize);
  81. $(this).pagination('loaded');
  82. },
  83. });
  84. */
  85. })();
  86. </script>
  87. </head>
  88. <body>
  89. <a href="javascript:vodi(0)" onclick="verify()" class="easyui-linkbutton">json</a>
  90. <h2><b>DataGrid</b></h2>
  91. <table id="tt"></table>
  92. <hr>
  93. <form action="">
  94. <input type="submit" value="提 交">
  95. </form>
  96. </body>
  97. </html>

说明:返回的json格式的数据格式:

{ "total": 2, "rows": [ { "userCode": "kf123456", "userName": "屌丝", "attorney": "屌丝", "addressee": "文哥哥", "userAddr": "大幅度舒服点", "tele2": "18987651525", "applyDate": "2013-06-27 09:11:38" }, { "userCode": "kf_123456", "userName": "白秋霜", "attorney": "白秋霜", "addressee": "文哥哥", "userAddr": "江湖难填恨海,剑出伤我心怀", "tele2": "18987651525", "applyDate": "2013-06-27 09:09:27" } ] }

web.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
  3. <display-name></display-name>
  4. <welcome-file-list>
  5. <welcome-file>index.jsp</welcome-file>
  6. </welcome-file-list>
  7. <filter>
  8. <filter-name>struts2</filter-name>
  9. <filter-class> org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
  10. </filter-class>
  11. </filter>
  12. <filter-mapping>
  13. <filter-name>struts2</filter-name>
  14. <url-pattern>*.action</url-pattern>
  15. </filter-mapping>
  16. </web-app>

strut2配置

<?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="dataGrid" extends="json-default"> <action name="studentAction" class="com.yjdgis.action.StudentAction" method="execute"> <result name="success" type="json"/> </action> </package> </struts>

Action:

package com.yjdgis.action;

  1. import java.util.List;
  2. import javax.servlet.http.HttpServletRequest;
  3. import org.apache.struts2.interceptor.ServletRequestAware;
  4. import com.opensymphony.xwork2.ActionSupport;
  5. import com.yjdgis.dao.StudentDao;
  6. import com.yjdgis.dao.impl.StudentDaoBean;
  7. public class StudentAction extends ActionSupport implements ServletRequestAware {
  8. private static final long serialVersionUID = 1L;
  9. private StudentDao studentDao;
  10. private HttpServletRequest request;
  11. private int total;
  12. private List<Object> rows;
  13. public StudentAction(){
  14. System.out.println("Action初始化了");
  15. }
  16. public int getTotal() {
  17. return total;
  18. }
  19. public void setTotal(int total) {
  20. this.total = total;
  21. }
  22. public List<Object> getRows() {
  23. return rows;
  24. }
  25. public void setRows(List<Object> rows) {
  26. this.rows = rows;
  27. }
  28. @Override
  29. public void setServletRequest(HttpServletRequest request) {
  30. this.request = request;
  31. }
  32. public String execute(){
  33. int page = Integer.parseInt(request.getParameter("page"));
  34. int row = Integer.parseInt(request.getParameter("rows"));
  35. studentDao=new StudentDaoBean();
  36. this.rows = studentDao.getStudent(page, row);
  37. this.total = studentDao.getTotalPages(row);
  38. System.out.println("总页数:"+this.total);
  39. return "success";
  40. }
  41. }

dao接口:

package com.yjdgis.dao;

  1. import java.util.List;
  2. public interface StudentDao {
  3. public List<Object> getStudent(int page,int rows);
  4. public int getTotalPages(int rows);
  5. }

 dao.impl(模拟了一个数据库)

package com.yjdgis.dao.impl;

  1. import java.util.ArrayList;
  2. import java.util.Date;
  3. import java.util.List;
  4. import com.yjdgis.dao.StudentDao;
  5. import com.yjdgis.domain.Student;
  6. public class StudentDaoBean implements StudentDao {
  7. private static List<Student> list ;
  8. static{
  9. //模拟数据<br> list = new ArrayList<Student>();
  10. for(int i=1;i<=110023;i++){
  11. Student st = new Student(i,i,"NAME"+i,new Date(),"ClassName"+i,i%2==0? '0':'1');
  12. list.add(st);
  13. }
  14. }
  15. @Override
  16. public List<Object> getStudent(int page, int rows) {
  17. System.out.println(page +" : "+ rows);
  18. List<Object> students = new ArrayList<Object>();
  19. for(int i=page*rows ;i<rows*(page+1); i++ ){
  20. students.add(list.get(i));
  21. }
  22. //获取数据<br> return students;
  23. }
  24. @Override
  25. public int getTotalPages(int rows) {
  26. int size = list.size();
  27. //int pages = size%rows==0? size/rows: size/rows+1;
  28. return size;
  29. //获取总的条数<br> }
  30. }

model实体类:

package com.yjdgis.domain;

  1. import java.util.Date;
  2. public class Student {
  3. private Integer id;
  4. private Integer age;
  5. private String name;
  6. private Date birthday;
  7. private String className;
  8. private char sex;
  9. public Student(){ }
  10. public Student(Integer id, Integer age, String name, Date birthday, String className, char sex) {
  11. this.id = id;
  12. this.age = age;
  13. this.name = name;
  14. this.birthday = birthday;
  15. this.className = className;
  16. this.sex = sex;
  17. }
  18. public Integer getId() {
  19. return id;
  20. }
  21. public void setId(Integer id) {
  22. this.id = id;
  23. }
  24. public Integer getAge() {
  25. return age;
  26. }
  27. public void setAge(Integer age) {
  28. this.age = age;
  29. }
  30. public String getName() {
  31. return name;
  32. }
  33. public void setName(String name) {
  34. this.name = name;
  35. }
  36. public Date getBirthday() {
  37. return birthday;
  38. }
  39. public void setBirthday(Date birthday) {
  40. this.birthday = birthday;
  41. }
  42. public String getClassName() {
  43. return className;
  44. }
  45. public void setClassName(String className) {
  46. this.className = className;
  47. }
  48. public char getSex() {
  49. return sex;
  50. }
  51. public void setSex(char sex) {
  52. this.sex = sex;
  53. }
  54. }
以上内容是否对您有帮助:
在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号