1.web层BaseAction:
package cn.itcast.web.action;
import java.lang.reflect.ParameterizedType;
import java.util.Map;
import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.interceptor.ApplicationAware;
import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.interceptor.SessionAware;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
//通过RequestAware, SessionAware, ApplicationAware实行接口获得request,session,application对象,action中就可直接调用
/**
*
*/
@Controller
@ParentPackage("default")
@Scope("prototype")
public class BaseAction<T> extends ActionSupport implements RequestAware, SessionAware, ApplicationAware{
private static Logger log = Logger.getLogger(BaseAction.class);
private static final long serialVersionUID = 1L;
protected Map<String, Object> request;
protected Map<String, Object> session;
protected Map<String, Object> application;
public Map<String, Object> getRequest() {
return request;
}
public Map<String, Object> getSession() {
return session;
}
public Map<String, Object> getApplication() {
return application;
}
@Override
public void setRequest(Map<String, Object> request) {
this.request = request;
}
@Override
public void setSession(Map<String, Object> session) {
this.session = session;
}
@Override
public void setApplication(Map<String, Object> application) {
this.application = application;
}
/**
*
* 将一个对象放入栈顶
*/
public void push(Object obj){
ActionContext.getContext().getValueStack().push(obj);
}
/**
* 放集合放入值栈中 context区域
*/
public void put(String key,Object value){
ActionContext.getContext().put(key, value);
}
/**
* 取出当前登录的用户对象
*/
/*public User getCurUser(){
User user = (User)session.get(SysConstant.CURRENT_USER_INFO);
return user;
}*/
}
2.新建DeptAtion继承BaseAction,目的:和struts2框架实现解耦和
并使用模型驱动
package cn.itcast.web.action.sysadmin;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import com.opensymphony.xwork2.ModelDriven;
import cn.itcast.domain.Dept;
import cn.itcast.service.DeptService;
import cn.itcast.utils.Page;
import cn.itcast.web.action.BaseAction;
@Namespace("/sysadmin")
public class DeptAction extends BaseAction implements ModelDriven<Dept>{
@Autowired
private DeptService deptService;
private Dept model = new Dept();
@Override
public Dept getModel() {
// TODO Auto-generated method stub
return model;
}
private Page page = new Page();
public Page getPage() {
return page;
}
public void setPage(Page page) {
this.page = page;
}
@Action(value="deptAction_list",results={@Result(name="list",location="/WEB-INF/pages/sysadmin/dept/jDeptList.jsp")})
public String list() throws Exception {
// TODO Auto-generated method stub
org.springframework.data.domain.Page<Dept> page2 = deptService.findPage(null, new PageRequest(page.getPageNo() - 1, page.getPageSize()));
//4个条件必写
page.setTotalPage(page2.getTotalPages()); //总页数
page.setTotalRecord(page2.getTotalElements()); //总记录数
page.setResults(page2.getContent()); //查询的记录
page.setUrl("deptAction_list"); //上下页跳转的地址(相对路径地址)
//可以在jsp中直接取
super.push(page);
return "list";
}
}
3.jsp中主要实现
<c:forEach items="${results }" var="dept" varStatus="st">
<tr class="odd" onmouseover="this.className='highlight'" onmouseout="this.className='odd'" >
<td><input type="checkbox" name="id" value="${dept.id }"/></td>
<td>${st.count }</td>
<td>${dept.id }</td>
<td>${dept.parent.deptName }</td>
<td><a href="deptAction_toview?id=${dept.id }">${dept.deptName }</a></td>
</tr>
</c:forEach>
可以看出这里
4.utils工具类 page.java:
package cn.itcast.utils;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* 分页辅助类:对分页的基本数据进行一个简单的封装
* 用来传递分页参数和查询参数params
*/
public class Page<T> {
private int pageNo = 1; //页码,默认是第一页
private int pageSize = SysConstant.PAGE_SIZE; //每页显示的记录数,默认是10
private long totalRecord; //总记录数
private int totalPage; //总页数
private List<T> results; //对应的当前页记录
private Map<String, Object> params = new HashMap<String, Object>(); //其他的参数我们把它分装成一个Map对象
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 long getTotalRecord() {
return totalRecord;
}
public void setTotalRecord(long totalRecord) {
this.totalRecord = totalRecord;
//在设置总页数的时候计算出对应的总页数,在下面的三目运算中加法拥有更高的优先级,所以最后可以不加括号。
int totalPage = ((Long)totalRecord).intValue()%pageSize==0 ? ((Long)totalRecord).intValue()/pageSize : ((Long)totalRecord).intValue()/pageSize + 1;
this.setTotalPage(totalPage);
}
public int getTotalPage() {
return totalPage;
}
public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
}
public List<T> getResults() {
return results;
}
public void setResults(List<T> results) {
this.results = results;
}
public Map<String, Object> getParams() {
return params;
}
public void setParams(Map<String, Object> params) {
this.params = params;
}
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("Page [pageNo=").append(pageNo).append(", pageSize=").append(pageSize).append(", results=").append(results).append(", totalPage=").append(totalPage).append(", totalRecord=").append(totalRecord).append("]");
return builder.toString();
}
/* 页面链接 */
public String url; //分页按钮中的转向链接
public void setUrl(String url) {
this.url = url;
}
public String links;
public String getLinks() {
StringBuffer sBuf = new StringBuffer();
int curPageNo = this.pageNo; //当前页
sBuf.append("<span class=\"noprint\" style=\"padding:5px;\">");
//利用js动态设置分页页码
sBuf.append("<script language=\"javascript\">");
sBuf.append(" function setPageNo( value ){");
sBuf.append(" document.getElementById(\"page.pageNo\").value = value;");
//sBuf.append(" alert(document.getElementById(\"pageNo\").value);");
sBuf.append(" }");
sBuf.append("</script>");
sBuf.append("<input type=\"hidden\" id=\"page.pageNo\" name=\"page.pageNo\" value=\"").append(curPageNo).append("\">"); //分页参数:当前页隐藏域
sBuf.append(" 第").append(curPageNo).append("页 / 共").append(this.totalPage).append("页 ");
sBuf.append(" 总共").append(this.totalRecord).append("条记录 每页").append(this.pageSize).append("条记录 ");
sBuf.append("<a href=\"#").append("\" οnclick=\"setPageNo(1);formSubmit('").append(url).append("','_self')");
sBuf.append("\">[首页]");
sBuf.append("</a> ");
if(this.pageNo<=1){
curPageNo = 1;
}else{
curPageNo = this.pageNo - 1;
}
sBuf.append("<a href=\"#").append("\" οnclick=\"setPageNo(").append(curPageNo).append(");formSubmit('").append(url).append("','_self')");
sBuf.append("\">[上一页]");
sBuf.append("</a> ");
if(this.pageNo>=this.totalPage){
curPageNo = this.totalPage;
}else{
curPageNo = this.pageNo + 1;
}
sBuf.append("<a href=\"#").append("\" οnclick=\"setPageNo(").append(curPageNo).append(");formSubmit('").append(url).append("','_self')");
sBuf.append("\">[下一页]");
sBuf.append("</a> ");
sBuf.append("<a href=\"#").append("\" οnclick=\"setPageNo(").append(this.totalPage).append(");formSubmit('").append(url).append("','_self')");
sBuf.append("\">[末页]");
sBuf.append("</a> ");
sBuf.append("</span>");
System.out.println(sBuf.toString());
return sBuf.toString();
}
}
${links }为:
结果为: