Struts2的容易示例action类
Struts2的简单示例action类
基类:
实现基本业务类:
基类:
package com.kit.common; import java.io.Serializable; import java.util.HashMap; import java.util.Map; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.struts2.ServletActionContext; import org.apache.struts2.interceptor.ServletRequestAware; import org.apache.struts2.interceptor.ServletResponseAware; import com.kit.utils.Config; import com.kit.utils.JsonUtil; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; @SuppressWarnings({"serial","unchecked","rawtypes"}) public abstract class BaseAction extends ActionSupport implements ServletRequestAware, ServletResponseAware, Serializable{ public Boolean isSearch;//是否查询 public String filterType;//查询业务类型 public QueryParameter queryParameter;//封装查询参数 public QueryResult queryResult;//查询结果 public Long rows = SysConstant.ADMIN_PAGE_COUNT;//分页(每页显示记录数10) public Long page = 1L;//页码 public String sortName;//jquery 表格排序字段 public String sortOrder;//jquery 表格排序方式 public HttpServletRequest request; public HttpServletResponse response; public String returnPageURL; public String returnParamater; public String resultJSON; public void beforeAnsy(){ ServletActionContext.getResponse().setCharacterEncoding("UTF-8"); ServletActionContext.getResponse().setHeader("Cache-Control","no-cache"); ServletActionContext.getResponse().setHeader("Pragma","no-cache"); ServletActionContext.getResponse().setDateHeader("Expires",0); } public final Log log = LogFactory.getLog(getActionClassFullName()); /** * 去指定的URL地址 * @return */ public String goToUrl(){ return "dispatcher"; } /** * 实体列表初始化 * @return */ protected String initQueryList(boolean isValidFilter) { if(null!=isSearch && isSearch.booleanValue()){//点击查询 Context.setQueryParameter(getActionClassFullName(), queryParameter); }else if(null!=isSearch && !isSearch.booleanValue()){//非查询 queryParameter = Context.getQueryParameter(getActionClassFullName()); if(null!=queryParameter){ Context.removeQueryParameter(getActionClassFullName()); } queryParameter = new QueryParameter(); }else{//查询分页 queryParameter = Context.getQueryParameter(getActionClassFullName());//获取查询条件 if(null == queryParameter){ queryParameter = new QueryParameter(); } } StringBuffer sb = new StringBuffer(" where "); sb.append(makeFilterString(queryParameter));//过滤HQL initQueryResult(rows);//初始化查询分页参数 if(isValidFilter){ if(sb.toString().trim().equals("where")){ sb.append(" valid='1'"); }else{ sb.append(" and valid='1'"); } }else{ if(sb.toString().trim().equals("where")) sb.append(" 1=1 "); } return sb.toString(); } /** * 初始化查询分页参数 */ protected void initQueryResult(Long queryResultCountType) { if(null==queryResult){ queryResult = new QueryResult(); } if(rows==0) queryResult.setMaxResult(queryResultCountType); else queryResult.setMaxResult(rows); queryResult.setCurrentPage(page); String currentRequest = request.getRequestURI(); queryResult.setUrl(currentRequest); } protected void retrunMsg(boolean sucess,Map retMsg){ if(retMsg==null)retMsg = new HashMap(); retMsg.put("state", sucess); String retJSON = ""; if(sucess){ retMsg.put("resultMsg", "操作成功!"); retJSON = JsonUtil.getJSONString(retMsg); }else{ retMsg.put("resultMsg", "操作异常!请检查并重试或联系管理员。"); retJSON = JsonUtil.getJSONString(retMsg); } JsonUtil.toJSONHtmlOut(retJSON); } protected void retrunMsg(boolean sucess){ Map retMsg = new HashMap(); retMsg.put("state", sucess); String retJSON = ""; if(sucess){ retMsg.put("resultMsg", "操作成功!"); retJSON = JsonUtil.getJSONString(retMsg); }else{ retMsg.put("resultMsg", "操作异常!请检查并重试或联系管理员。"); retJSON = JsonUtil.getJSONString(retMsg); } JsonUtil.toJSONHtmlOut(retJSON); } protected void retrunMsg(boolean state,String retMassage){ Map retMsg = new HashMap(); retMsg.put("state", state); retMsg.put("resultMsg", retMassage); String retJSON = JsonUtil.getJSONString(retMsg); JsonUtil.toJSONHtmlOut(retJSON); } public Map<String, Object> getSession() { return ActionContext.getContext().getSession(); } public Config Cfg() { return Config.getInstance(); } public QueryParameter getQueryParameter() { return queryParameter; } public void setQueryParameter(QueryParameter queryParameter) { this.queryParameter = queryParameter; } public Boolean getIsSearch() { return isSearch; } public void setIsSearch(Boolean isSearch) { this.isSearch = isSearch; } public void setServletResponse(HttpServletResponse response) { this.response = response; } public void setServletRequest(HttpServletRequest request) { this.request = request; } public QueryResult getQueryResult() { return queryResult; } public void setQueryResult(QueryResult queryResult) { this.queryResult = queryResult; } public String getReturnPageURL() { return returnPageURL; } public Long getRows() { return rows; } public void setRows(Long rows) { this.rows = rows; } public Long getPage() { return page; } public void setPage(Long page) { this.page = page; } public void setReturnPageURL(String returnPageURL) { this.returnPageURL = returnPageURL; } public String getReturnParamater() { return returnParamater; } public void setReturnParamater(String returnParamater) { this.returnParamater = returnParamater; } public String getSortName() { return sortName; } public void setSortName(String sortName) { this.sortName = sortName; } public String getSortOrder() { return sortOrder; } public void setSortOrder(String sortOrder) { this.sortOrder = sortOrder; } public String getFilterType() { return filterType; } public void setFilterType(String filterType) { this.filterType = filterType; } }
实现基本业务类:
package com.forveross.coas.web.action; import java.util.ArrayList; import java.util.Date; import java.util.List; import javax.annotation.Resource; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Controller; import com.forveross.plane.service.TaskInfoService; import com.forveross.plane.vo.TaskInfo; import com.kit.common.BaseAction; import com.kit.common.QueryParameter; @SuppressWarnings("serial") @Controller("taskInfoAction") @Scope("prototype") public class TaskInfoAction extends BaseAction { @Resource(name = "taskInfoServiceImpl") private TaskInfoService taskInfoService; private int pageNum = 1; private int pageSize = 15; private int totalCount; private int pageCount; private int cpage = 1;// 当前页 private List<Integer> pageList; private String mes;// 提示信息 private String act; private String taskId; private TaskInfo taskInfo; private Date grabTime; public Date getGrabTime() { return grabTime; } public void setGrabTime(Date grabTime) { this.grabTime = grabTime; } private List<TaskInfo> taskList; public TaskInfoService getTaskInfoService() { return taskInfoService; } public void setTaskInfoService(TaskInfoService taskInfoService) { this.taskInfoService = taskInfoService; } public int getPageNum() { return pageNum; } public void setPageNum(int pageNum) { this.pageNum = pageNum; } public int getPageSize() { return pageSize; } public void setPageSize(int pageSize) { this.pageSize = pageSize; } public int getTotalCount() { return totalCount; } public void setTotalCount(int totalCount) { this.totalCount = totalCount; } public int getPageCount() { return pageCount; } public void setPageCount(int pageCount) { this.pageCount = pageCount; } public int getCpage() { return cpage; } public void setCpage(int cpage) { this.cpage = cpage; } public String getMes() { return mes; } public void setMes(String mes) { this.mes = mes; } public List<Integer> getPageList() { return pageList; } public void setPageList(List<Integer> pageList) { this.pageList = pageList; } public String getAct() { return act; } public void setAct(String act) { this.act = act; } public TaskInfo getTaskInfo() { return taskInfo; } public String getTaskId() { return taskId; } public void setTaskId(String taskId) { this.taskId = taskId; } public void setTaskInfo(TaskInfo taskInfo) { this.taskInfo = taskInfo; } public List<TaskInfo> getTaskList() { return taskList; } public void setTaskList(List<TaskInfo> taskList) { this.taskList = taskList; } /** * 初始化分页信息 */ public void initPage() { cpage = pageNum; totalCount = taskInfoService.countTaskInfo(); pageList = new ArrayList<Integer>(); if (totalCount % pageSize != 0) { pageCount = totalCount / pageSize + 1; } else { pageCount = totalCount / pageSize; } for (int i = 1; i <= pageCount; i++) { pageList.add(i); } } /** * 添加航空公司 * * @return * @throws Exception */ public String addTaskInfo() throws Exception { String urlStr = ""; if (taskId != null && !taskId.trim().equals("")) {// 修改 taskInfo = taskInfoService.findTaskInfo(Long.parseLong(taskId)); urlStr = "WEB-INF/jsp/pg/flight/task_update.jsp"; } else {// 添加 urlStr = "WEB-INF/jsp/pg/flight/task_add.jsp"; } returnPageURL = urlStr; return "dispatcher"; } /** * 添加或修改任务 * * @return * @throws Exception */ public String editTaskInfo() throws Exception { String urlStr = ""; if (act != null && act.equals("update")) {// 修改 taskInfo.setNextGrabTime(this.grabTime); taskInfoService.saveOrUpdateTaskInfo(taskInfo); this.mes = "修改任务成功!"; initPage(); taskList = taskInfoService.findTaskInfoList(pageNum, pageSize); urlStr = "WEB-INF/jsp/pg/flight/task_setting.jsp"; } else {// 添加 taskInfo.setNextGrabTime(this.grabTime); taskInfoService.saveOrUpdateTaskInfo(taskInfo); setTaskInfo(null); urlStr = "WEB-INF/jsp/pg/flight/task_add.jsp"; } returnPageURL = urlStr; return "dispatcher"; } /** * 删除任务 * * @return * @throws Exception */ public String deleteTaskInfo() throws Exception { if (taskId != null && !taskId.trim().equals("")) { taskInfoService.deleteTaskInfo(Long.parseLong(taskId)); this.mes = "删除任务成功!"; } initPage(); taskList = taskInfoService.findTaskInfoList(pageNum, pageSize); returnPageURL = "WEB-INF/jsp/pg/flight/task_setting.jsp"; return "dispatcher"; } /** * 查询显示任务列表 * * @return * @throws Exception */ public String showTaskInfoList() throws Exception { initPage(); taskList = taskInfoService.findTaskInfoList(pageNum, pageSize); returnPageURL = "WEB-INF/jsp/pg/flight/task_setting.jsp"; return "dispatcher"; } }