Struts2.1.6+Spring2.5.6+Hibernate3.3.2+mysql整合+分页模板(3)
十、写测试类测试Spring和Hibernate是否结合成功,测试方法是否正确UserServiceTest .java
package ssh.test;import java.util.LinkedHashMap;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import ssh.model.User;import ssh.service.UserService;import ssh.utils.QueryResult;public class UserServiceTest {//测试获取保存@Testpublic void save(){ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");UserService service = (UserService)ctx.getBean("userService");User user=new User();user.setName("uuuuuuuuuuuuuu");service.save(user);}//测试获取分页@Testpublic void getScrollData(){ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");UserService service = (UserService)ctx.getBean("userService");LinkedHashMap<String, String> orderby = new LinkedHashMap<String, String>();//定义排序orderby.put("id", "desc");QueryResult<User> qr =service.getScrollData(1, 5, "o.id>?", new Object[]{3}, orderby);for (User user : qr.getResultlist()) {System.out.println("id:" + user.getId() + " name:" + user.getName());}}}
十一、封装分页页面
1)封装分页工具条页面
调用方法:
<form action="index" method="post">
<%@ include file="/share/fenye.jsp" %>
</form>
fenye.jsp
<%@ page language="java" contentType="text/html; charset=gb2312" pageEncoding="gb2312"%><%@ include file="/share/taglib.jsp" %><html><SCRIPT type="text/javascript">function topage(page){var form=document.forms;var currentPage=document.getElementById(currentPage);form.currentPage.value=page;form.submit();}</SCRIPT><body><input type="hidden" name="pageView.currentPage" id="currentPage" value="10"/>当前页:第${pageView.currentPage} | 总记录数:${pageView.totalRecord} | 每页显示:${pageView.maxResult}|总页数:${pageView.totalPage}|<c:if test="${pageView.currentPage==1}">首页|上一页</c:if><c:if test="${pageView.currentPage!=1}"><a href="javascript:topage(1)">首页</a>|<a href="javascript:topage('${pageView.currentPage-1}')">上一页</a></c:if>|第<c:forEach begin="${pageView.startIndex}" end="${pageView.endIndex}" var="i"><c:if test="${pageView.currentPage==i}"><b>${i }</b></c:if><c:if test="${pageView.currentPage!=i}"><a href="javascript:topage('${i}')">${i}</a></c:if></c:forEach>页|<c:if test="${pageView.currentPage==pageView.totalPage}">下一页|末页</c:if><c:if test="${pageView.currentPage!=pageView.totalPage}"><a href="javascript:topage('${pageView.currentPage+1}')">下一页</a>|<a href="javascript:topage('${pageView.totalPage}')">末页</a></c:if></body></html>
2)定义头不集合页面,每个JSP页面直接包含就行<%@ include file="/share/taglib.jsp" %>
taglib.jsp
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%><%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%><%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%><%@ taglib uri="/struts-tags" prefix="s" %>
十二、规划Struts 的Action和展示页面
1)Action 加@Component("ua") /@Scope("prototype")/@Resource ,所以在Struts.xml里面写
<action name="index" class="ssh.action.UserAction">
2)Action 没有加@Component("ua") /@Scope("prototype")/@Resource ,所以在Struts.xml里面写
<action name="index" class="ua">
UserAction.java
package ssh.action;import java.util.LinkedHashMap;import javax.annotation.Resource;import ssh.utils.QueryResult;import ssh.utils.PageView;import org.springframework.stereotype.Component;import ssh.base.BaseAction;import ssh.model.User;import ssh.service.UserService;//@Component("ua")//@Scope("prototype")public class UserAction extends BaseAction {private User user;private UserService userService;private PageView<User>pageView=new PageView<User>();//这里必须要构造新对象,不然刚打开没有currentPage参数传递过来,如果不新建也行,第一次打开必须传递currentPage参数过来public PageView<User> getPageView() {return pageView;}public void setPageView(PageView<User> pageView) {this.pageView = pageView;}public UserService getUserService() {return userService;}//@Resourcepublic void setUserService(UserService userService) {this.userService = userService;}public User getUser() {return user;}public void setUser(User user) {this.user = user;}@Overridepublic String execute() throws Exception {int maxresult=5;//设置每次显示条数int firstindex=(pageView.getCurrentPage()-1)*maxresult;//定义分页开始索引LinkedHashMap<String, String> orderby = new LinkedHashMap<String, String>();//定义排序orderby.put("id", "desc");QueryResult<User> qr=userService.getScrollData(firstindex,maxresult, "o.id>?", new Object[]{3}, orderby);pageView.setQueryResult(maxresult,qr);//把查询结果和每页显示数传递给pageViewrequest.put("pageView", pageView);return "index";//System.out.println("当前页"+pageView.getCurrentPage());//System.out.println("总页数"+pageView.getTotalPage());//System.out.println("总条数"+pageView.getTotalRecord());//System.out.println("startindex:"+pageView.getStartIndex());//System.out.println("endindex1:"+pageView.getEndIndex());//System.out.println("firstIndex:"+firstindex);//System.out.println("maxResult:"+pageView.getMaxResult());//System.out.println("currentPage:"+pageView.getCurrentPage());}public String save() throws Exception {userService.save(user);return "save";}}
效果图
http://dl.iteye.com/upload/picture/pic/81989/bb402282-1d63-385b-b8fd-00460bc837ad.jpg
源文件:
附件
页:
[1]