1、新建一个Web Project,命名为S2SH,并将项目编码改为UTF-8。
2、拷贝以下jar包到lib文件夹里面:
antlr-2.7.6.jar
asm.jar
cglib-2.1.3.jar
commons-collections.jar
commons-fileupload-1.2.1.jar
commons-logging-1.0.4.jar
dom4j-1.6.1.jar
freemarker-2.3.13.jar
hibernate3.jar
jstl.jar
jta.jar
log4j-1.2.11.jar
mysql-connector-java.jar
ognl-2.6.11.jar
spring.jar
standard.jar
struts2-core-2.1.6.jar
struts2-spring-plugin-2.1.6.jar
xwork-2.1.2.jar
3、修改web.xml文件,如下:
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <!-- 指定spring配置文件 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext*.xml</param-value> </context-param> <!-- 对Spring容器进行实例化 --> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> <!-- 编码过滤器(要放在struts2前面配置) --> <filter> <filter-name>encodeFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodeFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- struts2 配置 --> <filter> <filter-name>struts2</filter-name> <filter-class> org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter </filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
4、hibernate.cfg.xml,如下:
<?xml version='1.0' encoding='UTF-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="connection.username">root</property> <property name="connection.password">123456</property> <property name="connection.url"> jdbc:mysql://localhost:3306/test </property> <property name="dialect"> org.hibernate.dialect.MySQLDialect </property> <property name="myeclipse.connection.profile"> com.mysql.jdbc.Driver </property> <property name="connection.driver_class"> com.mysql.jdbc.Driver </property> <property name="show_sql">true</property> <property name="format_sql">true</property> <mapping resource="orm/TbUser.hbm.xml" /> </session-factory> </hibernate-configuration>
5、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.5.xsd"> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="configLocation" value="classpath:hibernate.cfg.xml" /> </bean> <bean id="userDAO" class="dao.UserDAOImpl"/> <bean id="userService" class="service.UserServiceImpl"> <property name="userDAO"> <ref local="userDAO"/> </property> </bean> <bean id="loginAction" class="action.UserAction"> <property name="userService"> <ref local="userService"/> </property> </bean> </beans>
6、struts.xml,如下:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <!-- 资源文件 --> <constant name="struts.custom.i18n.resources" value="native"/> <!-- Spring负责创建Action实例 --> <constant name="struts.objectFactory" value="spring"/> <package name="user" namespace="/user" extends="struts-default"> <!-- action配置的class属性值和spring中bean的名称相同 --> <action name="*" class="loginAction" method="{1}"> <result name="success">/index.jsp</result> <result name="input">/login.jsp</result> <result name="browse">/index.jsp</result> </action> </package> </struts>
7、分页需要用到的类Page
import java.util.List;
public class Page {
private int total;
private int pageSize;
private int totalPage;
private int pageNo;
private int prePage;
private int nextPage;
private List list;
public List getList() {
return list;
}
public void setList(List list) {
this.list = list;
}
public int getNextPage() {
if(pageNo==totalPage){
nextPage=pageNo;
}else{
nextPage=pageNo+1;
}
return nextPage;
}
public void setNextPage(int nextPage) {
this.nextPage = nextPage;
}
public int getPageNo() {
return pageNo;
}
public void setPageNo(int pageNo) {
this.pageNo = pageNo;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getPrePage() {
if(pageNo==1){
prePage=pageNo;
}else{
prePage=pageNo-1;
}
return prePage;
}
public void setPrePage(int prePage) {
this.prePage = prePage;
}
public int getTotal() {
return total;
}
public void setTotal(int total) {
this.total = total;
}
public int getTotalPage() {
if(total%pageSize==0){
totalPage=total/pageSize;
}else{
totalPage=total/pageSize+1;
}
return totalPage;
}
public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
}
}
8、Hibernate分页语句
public Page findByPageNo(int pageNo, int pageSize) {
Page page = new Page();
Query query = session.createQuery("select count(*) from TbUser");
int count = Integer.parseInt(query.uniqueResult().toString());
query = session.createQuery("from TbUser");
query.setMaxResults(pageSize);
query.setFirstResult((pageNo - 1) * pageSize);
page.setList(query.list());
page.setTotal(count);
page.setPageNo(pageNo);
page.setPageSize(pageSize);
return page;
}
9、JSP页面分页代码
共${page.totalPage }页 当前第${page.pageNo }页 共${page.total }条记录
<c:if test="${page.totalPage!=1}">
<c:if test="${page.pageNo==1}">
<font color="gray">首页 上一页</font>
<a href="user/browse?pageNo=${page.nextPage }">下一页</a>
<a href="user/browse?pageNo=${page.totalPage }">尾页</a>
</c:if>
<c:if test="${page.pageNo==page.totalPage}">
<a href="user/browse?pageNo=1">首页</a>
<a href="user/browse?pageNo=${page.prePage }">上一页</a>
<font color="gray">下一页 尾页</font>
</c:if>
<c:if test="${page.pageNo!=1 and page.pageNo!=page.totalPage}">
<a href="user/browse?pageNo=1">首页</a>
<a href="user/browse?pageNo=${page.prePage }">上一页</a>
<a href="user/browse?pageNo=${page.nextPage }">下一页</a>
<a href="user/browse?pageNo=${page.totalPage }">尾页</a>
</c:if>
</c:if>
注意:附件含代码