- 浏览: 136738 次
- 性别:
- 来自: 深圳
文章分类
最新评论
-
西蜀石兰:
永远保持一颗好奇心,面对新鲜的事物时,乐意花代价去尝试一番,这 ...
优秀程序员必备的15大技能 -
liuwuhen:
代码还是不错的,头像确实有点
sprig AOP之ProxyFactory -
西蜀石兰:
这头像。。。。完全不用看代码了
sprig AOP之ProxyFactory -
coding1688:
...
你的知识资产 -
tao_gun:
...
你的知识资产
在编写SSH的项目中通常会获取request,response,sessinon对象或者需要需要从表单中获取参数值,如果项目中
包含上百个Action或者更多,那么我们就会编写许多类似的代码,可以通过编写一个通用的基类从而减少了重复的代码,提
高了编码的效率。
package com.wuhen.struts2.hello;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang.StringUtils;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.interceptor.SessionAware;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Preparable;
public class BaseAction extends ActionSupport implements ServletRequestAware,
ServletResponseAware, Preparable, SessionAware {
private static final long serialVersionUID = -4871815191368438694L;
protected HttpServletRequest request;
protected HttpServletResponse response;
protected PrintWriter printWriter;
protected ActionContext actionContext;
protected Map<String, Object> session;
@Override
public void prepare() throws Exception {
}
@Override
public void setServletResponse(HttpServletResponse response) {
this.response = response;
}
@Override
public void setServletRequest(HttpServletRequest request) {
this.request = request;
}
public PrintWriter getPrintWriter() throws IOException {
return response.getWriter();
}
protected void initUTF8() {
response.setContentType("text/html");
response.setCharacterEncoding("UTF-8");
}
@Override
public void setSession(Map<String, Object> session) {
this.session = session;
}
public Object getSession(String key) {
return session.get(key);
}
public void setSession(String name, Object value) {
session.put(name, value);
}
protected ActionContext getActionContext() {
return ActionContext.getContext();
}
protected Object getAttribute(String name) {
return request.getAttribute(name);
}
public void setAttribute(String name, Object value) {
request.setAttribute(name, value);
}
public String getParameter(String name) {
return request.getParameter(name);
}
public String[] getParameterValues(String name) {
return request.getParameterValues(name);
}
public String ajax(String content, String type) throws IOException {
if (StringUtils.isEmpty(type)) {
initUTF8();
} else {
response.setContentType(type + ";charset=UTF-8");
}
response.setHeader("Pragma", "No-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
getPrintWriter().write(content);
getPrintWriter().flush();
return null;
}
}
获取request,response,session都是通过实现对应的接口,在struts2中的ServletConfigInterceptor中拦截器中
进行了相关代码的封装。其部分源码如下:
/**
* Sets action properties based on the interfaces an action implements. Things like application properties,
* parameters, session attributes, etc are set based on the implementing interface.
*
* @param invocation an encapsulation of the action execution state.
* @throws Exception if an error occurs when setting action properties.
*/
public String intercept(ActionInvocation invocation) throws Exception {
final Object action = invocation.getAction();
final ActionContext context = invocation.getInvocationContext();
if (action instanceof ServletRequestAware) {
//从contex对象中获取com.opensymphony.xwork2.dispatcher.HttpServletRequest对象
HttpServletRequest request = (HttpServletRequest) context.get(HTTP_REQUEST);
//进行对象设置
((ServletRequestAware) action).setServletRequest(request);
}
if (action instanceof ServletResponseAware) {
HttpServletResponse response = (HttpServletResponse) context.get(HTTP_RESPONSE);
((ServletResponseAware) action).setServletResponse(response);
}
if (action instanceof ParameterAware) {
((ParameterAware) action).setParameters((Map)context.getParameters());
}
if (action instanceof ApplicationAware) {
((ApplicationAware) action).setApplication(context.getApplication());
}
if (action instanceof SessionAware) {
((SessionAware) action).setSession(context.getSession());
}
if (action instanceof RequestAware) {
((RequestAware) action).setRequest((Map) context.get("request"));
}
if (action instanceof PrincipalAware) {
HttpServletRequest request = (HttpServletRequest) context.get(HTTP_REQUEST);
if(request != null) {
// We are in servtlet environment, so principal information resides in HttpServletRequest
((PrincipalAware) action).setPrincipalProxy(new ServletPrincipalProxy(request));
}
}
if (action instanceof ServletContextAware) {
ServletContext servletContext = (ServletContext) context.get(SERVLET_CONTEXT);
((ServletContextAware) action).setServletContext(servletContext);
}
return invocation.invoke();
}
对于这段代码是从context对象中获取的
(HttpServletRequest) context.get(HTTP_REQUEST);
那么struts2是从何时将这些对象设置到context对象中的呢?
我们可以从struts2中的StrutsPrepareAndExecuteFilter中的接口入口进行分析
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) res;
try {
prepare.setEncodingAndLocale(request, response);
//设置相关的request和respon对象
prepare.createActionContext(request, response);
prepare.assignDispatcherToThread();
if ( excludedPatterns != null && prepare.isUrlExcluded(request, excludedPatterns)) {
chain.doFilter(request, response);
} else {
request = prepare.wrapRequest(request);
ActionMapping mapping = prepare.findActionMapping(request, response, true);
if (mapping == null) {
boolean handled = execute.executeStaticResourceRequest(request, response);
if (!handled) {
chain.doFilter(request, response);
}
} else {
execute.executeAction(request, response, mapping);
}
}
} finally {
prepare.cleanupRequest(request);
}
}
从上述的代码可知,在 prepare.createActionContext(request, response);对request,response对象进行了设置
该方法的源码如下:
public ActionContext createActionContext(HttpServletRequest request, HttpServletResponse response) {
ActionContext ctx;
Integer counter = 1;
Integer oldCounter = (Integer) request.getAttribute(CLEANUP_RECURSION_COUNTER);
if (oldCounter != null) {
counter = oldCounter + 1;
}
ActionContext oldContext = ActionContext.getContext();
if (oldContext != null) {
// detected existing context, so we are probably in a forward
ctx = new ActionContext(new HashMap<String, Object>(oldContext.getContextMap()));
} else {
ValueStack stack = dispatcher.getContainer().getInstance(ValueStackFactory.class).createValueStack();
stack.getContext().putAll(dispatcher.createContextMap(request, response, null, servletContext));
ctx = new ActionContext(stack.getContext());
}
request.setAttribute(CLEANUP_RECURSION_COUNTER, counter);
ActionContext.setContext(ctx);
return ctx;
}
//如果旧ActionContext 对象不为空,就oldContext.getContextMap()直接获取,否则获取ValueStack对象,将其
相关对象信息putAll(),该方法源码如下:
public Map<String,Object> createContextMap(HttpServletRequest request, HttpServletResponse response,
ActionMapping mapping, ServletContext context) {
// request map wrapping the http request objects
Map requestMap = new RequestMap(request);
// parameters map wrapping the http parameters. ActionMapping parameters are now handled and applied separately
Map params = new HashMap(request.getParameterMap());
// session map wrapping the http session
Map session = new SessionMap(request);
// application map wrapping the ServletContext
Map application = new ApplicationMap(context);
Map<String,Object> extraContext = createContextMap(requestMap, params, session, application, request, response, context);
if (mapping != null) {
extraContext.put(ServletActionContext.ACTION_MAPPING, mapping);
}
return extraContext;
}
其中createContextMap方法是将request和response对象放入到map中。
public HashMap<String,Object> createContextMap(Map requestMap,
Map parameterMap,
Map sessionMap,
Map applicationMap,
HttpServletRequest request,
HttpServletResponse response,
ServletContext servletContext) {
HashMap<String,Object> extraContext = new HashMap<String,Object>();
extraContext.put(ActionContext.PARAMETERS, new HashMap(parameterMap));
extraContext.put(ActionContext.SESSION, sessionMap);
extraContext.put(ActionContext.APPLICATION, applicationMap);
Locale locale;
if (defaultLocale != null) {
locale = LocalizedTextUtil.localeFromString(defaultLocale, request.getLocale());
} else {
locale = request.getLocale();
}
extraContext.put(ActionContext.LOCALE, locale);
//extraContext.put(ActionContext.DEV_MODE, Boolean.valueOf(devMode));
extraContext.put(StrutsStatics.HTTP_REQUEST, request);
extraContext.put(StrutsStatics.HTTP_RESPONSE, response);
extraContext.put(StrutsStatics.SERVLET_CONTEXT, servletContext);
// helpers to get access to request/session/application scope
extraContext.put("request", requestMap);
extraContext.put("session", sessionMap);
extraContext.put("application", applicationMap);
extraContext.put("parameters", parameterMap);
AttributeMap attrMap = new AttributeMap(extraContext);
extraContext.put("attr", attrMap);
return extraContext;
}
通过对源码简单的分析,相信大家应该有所了解,如果需要深入的了解可以查看struts2相关的文档和源码。
发表评论
-
struts2基本属性介绍
2012-11-08 15:40 718struts 2框架有两个核心配置文件: struts.xml ... -
struts2标签介绍
2012-11-08 15:38 888关于struts2的标签,可以 ... -
struts2国际化
2012-10-26 16:17 670关于struts2的国际化 ,网上已经有如多的实例了,可以参考 ... -
struts2 拦截器
2012-10-22 14:47 940我们通过查看是struts-default.xml文件,可以看 ... -
struts2 跳转类型几种类型说明
2012-10-22 11:09 8571.关于struts2 result type的 ... -
struts2配置多个文件
2012-09-11 10:19 956在struts2中配置多个配置文件的方法很多,常见的几种为: ... -
struts2 原理说明
2012-09-09 22:43 682最近复习struts2,其实它就是webwork2.2的升级版 ...
相关推荐
struts1和struts2的区别其实并不是太大,两者的区别: ...Struts2提供一个ActionSupport基类去实现 常用的接口。Action接口不是必须的,任何有execute标识的POJO对象都可以用作Struts2的Action对象。
Struts 2提供一个ActionSupport基类去实现常用的接口。即使Action接口不是必须实现的,只有一个包含execute方法的POJO类都可以用作Struts 2的Action。 2 线程模式方面的对比:Struts 1 Action是单例模式并且必须是...
NULL 博文链接:https://sdh88hf.iteye.com/blog/2229874
程序员上班的必要的书籍,描写在岗位如何遇到的一些问题,...Struts1和Struts2的区别Action 类: Struts1要求Action类继承一个抽象基类。Struts1的一个普遍问题是使用抽象类编程而不是接口。(侵入式编程,不赞同) ...
Struts 1要求Action类要扩展自一个抽象基类。Struts 1的一个共有的问题是面向抽象类编程而不是面向接口编程。
下面小编就为大家带来一篇在Action中以Struts2的方式输出JSON数据的实例。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
Struts2和Struts1的不同 ◆Struts1要求Action类继承一个抽象基类。Struts1的一个普遍问题是使用抽象类编程而不是接口。
特性 Struts 1 Struts 2 Action类 Struts 1要求Action类要扩展自一个抽象基类。Struts 1的一个共有的问题是面向抽象类编程而不是面向接口编程。
详细讲述通过Struts2+hiberante+Spring+ExtJs实现的实例说明,包括S2SH环境搭建和开发的过程,有详细的开发步骤说明,包括通用基类,事务等关键地方的讲解,图文并茂,适合初中级学习者
struts2.2.1 spring2.5.6 proxool0.9.1 jstl1.2 说明:demo实现了最简单的取值,数据库操作。 没有使用hibernate和ibatis。自己写了个基类,包装了获取链接和释放链接。 从action返回后,使用了jstl获取action中的...
Struts2、Hibernate、Spring整合的泛型DAO (本人评价: 代码开发效率提高30% 代码出错率减少70%) 对于大多数开发人员,系统中的每个 DAO 编写几乎相同的代码到目前为止已经成为一种习惯。虽然所有人都将这种重复...
如果需要,Struts2 Action仍然可以访问初始的Request和Response。 但是,其他的元素减少或者消除了直接访问HttpServletRequest和HttpServletResponse的必要性。 d、可测性 测试struts1.2 Action的一个主要问题是...
项目适合对struts2,spring,ibatis 和 restfull以及jquery有所了解的人员用来学习研究执行原理之用,亦可以作为项目对功能进行延伸。底层已经写好的基类。只要在此基础上拓展自己需要的功能接口就可以了,另外项目中...
ssh 分页,作为了基类。它的子类可以只要继承父类就行了。封装性强。
好东西 struts
7、实体类快速生成,提供各种常用的实体类代码生成,直接在窗体中显示,并用语法高亮显示,方便拷贝使用。 8、数据库文档的生成,在模块设计中非常有用,谁想去写描述和字段名称,类型的对应关系呢? 9、测试...
为了简化开发,预定义好了一套目录结构、基类、用来创建数据库、配置Tomcat、测试部署应用的 Ant 任务,帮助快速自动生成源程序和自动维护部分配置文件。 AppFuse2.0重构了AppFuse1.0,转到Maven2和Jdk1.5。
这个项目是基于SSH框架(Spring+hibernate+struts2)框架来实现搭建的,具体搭建说明可以参考我的博客 里面做了详细的说明 当然里面也包含其他功能需要用到的jar包,对配置以及实体配置都有注释说明,非常清晰,相信...
erp 这是我近期做的一个erp的系统的源代码
为了简化开发,预定义好了一套目录结构、基类、用来创建数据库、配置Tomcat、测试部署应用的 Ant 任务,帮助快速自动生成源程序和自动维护部分配置文件。 AppFuse2.0重构了AppFuse1.0,转到Maven2和Jdk1.5。