练习自己搭建框架(1)BaseAction

来源:互联网 发布:网上银行系统繁忙淘宝 编辑:程序博客网 时间:2024/06/17 13:43

刚来工作不久,看见别人自己搭建的框架实现crud那么容易,而自己还在写servlet心中实在不忍,于是发奋图强,自己学着来,今天分析下人家的BaseAction,源码如下:


public class BaseAction {
 
 public static final int PAGE_SIZE = 15;//每页数据条数
 protected int currentPage;//当前页
 
 protected int id;//很关键的

 public int getId() {
  return id;
 }

 public void setId(int id) {
  this.id = id;
 }

 protected String ids;//删除多条数据时用到
 
 protected String errorMsg;//后台返回的错误信息
 
 public String getIds() {
  return ids;
 }

 public void setIds(String ids) {
  this.ids = ids;
  if(ids!=null && ids.length()>1){
   this.ids = ids.substring(0, ids.length()-1);//不知道什么意思,求解释
  }
 }

 public String getErrorMsg() {
  return errorMsg;
 }

 public void setErrorMsg(String errorMsg) {
  this.errorMsg = errorMsg;
 }

 public int getCurrentPage() {
  if(currentPage<1){
   currentPage = 1;
  }
  return currentPage;
 }

 public void setCurrentPage(int currentPage) {
  this.currentPage = currentPage;
  if(currentPage<1){
   currentPage = 1;
  }
 }

 protected HttpServletRequest getRequest(){
  return ServletActionContext.getRequest();
 }
 
 protected HttpServletResponse getResponse(){
  return ServletActionContext.getResponse();
 }
 
 protected HttpSession getSession(){
  return ServletActionContext.getRequest().getSession();
 }
 
 //我不知道为什么把action里面的参数要放在application里面,求解释?
 protected void put(String key, Object value){
  ActionContext.getContext().put(key, value);
 }
 
 protected String getParameter(String key){
  return ServletActionContext.getRequest().getParameter(key);
 }
 

//这个方法不错
 protected User getLoginUser(){
  return (User) ServletActionContext.getRequest().getSession().getAttribute(Globals.LOGIN_USER_KEY);
 }
 
 protected Department getLoginUserDepartment(){
  return (Department) ServletActionContext.getRequest().getSession().getAttribute(Globals.LOGIN_USER_DEPARTMENT_KEY);
 }
 
 protected Position getLoginUserPosition(){
  return (Position) ServletActionContext.getRequest().getSession().getAttribute(Globals.LOGIN_USER_POSITION_KEY);
 }
 

//不知道干嘛的?
 public Map<String, Object> fitParam(String name,Object value){
  Map<String, Object> parameters = new HashMap<String, Object>();
  parameters.put(name, value);
  return parameters;
 }
 
 public Map<String, Object> getMap(){
  return new HashMap<String, Object>();
 }
 

//这个方法也用的很多
 public String getRealPath(String path){
  return ServletActionContext.getRequest().getSession().getServletContext().getRealPath(path);
 }
 
 
 public boolean isLogin(){
  HttpSession session = ServletActionContext.getRequest().getSession();
  if(session.getAttribute(Globals.LOGIN_USER_KEY) != null){
   return true;
  } else {
   return false;
  }
 }
 
 public PrintWriter getWriter(){
  try {
   return ServletActionContext.getResponse().getWriter();
  } catch (IOException e) {
   return null;
  }
 }
 
 public void write(String str){
  try {
   ServletActionContext.getResponse().getWriter().write(str);
  } catch (IOException e) {
  }
 }
 
}

 

其实也挺简单的是吧,只是做的东西少很多地方想不到。下面看一下在其他继承自BaseAction的action中的相关写法,拿一个list()方法为例

 public String list() {
  if (currentPage < 1)
   currentPage = 1;
  Map<String,Object> parameters = getMap();
  parameters.put("department", getLoginUserDepartment());
  
  int count = techOwnDocTypeService.findCount(parameters);
  List<TechOwnDocType> list = techOwnDocTypeService.findPart(currentPage,
    PAGE_SIZE, "name", "ASC", parameters);
  put("pageView", new PageView<TechOwnDocType>(currentPage, PAGE_SIZE,
    count, list));
  return "list";
 }

下面来看看pageView类里面都是些什么?

public class PageView<T> {
 private int currentPage; // 

 private int pageSize; // 

 
 private int recordCount; // recordList 的大小

 private List<T> recordList; // 页面的list <c:forEach items="${pageView.recordList}" var="conduit">
 
 // ����
 private int pageCount; // ��ҳ��
 
 private int beginIndex;

 public int getBeginIndex() {
  return beginIndex;
 }

 public void setBeginIndex(int beginIndex) {
  this.beginIndex = beginIndex;
 }
 
 public PageView() {
  super();
 }

 @SuppressWarnings("unchecked")
 public PageView(int currentPage, int pageSize, int recordCount, List recordList) {
  this.currentPage = currentPage;
  this.pageSize = pageSize;
  this.recordCount = recordCount;//
  this.recordList = recordList;

  // ��ҳ��
  pageCount = (recordCount + pageSize - 1) / pageSize;
  beginIndex = (currentPage-1)*pageSize;

 }

 public int getCurrentPage() {
  if(currentPage < 1){
   currentPage = 1;
  }
  return currentPage;
 }

 public void setCurrentPage(int currentPage) {
  this.currentPage = currentPage;
 }

 public int getPageSize() {
  if(pageSize<1){
   pageSize=10;
  }
  return pageSize;
 }

 public void setPageSize(int pageSize) {
  this.pageSize = pageSize;
 }

 public int getRecordCount() {
  return recordCount;
 }

 public void setRecordCount(int recordCount) {
  this.recordCount = recordCount;
 }

 @SuppressWarnings("unchecked")
 public List getRecordList() {
  return recordList;
 }

 @SuppressWarnings("unchecked")
 public void setRecordList(List recordList) {
  this.recordList = recordList;
 }

 public int getPageCount() {
  return pageCount;
 }

 public void setPageCount(int pageCount) {
  this.pageCount = pageCount;
 }

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

原创粉丝点击