Posted on 2009-06-10 12:26
啥都写点 阅读(867)
评论(0) 编辑 收藏 所属分类:
J2EE
今天写了一个分页程序,是使用struts2+Spring完成的,现提供完整的源代码。如果看完后,还有不明白之处大家可以给我发邮件:
chenyanhubusiness@gmail.com
在这里对struts2 和 Spring的基本配置我就不多说了,在这个小程序中数据源我是使用Spring提供的c3po的数据源,并且查询数据库使用的是Jdbc模板,现提供
数据源和Jdbc模板以及相关配置。
<!--引入属性配置文件-->
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="/WEB-INF/jdbc.properties"/>
</bean>
<!-- C3P0 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="${jdbc.driverClassName}"></property>
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="user">
<value>${jdbc.username}</value>
</property>
<property name="password">
<value>${jdbc.password}</value>
</property>
<property name="minPoolSize" value="5"></property>
<property name="maxPoolSize" value="20"></property>
<property name="acquireIncrement" value="5"></property>
</bean>
<!-- JDBC模板 -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" >
<ref bean="dataSource" />
</property>
<property name="lazyInit" value="true"></property>
</bean>
<!-- Dao -->
<bean id="dao" abstract="true">
<property name="jdbcTemplate" ref="jdbcTemplate"/>
</bean>
<bean id="userDao" parent="dao" class="ryan.chapter8.UserDao"/>
<!-- 注入Action -->
<bean id="user" scope="prototype" class="ryan.chapter8.UserAction">
<property name="uds" ref="userDao"></property>
</bean>
我将相关的jdbc配置写在了jdbc.properties里面,大家可以根据自己机器的情况自行修改:
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/spring
jdbc.username=root
jdbc.password=123456
来看看index.jsp页面
<s:form action="pagination_user" method="post">
<s:submit value="查看列表"/>
</s:form>
这里是struts.xml配置:
<struts>
<!-- Struts2 in spring conformity -->
<constant name="struts.objectFactory" value="spring" />
<package name="Action" extends="struts-default">
<action name="*_user" method="{1}" class="user">
<result name="success">/index.jsp</result>
<result name="page">/pagination.jsp</result>
</action>
</package>
</struts>
接下来看看UserDaoService 和 UserDao。
public interface UserDaoService {
public void insertUser() ;
public List<User> pageUser(int pageNow,int pageSize);
public int pageCount(int pageSize);
}
import java.util.ArrayList;
import java.util.List;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
public class UserDao extends JdbcDaoSupport implements UserDaoService{
List<User> list = new ArrayList<User>();
int pageCount = 0;
public void insertUser() {
String sql = "insert into user(username,password)values('ryan','chen')";
getJdbcTemplate().execute(sql);
}
public List<User> pageUser(int pageNow,int pageSize) {
String limitF = String.valueOf((pageNow-1)*pageSize);
System.out.println("limitF = " +limitF);
String limitA = String.valueOf(pageSize);
System.out.println("limitA = " +limitA);
String sql2 = "select * from user limit"+" "+limitF+","+limitA ;
System.out.println("sql2 = "+sql2);
list = getJdbcTemplate().queryForList(sql2);
return list;
}
/**
* 分页
*/
public int pageCount(int pageSize) {
String sql = "select count(*) from user";
int rowCount = getJdbcTemplate().queryForInt(sql);
if(rowCount%pageSize==0){
pageCount = rowCount/pageSize;
}else{
pageCount = rowCount/pageSize+1;
}
return pageCount;
}
}
POJO:
package ryan.chapter8;
public class User {
private int id ;
private String username;
private String password;
省略 getter和setter......
}
下面是UserAction:
package ryan.chapter8;
import java.util.ArrayList;
import java.util.List;
import org.springframework.dao.DataAccessException;
import com.opensymphony.xwork2.ActionSupport;
public class UserAction extends ActionSupport{
UserDaoService uds;
int pageCount = 0;
int pageSize = 5;
int listSize;
List<User> list = new ArrayList<User>();
int pageNow = 1; //希望显示第几页
public List<User> getList() {
return list;
}
public void setList(List<User> list) {
this.list = list;
}
public int getPageNow() {
return pageNow;
}
public void setPageNow(int pageNow) {
this.pageNow = pageNow;
}
public UserDaoService getUds() {
return uds;
}
public void setUds(UserDaoService uds) {
this.uds = uds;
}
public String create(){
try {
uds.insertUser();
}catch (DataAccessException dae) {
// TODO: handle exception
dae.printStackTrace();
System.out.println("数据访问异常!!");
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
System.out.println("出现异常了!");
}
return SUCCESS;
}
//分页
public String pagination() {
pageCount = uds.pageCount(pageSize);
list = uds.pageUser(pageNow,pageSize);
if(list.size()==0){
listSize = 1;
addActionError("数据库中没有值");
}
return "page";
}
@Override
public String execute() throws Exception {
return null;
}
public int getPageCount() {
return pageCount;
}
public void setPageCount(int pageCount) {
this.pageCount = pageCount;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getListSize() {
return listSize;
}
public void setListSize(int listSize) {
this.listSize = listSize;
}
}
接下来是分页的页面 pagination.jsp
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'pagination.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<s:actionerror/>
<table>
<tr>
<td>序号</td>
<td>姓名</td>
<td>密码</td>
</tr>
<s:iterator value="list" id="li">
<tr>
<td><s:property value="#li.id"/></td>
<td><s:property value="#li.username"/></td>
<td><s:property value="#li.password"/></td>
</tr>
</s:iterator>
</table>
<!-- =================================分页================================ -->
<s:if test="listSize!=1">
<s:url action="pagination_user" id="homePage">
<s:param name="pageNow">1</s:param>
</s:url>
<s:a href="%{homePage}">首页</s:a>
<!-- 这是上一页的链接 -->
<s:url action="pagination_user" id="previousPage" >
<s:param name="pageNow">${pageNow-1}</s:param>
</s:url>
<s:if test="pageNow!=1">
<s:a href="%{previousPage}">上一页</s:a>
</s:if>
<s:bean name="org.apache.struts2.util.Counter" id="counter">
<s:param name="first">${pageNow}</s:param>
<s:param name="last">${pageNow+2}</s:param>
<s:iterator>
<s:if test=" (pageNow!=pageCount)&&(pageNow = pageNow+1)">
<s:url action="pagination_user" id="currentPage">
<s:param name="pageNow">${pageNow}</s:param>
</s:url>
<s:a href="%{currentPage}">${pageNow}</s:a>
</s:if>
</s:iterator>
</s:bean>
<!-- 下一页 -->
<s:url action="pagination_user" id="nextPage">
<s:param name="pageNow">${pageNow+1}</s:param>
</s:url>
<s:if test="pageNow!=pageCount">
<s:a href="%{nextPage}">下一页</s:a>
</s:if>
<!-- 尾页 -->
<s:url action="pagination_user" id="endPage">
<s:param name="pageNow">${pageCount}</s:param>
</s:url>
<s:a href="%{endPage}">尾页</s:a>
</s:if>
</body>
</html>
-- 学海无涯