技术开发 频道

Spring,Struts整合方法

     四、在另一篇文章中提到在上面的方法中OpenSessionInView Filter不能用

    这个东西我也不熟悉,是不是有不少Spring的东西在这种方式中都不能用呢? 这就说到另一种取得Spring WebApplicationContext的方法:
    在web.xml中配置ContextLoaderListener:

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/beans.xml
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

    对应的beans.xml和前边那个一样,Log4jConfigListener先不用管,去查看相关文档。Web服务启动的时候,我们去看看ContextLoaderListener作了什么:

WebApplicationContext = createWebApplicationContext(servletContext, parent);
servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.context);

    同样是保存WebApplicationContext,但是key是ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE

    怎么才能不用ContextLoaderPlugIn而只用ContextLoaderListener?下面我修改org.springframework.web.struts. DelegatingActionProxy 把它修改成ca.nexcel.books.actions. DelegatingActionProxy并且修改一下代码,修改getWebApplicationContext方法

Return DelegatingActionUtils.findRequiredWebApplicationContext(actionServlet, moduleConfig);

    换成下边方法

ServletContext sc = actionServlet.getServletContext(); WebApplicationContext wac = null; wac = WebApplicationContextUtils.getWebApplicationContext(sc); return wac;

    并且在struts-config.xml中将action的type指向自己的ca.nexcel.books.actions. DelegatingActionProxy,PlugIn删除web.xml加上刚才提到的Listener,启动tomcat运行一切正常。

    五、我把northland的配置文件贴出来。
    Struts-config.xml:

<action-mappings>
<action
path="/list"
input="/list.jsp"
name="_list"
scope="request"
type="jp.co.nec.hnes.northland.web.struts.FlowAction"
>
<display-name>一覧画面</display-name>
</action>
<action
path="/register"
input="/register.jsp"
name="_register"
scope="request"
type="jp.co.nec.hnes.northland.web.struts.FlowAction"
>
<display-name>登録画面</display-name>
</action>
Web.xml:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:flowConfig.xml,
classpath:viewConfig.xml,
classpath:applicationContext.xml,
classpath:applicationContext-extra.xml
</param-value>
</context-param> 
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>ActionServlet</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
</servlet>

    从中可以看到其中的jp.co.nec.hnes.northland.web.struts.FlowAction和ca.nexcel.books.actions. DelegatingActionProxy的功能差不多。

0
相关文章