Struts 2 对 Ajax 分页怎么



preface

Struts 2.1 的基于Dojo的Ajax标签。

1.部署是struts2-dojo-plugin-2.1.x.x。jar

2.在需要使用Ajax变标签的JSP页面中导入Ajax标签

<%@taglib prefix = "sx" uri="/struts-dojo-tags"%>

3.在需要使用Ajax变标签的JSP页面中导入head标签,在页面上导入了Dojo所需要的是的CSS库和JavaScript库

 flow path

 1.通过显示首页租房信息;

 2.通过 标签建立多个页号的连接

 3.分页租房信息的遍历显示在result 页面中,在div 中返回结果。

 4.分页租房信息的遍历显在result 页面中,在div中返回结果。

 5.单击具体页号的超链接异步显示指定页面的租房信息到div

How:

分页:首页页码

 <%@ page language="java" contentType="text/html;charset=UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%@ taglib prefix="sx" uri="/struts-dojo-tags"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
int total = (Integer)request.getAttribute("total");
%>




	
	
	青鸟租房 - 用户管理
	
	
	



                        
${house.title}
${house.street.district.name}区${house.street.name},${house.floorage }平米
联系方式:${house.contact }
${house.title}
${house.street.district.name}区${house.street.name},${house.floorage }平米
联系方式:${house.contact }
/
2.result.jsp
<%@ page language="java" contentType="text/html;charset=UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%@ taglib prefix="sx" uri="/struts-dojo-tags"%>



  
    房屋信息列表    
	
	
	    
	
	
	
    
  
		
	
		
		
		
    	
${house.title}
${house.street.district.name}区${house.street.name},${house.floorage }平米
联系方式*****:${house.contact }
${house.title}
${house.street.district.name}区${house.street.name},${house.floorage }平米
联系方式:${house.contact }
/

2.manageAtion

package action;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;

import dao.IHouseDao;
import dao.impl.HouseDaoImpl;
import entity.House;

public class ManageAction  {
	
	private static final long serialVersionUID = 1L;
	private int number = 0; //要显示第几页
	private int total = 0; //总页数
	private int prev = 0; //上一页
	private int next = 0; //下一页
	private int[] numbers;
	private static final int RECORD_COUNT = 3; //每页显示3条数据
	public int getNumber() {
		return number;
	}
	public void setNumber(int number) {
		this.number = number;
	}
	public int getTotal() {
		return total;
	}
	public void setTotal(int total) {
		this.total = total;
	}
	public int getPrev() {
		return prev;
	}
	public void setPrev(int prev) {
		this.prev = prev;
	}
	public int getNext() {
		return next;
	}
	public void setNext(int next) {
		this.next = next;
	}
	public int[] getNumbers() {
		return numbers;
	}
	public void setNumbers(int[] numbers) {
		this.numbers = numbers;
	}
	public static long getSerialversionuid() {
		return serialVersionUID;
	}
	public static int getRecordCount() {
		return RECORD_COUNT;
	}
	
	@SuppressWarnings("unchecked")
	public String list(){
		
		int start =  0;	//页面的初始的位置	
		IHouseDao houseDao  = new HouseDaoImpl();
		int count =houseDao.getCount(null);//获得总的条数
		System.out.println("-------count---------"+count);
		//后的总的页数
		total = count%RECORD_COUNT==0?(count/RECORD_COUNT):(count/RECORD_COUNT+1);
		/**
		 * 页面处理
		 */
		if(number <= 0){
			number =1;
			prev = 1;
			next= number+1;
		}else if(number > total){
			prev = number-1;
			next = total;
		}else{
			prev = number-1;
			prev = number+1;
		}
		
		start = number;
		List ll =  houseDao.FenPageShowHouse(start,RECORD_COUNT, null);
		 HttpSession session=	ServletActionContext.getRequest().getSession();
		ActionContext ac = ActionContext.getContext();
          
		Map request = (Map) ac.get("request");
		request.put("result", ll);
		
		session.setAttribute("result", ll);
		request.put("number", number);
		request.put("total", total);
		numbers = new int[total];
	   for(int i = 0 ; i < total ; i++){
		      numbers[i]= i+1;
	   }
	   
		return "list";
	}
	
	

	@SuppressWarnings("unchecked")
	public String ajaxList() throws Exception{
		 HttpSession session=	ServletActionContext.getRequest().getSession();
	
		 ActionContext ac = ActionContext.getContext();
         
		Map request = (Map) ac.get("request");
		 
		if(number == 0){
			number = 1;
		}
		int start = 0; //从第几条开始
		
		start = (number - 1) * RECORD_COUNT;
		
		IHouseDao houseDao  = new HouseDaoImpl();
		List ll =  houseDao.FenPageShowHouse(start,RECORD_COUNT, null);
		session.setAttribute("result", ll);
		int count =houseDao.getCount(null);//获得总的条数
		//后的总的页数
		total = count%RECORD_COUNT==0?(count/RECORD_COUNT):(count/RECORD_COUNT+1);
		if(number <= 1){
			prev = 1;
			next = number + 1;
		}else if(number >= total){
			prev = number - 1;
			next = total;
		}else{
			prev = number - 1;
			next = number + 1;
		}
		
		System.out.println("---number---"+number);
		numbers = new int[total];
		   for(int i = 0 ; i < total ; i++){
			      numbers[i]= i+1;
		   }
		return "ajaxlist";
	}
	
	
	

}

3.struts.xml





	
	

    
		
		   
    	
        
        	/page/success.jsp
        	/page/register.jsp
        	manage!list.action
        	/page/login_struts2.jsp        	
         
        
        	/page/manage.jsp
        	/page/result.jsp
        
                       
    

3.部署运行项目。







你可能感兴趣的:(Struts2,Ajax)