Struts2的Action获取ServletConfig对象的解决方案
2012-11-12 15:56
387 查看
这是一个在实际项目中遇到的问题,问题描述如下:
系统整体架构是通过struts2+公司自己的框架,公司自己的框架做了很多事情,为了共用这些部分,减少开发工作量,现在要在每一个请求到达struts2的action时,在action里得到一个已经初始化的ServletConfig对象,也就是请求到达的时候,ServletConfig已经初始化了,即这个struts的Action有了两重职责,一方面要处理请求转发,另一方面要完成ServletConfig对象的初始化。
那么在struts2的Action中该如何初始化ServletConfig对象呢?其实在Struts的Action中根本不可能直接初始化ServletConfig,Action已经跳出了servelt的范围,ServletConfig的初始化一定是在web应用启动的时候所做的事情。那么通过分析,能在web应用启动的时候进行初始化的方式就有好几种了:
1.直接通过一个servlet实现;
2.通过监听器实现;
3.通过继承struts2的FilterDispatcher,复写init()方法实现。
本人是通过第一种方法实现的,我们知道Servlet里有如下的方法:
Java代码
![](http://my.oschina.net/images/icon_copy.gif)
![](http://my.oschina.net/images/icon_star.png)
![](http://my.oschina.net/images/spinner.gif)
public void init(ServletConfig config) throws ServletException {
super.init(config);
}
public void init(ServletConfig config) throws ServletException {
super.init(config);
}
写一个servlet,复写上面的init方法,即可达到初始化ServletConfig对象的目的
例如:
Java代码
![](http://my.oschina.net/images/icon_copy.gif)
![](http://my.oschina.net/images/icon_star.png)
![](http://my.oschina.net/images/spinner.gif)
public void init(ServletConfig config) throws ServletException {
super.init(config);
将config设置到ServletContext中,需要的时候去get即可
config.getServletContext().setAttribute("servletConfig", config);
System.out.println("初始化Config对象......" + servletConfig.toString());
}
public void init(ServletConfig config) throws ServletException {
super.init(config);
将config设置到ServletContext中,需要的时候去get即可
config.getServletContext().setAttribute("servletConfig", config);
System.out.println("初始化Config对象......" + servletConfig.toString());
}
并在web.xml中做如下配置
例如:
Java代码
![](http://my.oschina.net/images/icon_copy.gif)
![](http://my.oschina.net/images/icon_star.png)
![](http://my.oschina.net/images/spinner.gif)
<servlet>
<servlet-name>initServlet</servlet-name>
<servlet-class>com.servlet.InitServlet</servlet-class>
<init-param>
<param-name>method</param-name>
<param-value>findByPage</param-value>
</init-param>
<init-param>
<param-name>pageSize</param-name>
<param-value>15</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet>
<servlet-name>initServlet</servlet-name>
<servlet-class>com.servlet.InitServlet</servlet-class>
<init-param>
<param-name>method</param-name>
<param-value>findByPage</param-value>
</init-param>
<init-param>
<param-name>pageSize</param-name>
<param-value>15</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
在web应用启动的时候,ServletConfig对象就进行了初始化,并将其初始化信息放置在了ServletContext中,在需要ServletConfig对象信息时,我们就可以在struts2的Action中通过如下的代码得到servletConfig对象信息:
Java代码
![](http://my.oschina.net/images/icon_copy.gif)
![](http://my.oschina.net/images/icon_star.png)
![](http://my.oschina.net/images/spinner.gif)
ServletConfig sconfig = (ServletConfig)request.getSession().getServletContext().getAttribute("servletConfig");
ServletConfig sconfig = (ServletConfig)request.getSession().getServletContext().getAttribute("servletConfig");
以上是在项目开发中遇到的一个小问题,当然你可能不会遇到这个问题,(因为我在公司自己的框架中开发,会有一些限制)但我相信这个思路会对大家遇到类似问题时有一些帮助。
另外,我们甚至可以让自己的struts2的Action继续于一个servlet,此时的Action像这样:
Java代码
![](http://my.oschina.net/images/icon_copy.gif)
![](http://my.oschina.net/images/icon_star.png)
![](http://my.oschina.net/images/spinner.gif)
public class MyAction extends MyServlet implements Action
public class MyAction extends MyServlet implements Action
在MyServlet中做一些初始化的工作,然后实现Action接口的public String execute() throws Exception方法,这也同样是一个struts2的Action。 只不过,struts2的初衷就是要脱离servlet容器,而如果继承一个servlet就有点违背struts2的初衷了。
系统整体架构是通过struts2+公司自己的框架,公司自己的框架做了很多事情,为了共用这些部分,减少开发工作量,现在要在每一个请求到达struts2的action时,在action里得到一个已经初始化的ServletConfig对象,也就是请求到达的时候,ServletConfig已经初始化了,即这个struts的Action有了两重职责,一方面要处理请求转发,另一方面要完成ServletConfig对象的初始化。
那么在struts2的Action中该如何初始化ServletConfig对象呢?其实在Struts的Action中根本不可能直接初始化ServletConfig,Action已经跳出了servelt的范围,ServletConfig的初始化一定是在web应用启动的时候所做的事情。那么通过分析,能在web应用启动的时候进行初始化的方式就有好几种了:
1.直接通过一个servlet实现;
2.通过监听器实现;
3.通过继承struts2的FilterDispatcher,复写init()方法实现。
本人是通过第一种方法实现的,我们知道Servlet里有如下的方法:
Java代码
![](http://my.oschina.net/images/icon_copy.gif)
![](http://my.oschina.net/images/icon_star.png)
![](http://my.oschina.net/images/spinner.gif)
public void init(ServletConfig config) throws ServletException {
super.init(config);
}
public void init(ServletConfig config) throws ServletException {
super.init(config);
}
写一个servlet,复写上面的init方法,即可达到初始化ServletConfig对象的目的
例如:
Java代码
![](http://my.oschina.net/images/icon_copy.gif)
![](http://my.oschina.net/images/icon_star.png)
![](http://my.oschina.net/images/spinner.gif)
public void init(ServletConfig config) throws ServletException {
super.init(config);
将config设置到ServletContext中,需要的时候去get即可
config.getServletContext().setAttribute("servletConfig", config);
System.out.println("初始化Config对象......" + servletConfig.toString());
}
public void init(ServletConfig config) throws ServletException {
super.init(config);
将config设置到ServletContext中,需要的时候去get即可
config.getServletContext().setAttribute("servletConfig", config);
System.out.println("初始化Config对象......" + servletConfig.toString());
}
并在web.xml中做如下配置
例如:
Java代码
![](http://my.oschina.net/images/icon_copy.gif)
![](http://my.oschina.net/images/icon_star.png)
![](http://my.oschina.net/images/spinner.gif)
<servlet>
<servlet-name>initServlet</servlet-name>
<servlet-class>com.servlet.InitServlet</servlet-class>
<init-param>
<param-name>method</param-name>
<param-value>findByPage</param-value>
</init-param>
<init-param>
<param-name>pageSize</param-name>
<param-value>15</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet>
<servlet-name>initServlet</servlet-name>
<servlet-class>com.servlet.InitServlet</servlet-class>
<init-param>
<param-name>method</param-name>
<param-value>findByPage</param-value>
</init-param>
<init-param>
<param-name>pageSize</param-name>
<param-value>15</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
在web应用启动的时候,ServletConfig对象就进行了初始化,并将其初始化信息放置在了ServletContext中,在需要ServletConfig对象信息时,我们就可以在struts2的Action中通过如下的代码得到servletConfig对象信息:
Java代码
![](http://my.oschina.net/images/icon_copy.gif)
![](http://my.oschina.net/images/icon_star.png)
![](http://my.oschina.net/images/spinner.gif)
ServletConfig sconfig = (ServletConfig)request.getSession().getServletContext().getAttribute("servletConfig");
ServletConfig sconfig = (ServletConfig)request.getSession().getServletContext().getAttribute("servletConfig");
以上是在项目开发中遇到的一个小问题,当然你可能不会遇到这个问题,(因为我在公司自己的框架中开发,会有一些限制)但我相信这个思路会对大家遇到类似问题时有一些帮助。
另外,我们甚至可以让自己的struts2的Action继续于一个servlet,此时的Action像这样:
Java代码
![](http://my.oschina.net/images/icon_copy.gif)
![](http://my.oschina.net/images/icon_star.png)
![](http://my.oschina.net/images/spinner.gif)
public class MyAction extends MyServlet implements Action
public class MyAction extends MyServlet implements Action
在MyServlet中做一些初始化的工作,然后实现Action接口的public String execute() throws Exception方法,这也同样是一个struts2的Action。 只不过,struts2的初衷就是要脱离servlet容器,而如果继承一个servlet就有点违背struts2的初衷了。
相关文章推荐
- Struts2的Action获取ServletConfig对象的解决方案
- Struts2的Action获取ServletConfig对象的解决方案
- struts2中ServletActionContext与ActionContext区别及获取Servlet对象方法
- Struts2的Action中获取HttpServletRequest对象
- struts2的action中获得HttpServletRequest对象
- 【struts2】jsp如何获取struts2 action中返回的对象的值
- Action中获取Servlet的其他对象-request.response,ActionMapping,context
- servlet都有一个servletConfig对象;四个config对象可以调用的方法;ServletContext对象之获取web项目信息;设置全局初始化参数的配置
- Action中获取HttpServletRequest对象的方法(表单提交值,url传递参数)
- action中获取HttpServletRequest对象
- Struts2和Servlet共用时获取ServletContext对象的问题
- ActionContext与ServletActionContext的区别及获取request、session等对象
- Struts2初学 Struts2在Action获取内置对象+++struts2自定义类型转换器
- Struts2-Action中获取ServletAPI
- struts2 如何获取servlet对象
- Struts2_Action获取Scope对象
- struts2的s:iterator标签在jsp页面获取action中数组对象
- struts2 ActionContext获取session对象有时为null
- Struts2获取servlet的内置对象和struts2的通配符
- ActionContext与ServletActionContext的区别及获取request、session等对象