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

说明:
1、后台程序是参考本公司“杜师傅”的例子!加上自学了ligerui,整合来使用!
2、最近在做一个项目,使用到了上面的技术,不知道现在ssh发展到了上面地步,是不是已经快淘汰了,发现springMVC+mybatis、hibernate+esayui、juqeryui等是企业级应用比较广泛,本人在大学期间,就是学习ssh整合开发,个人感觉现在ssh在java企业应用中还是普遍存在的吧!
3、重在个人总结,技术积累,有什么不好的地方,还请各位牛人指点!相互学习、相互进步。

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

一、项目结构:

二、把需要的jar包添加到lib目录,配置web.xml、applicationContext.xml
[b]web.xml文件[/b]



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





[b]applicationContext.xml[/b]

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">

































[b]三、dao[/b]

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 {
ps.setString(index++, dataList.get(i));
}
}
ps.setInt(index++, pageNow*pageSize);
ps.setInt(index++, (pageNow-1)*pageSize);
System.out.println("executeSQL:"+sql_all);
ResultSet rs=ps.executeQuery();
List l=null;
try
{
if(rs!=null)
{
int pageCount=PageUnit.getPageCount(session,sql, dataList, pageSize);
int Total = PageUnit.getRowCount(session, sql, dataList);
l=new ArrayList();
while(rs.next())
{
Map m=new HashMap();
for(int i=0;i {
String key=rs.getMetaData().getColumnName(i+1);
m.put(key, DBEncoding.EncodingOutputDB(rs.getString(key)));
if("null".equals(m.get(key)+""))
{
m.put(key,"");
}
}
m.put("pageCount", pageCount);
m.put("Total", Total);
l.add(m);
}
}
releaseSession(session);
}
finally
{
if(rs!=null)
{
rs.close();
}
if(ps!=null)
{
ps.close();
}
}
return l;
}

}


[b] dao中的分页类[/b]

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 {
ps.setString(index++, DBEncoding.EncodingInputDB(data.get(i)+""));
}
}
System.out.println("executeSQL:"+sql);
ResultSet rs=ps.executeQuery();
try
{
if(rs!=null)
{
if(rs.next())
{
rowCount=Integer.parseInt(rs.getInt(1)+"");
}
}
if(rowCount%pageSize==0)
{
pageCount=rowCount/pageSize;
}
else
{
pageCount=rowCount/pageSize+1;
}
}
finally
{
if(rs!=null)
{
rs.close();
}
if(ps!=null)
{
ps.close();
}
}
return pageCount;
}
public static int getRowCount(Session session,String sql,List 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 {
ps.setString(index++, DBEncoding.EncodingInputDB(data.get(i)+""));
}
}
System.out.println("executeSQL:"+sql);
ResultSet rs=ps.executeQuery();
try
{
if(rs!=null)
{
if(rs.next())
{
rowCount=Integer.parseInt(rs.getInt(1)+"");
}
}
}
finally
{
if(rs!=null)
{
rs.close();
}
if(ps!=null)
{
ps.close();
}
}
return rowCount;
}
}


[b] service类[/b]

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;
}

}


[b] 这时候应该写个测试类来测试一下结果[/b]

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 System.out.println(list.get(i));
}

}

}
package com.factory;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SpringContextFactory {
private static ClassPathXmlApplicationContext act=null;

public static ClassPathXmlApplicationContext getSpringContext()
{
if(act==null)
{
act=new ClassPathXmlApplicationContext("applicationContext.xml");
}
return act;
}
}



[b] action 类[/b]

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;
}

}



[b] struts.xml文件[/b]


"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">





Rows






[b] 下面来使用ligerui的ligerGrid查询显示列表数据,userlist.jsp[/b]

<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>




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






显示结果
[img][/img]
只写了个查询的列子,后面的增、删、改都差不多啦!

你可能感兴趣的:(java,web)