BaseAction

来源:互联网 发布:get什么意思网络用语 编辑:程序博客网 时间:2024/05/17 02:28

一个项目中可能会有很多action都需要用到session,如果每个action都来实现 org.apache.struts2.interceptor.SessionAware这个接口,可能会显得比较麻烦,所以建议作一个抽象的 BaseAction类来实现org.apache.struts2.interceptor.SessionAware接口,以后所有的action只要继承这个BaseAction就可以了。

BaseAction一般用于统一的过程包装,例如异常处理,记录登录用户操作日志实现的模式一般使用模板方法。action类基础,继承自ActionSupport,用户实现自己的action的时候继承,提供 Map<String, Object> session, 提供 HttpServletResponse response, 提供 HttpServletRequest request等

/**
* 响应文本信息,utf-8编码
* @param json 文本信息,多为JSON
*/
public void responseText(String json){
response.setContentType("text/html;charset=utf-8");
PrintWriter out = null;
try {
out = response.getWriter();
} catch (IOException e) {
e.printStackTrace();
}
out.print(json);
out.flush();
}
public void responseXml(String json){
response.setContentType("text/plain;charset=utf-8");
PrintWriter out = null;
try {
out = response.getWriter();
} catch (IOException e) {
e.printStackTrace();
}
out.print(json);
out.flush();
}

public void setSession(Map<String, Object> arg0) {
this.session = arg0;
}

涉及到session要谨慎,一旦session过期将会产生很多无法预料的问题。baseAction只适合解决一些横切方面的问题

0 0
原创粉丝点击