YUI2.8 实现datatable从server端动态获取数据,并实现分页功能(上)

实现的功能:用YUI的datatable实现从服务器端获取数据,在前台页面展示,并实现分页的功能

使用的开发工具:jdk6+myeclipse6.6+tomcat6+yui2.8+struts2+spring+json

所需要的jar包: 13个

commons-beanutils-1.7.0.jar
 commons-collections-3.1.jar
 commons-httpclient-3.0.jar
 commons-lang-2.0.jar
 commons-logging-1.1.jar
 ezmorph-1.0.5.jar
 freemarker-2.3.8.jar
 json-lib-2.2.2-jdk15.jar
 ognl-2.6.11.jar
 spring.jar
 struts2-core-2.0.11.1.jar
 struts2-spring-plugin-2.0.14.jar
 xwork-2.0.4.jar

 

我的个人联系方式是QQ 604320137 手机是1343917 2635 或 133 9175 8739,加我请注明是javaeye的yui。

 

具体步骤如下:

1、新建工程yuiapp,导入所需要的jar和配置好tomcat,并能正常启动。

2、在yuiapp/WebRoot新建appjs/yuijs/build,具体的路径是/yuiapp/WebRoot/appjs/yuijs/build,然后把yui的build下的所有文件放进来。

3、配置web的内容,如下:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.5" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/applicationContext.xml</param-value>
  </context-param>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <filter>
   <filter-name>struts</filter-name>
   <filter-class>org.apache.struts2.dispatcher.FilterDispatcher
   </filter-class>
  </filter>
 
  <filter-mapping>
  <filter-name>struts</filter-name>
  <url-pattern>/*</url-pattern>
  </filter-mapping>
 
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

4、配置src下新建struts.xml,具体的路径是/yuiapp/src/struts.xml,具体的内容是

<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
        "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
 <include file="struts-default.xml"/>
 <constant name="struts.objectFactory" value="spring" />
 <package name="yuitab" namespace="/yuitab" extends="struts-default">
  <action name="do_*" method="{1}" class="com.demo.action.YuiTabAction">
  </action>
 </package>
</struts>

5、配置applicationContext.xml,具体的路径是/yuiapp/WebRoot/WEB-INF/applicationContext.xml,具体的内容是

<?xml version="1.0" encoding="UTF-8"?>
<beans
 xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
</beans>

 

其实就是一个空文件

 

6、编写Action和Bean类

6.1 YuiTabAction.java

package com.demo.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("all")
public class YuiTabAction extends ActionSupport{
 @Override
 public String execute() {
  System.out.println("............ execute() is called....");
  return SUCCESS;  
 }
 
 public void getBookList(){
  System.out.println(" getBookList is called....startIndex="+startIndex);
  List<Book> bookList = new ArrayList<Book>();
  recordsReturned = 10;
  for(int i=1; i<10; i++){
   Book book = new Book();
   book.setId(startIndex+i);
   book.setAmount(startIndex+i+10);
   book.setQuantity(startIndex+i+100);
   book.setTitle("title("+startIndex+i+")");
   bookList.add(book);
  }
  pageSize = 10;
  totalRecords = 20;//假设总记录数是20条,
  YUIPagination yuiPage = new YUIPagination();
  yuiPage.setRecordsReturned(recordsReturned);
  yuiPage.setTotalRecords(totalRecords);
  yuiPage.setStartIndex(startIndex);
  yuiPage.setPageSize(pageSize);
  yuiPage.setSort("ASC");
  yuiPage.setRecords(bookList);  
  this.outJson(yuiPage); 
 }
 
 public HttpServletRequest getRequest() {
  return ServletActionContext.getRequest();
 }
 public HttpServletResponse getResponse() {
  return ServletActionContext.getResponse();
 }

 public void outJsonString(String str) {
  getResponse().setContentType("text/javascript;charset=UTF-8");
  outString(str);
 }

 public void outJson(Object obj) {
  outJsonString(JSONObject.fromObject(obj).toString());
 }

 public void outJsonArray(Object array) {
  outJsonString(JSONArray.fromObject(array).toString());  
 }

 public void outString(String str) {
  try {
   PrintWriter out = getResponse().getWriter();
   out.write(str);
  } catch (IOException e) {
  }
 }

 public void outXMLString(String xmlStr) {
  getResponse().setContentType("application/xml;charset=UTF-8");
  outString(xmlStr);
 }
 
 protected int recordsReturned;
 protected int totalRecords;
 protected int startIndex;
 protected String sort;  
 protected String dir;
 protected List records;
 protected int pageSize;
 //getter and setter

}

6.2 YUIPagination.java

package com.demo.action;

import java.util.List;
@SuppressWarnings("all")
public class YUIPagination {
 private int recordsReturned;
 private int totalRecords;
 private int startIndex;
 private String sort;  
 private String dir;
 private List records;
 private int pageSize;

//getter and setter

}

 

6.3 Book.java

package com.demo.action;

public class Book {
 private Integer id;
 private Integer quantity;
 private Integer amount;
 private String title;

//getter and setter

}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值