BaseAction
import?javax.servlet.http.HttpServletRequest;
import?javax.servlet.http.HttpServletResponse;
import?javax.servlet.http.HttpSession;
import?org.apache.struts2.ServletActionContext;
import?com.alibaba.fastjson.JSONObject;
import?com.opensymphony.xwork2.ActionContext;
import?com.opensymphony.xwork2.ActionSupport;
import?com.opensymphony.xwork2.util.ValueStack;
public?class?BaseAction?extends?ActionSupport?{
private?static?final?long?serialVersionUID?=?9007242859196909496L;
protected?static?final?String?FAILURE="failure";
/**
*?獲得ValueStack
*?@return
*/
public?ValueStack?getStack(){
return?ServletActionContext.getValueStack(getRequest());
}
/**
*?獲得request
*/
public?HttpServletRequest?getRequest()?{
return?ServletActionContext.getRequest();
}
/**
*?獲得response
*/
public?HttpServletResponse?getResponse()?{
return?ServletActionContext.getResponse();
}
/**
*?獲得session
*/
public?HttpSession?getSession()?{
return?getRequest().getSession();
}
/**
*?獲得servlet上下文
*/
public?ServletContext?getServletContext()?{
return?ServletActionContext.getServletContext();
}
/**
*?獲得action上下文
*/
public?ActionContext?getContext(){
return?ActionContext.getContext();
}
/**
*?向上下文中put值
*?@param?key
*?@param?value
*/
public?void?putContext(String?key,Object?value){
getContext().put(key,?value);
}
/**
*?獲得資源路徑
*/
public?String?getRealPath(String?path)?{
return?getServletContext().getRealPath(path);
}
/**
*?session?中存儲對象
*?@param?key
*?@param?value
*/
public?void?setAttribute(String?key,Object?value){
getSession().setAttribute(key,?value);
}
/**
*?獲得session中的對象
*?@param?param
*?@return
*/
public?Object?getAttribute(String?key){
return?getSession().getAttribute(key);
}
/**
*?獲得request里面的參數值
*?@param?arg0
*?@return
*/
public?String?getParameter(String?arg0){
return?getRequest().getParameter(arg0);
}
/**
*?獲得數組對象參數
*?@param?arg0
*?@return
*/
public?String[]?getParameterValues(String?arg0){
return?getRequest().getParameterValues(arg0);
}
/**
*?獲得int型參數
*?@param?arg0
*?@return
*/
public?int?getIntParameter(String?arg0){
String?p?=?getParameter(arg0);
try{
return?Integer.parseInt(p);
}catch(NumberFormatException?e){
return?-1;
}
}
public?long?getLongParameter(String?arg0){
String?p?=?getParameter(arg0);
try{
return?Long.parseLong(p);
}catch(NumberFormatException?e){
return?-1;
}
}
/**
*?獲得Boolean型參數
*?@param?args0
*?@return
*/
public?boolean?getBoolParameter(String?args0){
String?params?=?getParameter(args0);
if(?params!=null&&
("yes".equalsIgnoreCase(params)||
"1".equalsIgnoreCase(params)||
"true".equalsIgnoreCase(params)||
"do".equalsIgnoreCase(params)
)
){
return?true;
}
return?false;
}
public?String?execResult(JSONObject?json){
addActionMessage(json.getString("message"));
String?result?=?json.getString("result");
if(result==null?||?result.length()?==?0?){
return?SUCCESS;
}else{
return?result;
}
}
}
總結
以上是生活随笔為你收集整理的BaseAction的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java 根据当前时间获得一周日期
- 下一篇: OGNL使用