BaseAction

struts的action需要继承BaseAction 方便编写action,记录一下:

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;

}

}


}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值