SSM 中 BaseController 使用 session

转载: http://blog.sina.com.cn/s/blog_7085382f0102v9jg.html

 

public class BaseController {


	//region HttpRequest Response Session
	protected HttpServletRequest request;
	protected HttpServletResponse response;
	protected HttpSession session;
	protected SysUser currentUser;

	@ModelAttribute
	public void setReqAndRes(HttpServletRequest request, HttpServletResponse response){
		this.request = request;
		this.response = response;
		this.session = request.getSession();
this.currentUser=(SysUser)this.session.getAttribute("userMessage");
	}

	//endregion

	//region Paged
	protected static final String CURRENTPAGE = "1";
	protected static final Integer PAGE_SIZE = 2;

	protected Integer getTotalPage(Integer pageSize, Integer total) {
		return total / pageSize == 0 ? total / pageSize : total / pageSize + 1;
	}

	protected PageResult getPageResult(HttpServletRequest request, Integer currentPage, Page page,
			List list) {
		Integer total = Integer.valueOf(String.valueOf(page.getTotal()));
    	Integer totalPage =getTotalPage(PAGE_SIZE,total);
    	PageResult pageResult = new PageResult(currentPage, PAGE_SIZE, list, total, totalPage,request.getQueryString());
		return pageResult;
	}
	//endregion
    
	
}

 

 

说明:@ModelAttribute的作用


1)放置在方法的形参上:表示引用Model中的数据

2)放置在方法上面:表示请求该类的每个Action前都会首先执行它,也可以将一些准备数据的操作放置在该方法里面。

 

posted @ 2017-05-27 14:31  正怒月神  阅读(998)  评论(0编辑  收藏  举报