struts2 零配置代码
2016-02-16 21:10
483 查看
package com.ss.hria.action;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.dispatcher.mapper.ActionMapping;
import com.opensymphony.xwork2.ActionSupport;
import com.ss.hria.util.StringUtil;
public class BaseAction extends ActionSupport {
public BaseAction() {
}
private Long bizId;
/**
* 附件的大小,默认限制为8M
*/
protected static int fileSize = 8192;
/**
* defalut current year
*/
protected static int yearAim = Calendar.getInstance().get(Calendar.YEAR);;
/**
* year bound,+-10
*/
protected static int yearBound = 5;
private List<String> yearList;
private List<String> monthList;
public List<String> getYearList() {
if (yearList == null) {
yearList = new ArrayList<String>();
}
for (int year = yearAim - yearBound; year < yearAim + yearBound; year++) {
yearList.add("" + year);
}
return yearList;
}
public void setYearList(List<String> yearList) {
this.yearList = yearList;
}
public List<String> getMonthList() {
if (monthList == null) {
monthList = new ArrayList<String>();
}
for (int j = 1; j < 13; j++) {
if (j < 10) {
monthList.add("0" + String.valueOf(j));
} else {
monthList.add(String.valueOf(j));
}
}
return monthList;
}
public void setMonthList(List<String> monthList) {
this.monthList = monthList;
}
/**
* Save the message in the session, appending if messages already exist
*
* @param msg
* the message to put in the session
*/
@SuppressWarnings("unchecked")
protected void saveMessage(String msg) {
List messages = (List) getRequest().getSession().getAttribute("messages");
if (messages == null) {
messages = new ArrayList();
}
messages.add(msg);
getRequest().getSession().setAttribute("messages", messages);
}
/**
* Convenience method to get the Configuration HashMap from the servlet
* context.
*
* @return the user's populated form from the session
*/
@SuppressWarnings("unchecked")
protected Map getConfiguration() {
Map config = (HashMap) getSession().getServletContext().getAttribute("appConfig");
// so unit tests don't puke when nothing's been set
if (config == null) {
return new HashMap();
}
return config;
}
/**
* Convenience method to get the request
*
* @return current request
*/
protected HttpServletRequest getRequest() {
return ServletActionContext.getRequest();
}
/**
* Convenience method to get the response
*
* @return current response
*/
protected HttpServletResponse getResponse() {
return ServletActionContext.getResponse();
}
/**
* Convenience method to get the session. This will create a session if one
* doesn't exist.
*
* @return the session from the request (request.getSession()).
*/
protected HttpSession getSession() {
return getRequest().getSession();
}
public static final String CUSTOM = "custom";
private String forwardUrl;
public String getForwardUrl() {
return forwardUrl;
}
public void setForwardUrl(String forwardUrl) {
this.forwardUrl = forwardUrl;
}
public String customMethod(String method) {
return custom(null, method, null);
}
public String custom() {
return custom(null, null, null);
}
public String custom(String prefix, String method, String suffix) {
String msuffix = "";
if (StringUtil.isRealEmpty(prefix)) {
prefix = "/WEB-INF/content/";
}
if (StringUtil.isRealEmpty(suffix)) {
suffix = ".jsp";
}
if (!suffix.startsWith(".")) {
suffix = "." + suffix;
}
ActionMapping actionMapping = ServletActionContext.getActionMapping();
if (StringUtil.isRealEmpty(method)) {
method = actionMapping.getMethod();
}
if ("add".equals(method) || "save".equals(method) || "load".equals(method) || "edit".equals(method)
|| "update".equals(method)) {
method = "form";
} else if ("view".equals(method)) {
method = "view";
} else if
c514
("list".equals(method)) {
method = "list";
} else if ("find".equals(method)) {
method = "find";
} else if ("search".equals(method)) {
method = "search";
} else if ("query".equals(method)) {
method = "query";
}
String tempForwardUrl = StringUtil.connectURL(prefix, actionMapping.getNamespace(), actionMapping.getName(),
method + suffix);
return custom(tempForwardUrl);
}
@Override
public String input() {
return customMethod("add");
}
public String custom(String forwardUrl) {
return _custom(0, forwardUrl);
}
public String customRedirect(String forwardUrl) {
return _custom(2, forwardUrl);
}
public String customRedirectAction(String forwardUrl) {
return _custom(3, forwardUrl);
}
private String _custom(int type, String forwardUrl) {
setForwardUrl(forwardUrl);
return CUSTOM + (type < 1 ? "" : type);
}
public Long getBizId() {
return bizId;
}
public void setBizId(Long bizId) {
this.bizId = bizId;
}
}
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.dispatcher.mapper.ActionMapping;
import com.opensymphony.xwork2.ActionSupport;
import com.ss.hria.util.StringUtil;
public class BaseAction extends ActionSupport {
public BaseAction() {
}
private Long bizId;
/**
* 附件的大小,默认限制为8M
*/
protected static int fileSize = 8192;
/**
* defalut current year
*/
protected static int yearAim = Calendar.getInstance().get(Calendar.YEAR);;
/**
* year bound,+-10
*/
protected static int yearBound = 5;
private List<String> yearList;
private List<String> monthList;
public List<String> getYearList() {
if (yearList == null) {
yearList = new ArrayList<String>();
}
for (int year = yearAim - yearBound; year < yearAim + yearBound; year++) {
yearList.add("" + year);
}
return yearList;
}
public void setYearList(List<String> yearList) {
this.yearList = yearList;
}
public List<String> getMonthList() {
if (monthList == null) {
monthList = new ArrayList<String>();
}
for (int j = 1; j < 13; j++) {
if (j < 10) {
monthList.add("0" + String.valueOf(j));
} else {
monthList.add(String.valueOf(j));
}
}
return monthList;
}
public void setMonthList(List<String> monthList) {
this.monthList = monthList;
}
/**
* Save the message in the session, appending if messages already exist
*
* @param msg
* the message to put in the session
*/
@SuppressWarnings("unchecked")
protected void saveMessage(String msg) {
List messages = (List) getRequest().getSession().getAttribute("messages");
if (messages == null) {
messages = new ArrayList();
}
messages.add(msg);
getRequest().getSession().setAttribute("messages", messages);
}
/**
* Convenience method to get the Configuration HashMap from the servlet
* context.
*
* @return the user's populated form from the session
*/
@SuppressWarnings("unchecked")
protected Map getConfiguration() {
Map config = (HashMap) getSession().getServletContext().getAttribute("appConfig");
// so unit tests don't puke when nothing's been set
if (config == null) {
return new HashMap();
}
return config;
}
/**
* Convenience method to get the request
*
* @return current request
*/
protected HttpServletRequest getRequest() {
return ServletActionContext.getRequest();
}
/**
* Convenience method to get the response
*
* @return current response
*/
protected HttpServletResponse getResponse() {
return ServletActionContext.getResponse();
}
/**
* Convenience method to get the session. This will create a session if one
* doesn't exist.
*
* @return the session from the request (request.getSession()).
*/
protected HttpSession getSession() {
return getRequest().getSession();
}
public static final String CUSTOM = "custom";
private String forwardUrl;
public String getForwardUrl() {
return forwardUrl;
}
public void setForwardUrl(String forwardUrl) {
this.forwardUrl = forwardUrl;
}
public String customMethod(String method) {
return custom(null, method, null);
}
public String custom() {
return custom(null, null, null);
}
public String custom(String prefix, String method, String suffix) {
String msuffix = "";
if (StringUtil.isRealEmpty(prefix)) {
prefix = "/WEB-INF/content/";
}
if (StringUtil.isRealEmpty(suffix)) {
suffix = ".jsp";
}
if (!suffix.startsWith(".")) {
suffix = "." + suffix;
}
ActionMapping actionMapping = ServletActionContext.getActionMapping();
if (StringUtil.isRealEmpty(method)) {
method = actionMapping.getMethod();
}
if ("add".equals(method) || "save".equals(method) || "load".equals(method) || "edit".equals(method)
|| "update".equals(method)) {
method = "form";
} else if ("view".equals(method)) {
method = "view";
} else if
c514
("list".equals(method)) {
method = "list";
} else if ("find".equals(method)) {
method = "find";
} else if ("search".equals(method)) {
method = "search";
} else if ("query".equals(method)) {
method = "query";
}
String tempForwardUrl = StringUtil.connectURL(prefix, actionMapping.getNamespace(), actionMapping.getName(),
method + suffix);
return custom(tempForwardUrl);
}
@Override
public String input() {
return customMethod("add");
}
public String custom(String forwardUrl) {
return _custom(0, forwardUrl);
}
public String customRedirect(String forwardUrl) {
return _custom(2, forwardUrl);
}
public String customRedirectAction(String forwardUrl) {
return _custom(3, forwardUrl);
}
private String _custom(int type, String forwardUrl) {
setForwardUrl(forwardUrl);
return CUSTOM + (type < 1 ? "" : type);
}
public Long getBizId() {
return bizId;
}
public void setBizId(Long bizId) {
this.bizId = bizId;
}
}
相关文章推荐
- Java基本类型和取值范围
- (转)JAVA中堆和栈的区别
- ssh(struts2.3.8+spring3.2+heibernate4.1+annotation零配置
- Java基础01 从HelloWorld到面向对象
- JVM调优-Java中的对象
- Struts2验证
- java中的分代垃圾回收策略
- Java读写.ini文件
- myeclipse安装破解教程
- Java 并发读取List
- 【Java-IO】PipedInputStream和PipedOutputStream
- 【转】推荐一款Java反编译器,比较好用
- 随笔笔记三——关于SpringMVC接收请求参数和向页面传递参数
- Java实现Mysql数据库自动备份
- 使用JNI实现Java与C之间传递数据
- Realm Java 简明教程(1)
- java栈--后进先出(顺序栈、链栈、单词逆序)
- JavaBean是什么
- Maven在Eclipse中的实用小技巧
- java 设计模式之六大原则