spring+struts2+hibernate+ligerui+json整合开发(带分页)

说明:

1、后台程序是参考本公司“杜师傅”的例子!加上自学了ligerui,整合来使用!

2、最近在做一个项目,使用到了上面的技术,不知道现在ssh发展到了上面地步,是不是已经快淘汰了,发现springMVC+mybatis、hibernate+esayui、juqeryui等是企业级应用比较广泛,本人在大学期间,就是学习ssh整合开发,个人感觉现在ssh在java企业应用中还是普遍存在的吧!

3、重在个人总结,技术积累,有什么不好的地方,还请各位牛人指点!相互学习、相互进步。

 

话不多说,下面是整合信息

一、项目结构:

spring+struts2+hibernate+ligerui+json整合开发(带分页)_第1张图片

、把需要的jar包添加到lib目录,配置web.xml、applicationContext.xml




	org.springframework.web.context.ContextLoader


    Struts Blank


    struts2
    org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter


    struts2
    /*




    CharacterEncodingFilter
    org.springframework.web.filter.CharacterEncodingFilter
    
      encoding
      UTF-8
    
    
      forceEncoding
      true
    


    CharacterEncodingFilter
    /*

    
        index.jsp
    





	



	


	
		
		
		
		
		
	


	
	




	



	

DAO


 

 

package com.dao.impl;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.Session;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.common.PageUnit;
import com.dao.UserDao;
import com.tool.DBEncoding;

public class UserDaoImpl extends HibernateDaoSupport implements UserDao {

	public List userList(Map data,int pageNow,int pageSize) throws Exception{
		
		Session session=this.getHibernateTemplate().getSessionFactory().getCurrentSession();
		String sql = "select " +
						"t.ID," +
						"t.SFZH," +
						"t.YHXM," +
						"t.YHXB," +
						"decode(t.YHXB,'0','男','1','女','') as STR_YHXB," +
						"t.PHONE," +
						"t.PASSWORD," +
						"t.CSRQ," +
						"t.UPDATENUMBER," +
						"to_char(t.ADDDATE,'yyyy-MM-dd hh24:mi:ss') as ADDDATE," +
						"t.YXBS " +
						" from USERS t " +
						" where 1=1 ";
						//" and t.YXBS=0";
		String sql_where="";
		List dataList=new ArrayList();
		if(data!=null&&data.size()>0)
		{
			if(data.get("SFZH")!=null)
			{
				sql_where+=" and t.SFZH like ? ";
				dataList.add("%"+data.get("SFZH")+"%");
			}
			if(data.get("YHXM")!=null)
			{
				sql_where+=" and t.YHXM like ? ";
				dataList.add("%"+data.get("YHXM")+"%");
			}
		}
		sql+=sql_where+"order by t.ADDDATE desc";
		String sql_pageHear="select * from ( select zt.*,rownum m from ( ";
		String sql_pageFoot=" ) zt where rownum<=? ) where m>?";
		String sql_all=sql_pageHear+sql+sql_pageFoot;
		PreparedStatement ps=session.connection().prepareStatement(sql_all);
		int index=1;
		if(dataList!=null&&dataList.size()>0)
		{
			for(int i=0;i

dao中的分页类

package com.common;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.List;

import org.hibernate.Session;

import com.tool.DBEncoding;

public class PageUnit{
	public static int getPageCount(Session session,String sql, List data, int pageSize)
			throws Exception {
		int pageCount=0;
		int rowCount=0;
		sql = "select count(*) from (" +sql+")";
		PreparedStatement ps=session.connection().prepareStatement(sql);
		int index=1;
		if(data!=null&&data.size()>0)
		{
			for(int i=0;i data) throws Exception{
		//int pageCount=0;
		int rowCount=0;
		sql = "select count(*) from (" +sql+")";
		PreparedStatement ps=session.connection().prepareStatement(sql);
		int index=1;
		if(data!=null&&data.size()>0)
		{
			for(int i=0;i

service类

package com.service;

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

import com.dao.UserDao;

public class UserService {
	private UserDao userdao;
	public UserDao getUserdao() {
		return userdao;
	}

	public void setUserdao(UserDao userdao) {
		this.userdao = userdao;
	}

	/**
	 * 带分页的查询用户信息
	 * @param data
	 * @param pageNow
	 * @param pageSize
	 * @return
	 */
	public List userList(Map data,int pageNow,int pageSize){
		List list = new ArrayList();
		try {
			list = userdao.userList(data, pageNow, pageSize);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
	}

}

这时候 应该写个测试类来测试一下结果

package com.test;

import java.util.List;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.factory.SpringContextFactory;
import com.service.UserService;

public class UserDaoTest {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		ClassPathXmlApplicationContext act=SpringContextFactory.getSpringContext();
		UserService us = (UserService)act.getBean("UserService");
		List list = us.userList(null, 1, 10);
		for(int i=0;i

action 类

package com.action;

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

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.factory.SpringContextFactory;
import com.opensymphony.xwork2.ActionSupport;
import com.service.UserService;

public class UserAction extends ActionSupport{
	private int pageNow = 1;
	private int pageSize = 10;
	private JSONObject Rows;
	private String action;
	
	@SuppressWarnings("unchecked")
	public String execute(){
		//HttpServletRequest request=ServletActionContext.getRequest();
		ClassPathXmlApplicationContext act=SpringContextFactory.getSpringContext();
		UserService us = (UserService)act.getBean("UserService");
		if(action.equals("ulist")){
			List userList = us.userList(null, pageNow, pageSize);
			/**
			 * json 
			 */
			HashMap maprow = new HashMap();
			if(userList!=null&&userList.size()>0){
				String Total=(((Map)userList.get(0)).get("Total"))+"";  
				maprow.put("Rows", userList);
				maprow.put("Total", Total);
			}
			Rows =JSONObject.parseObject(JSON.toJSONString(maprow));
			System.out.println(Rows);
			return SUCCESS;
		}
		return ERROR;
	}
	
	public int getPageNow() {
		return pageNow;
	}

	public void setPageNow(int pageNow) {
		this.pageNow = pageNow;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	public JSONObject getRows() {
		return Rows;
	}
	public void setRows(JSONObject rows) {
		Rows = rows;
	}

	public String getAction() {
		return action;
	}

	public void setAction(String action) {
		this.action = action;
	}
	
}
struts.xml文件





	
		
			
				Rows
			
		
	

下面 来使用ligerui的ligerGrid查询显示列表数据,userlist.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>




用户列表
<%@include file="include/head.jsp" %>



	

显示结果
spring+struts2+hibernate+ligerui+json整合开发(带分页)_第2张图片

你可能感兴趣的:(SSH)