您的位置:首页 > 编程语言 > Java开发

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;
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: