Spring整合Struts2的两种方法小结

网友投稿 335 2023-04-25


Spring整合Struts2的两种方法小结

spring提供了一个ContextLoaderListener,该监听类实现了ServletContextListener接口。该类可以作为Listener使用,它会在创建时自动查找WEB-INF/下的applicationContext.xml文件,因此如果只有一个配置文件且配置文件命名为applicationContext.xml,则只需在web.xml文件中增加如下配置片段:

org.springframework.web.context.ContextLoaderListener

如果有多个配置文件需要载入,则考虑使用元素确定配置文件的文件名。,COntextLoaderListener加载时,会查找名为contextConfigLocation的初始化参数,因此配置时应指定参数名为contextConfigLocation。

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee

http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">

contectCOnfigLocation

/WEB-INF/daocontext.xml,/WEB-INF/applicationCotext.xml

org.springframework.web.context.ContextLoaderListener

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee

http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">

contectCOnfigLocation

/WEB-INF/daocontext.xml,/WEB-INF/applicationCotext.xml

org.springframework.web.context.ContextLoaderListener

Spring根据配置文件创建WebApplicationContext对象,并将其保存在Web应用的ServletContext中。如果要获取应用中的ApplicationContext实例,则可以根据

如下获取:

WebApplicationContext ctx=WebApplicationContextUtils.getWebApplicationContext(servletContext)

让Spring管理控制器

当Struts2将请求转发给指定的Action时,Struts2中的该Action只是一个傀儡,他只是一个代号,并没有指QhYWauE定实际的实现类,当然也不可能创建Action实例,二隐藏在该action下的是Spring容器中的Action实例,他才是真正处理用户请求的控制器。

其中Struts2只是一个伪控制器,这个伪控制器的功能实际由Spring容器中的控制器来完成,这就实现了让核心控制器调用Spring容器中的action来处理用户请求。在这种策略下,处理用户请求的Action由Spring插件负责创建,但Spring插件创建Action实例时。并不是利用配置Action时指定的class属性来创建该action实例,而是从Spring容器中取出对应的Bean实例完成创建。

web.xml

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee

http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">

org.springframework.web.context.ContextLoaderListener

struts2

org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter

struts2

/*

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee

http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">

org.springframework.web.context.ContextLoaderListener

struts2

org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter

struts2

/*

applicationcontext.xml

xmlns="http://springframework.org/schema/beans"

xsi:schemaLocationQhYWauE="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans-3.0.xsd">

class="com.bh.service.impl.MyServiceImpl"/>

scope="prototype">

xmlns="http://springframework.org/schema/beans"

xsi:schemaLocationQhYWauE="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans-3.0.xsd">

class="com.bh.service.impl.MyServiceImpl"/>

scope="prototype">

class="com.bh.service.impl.MyServiceImpl"/>

scope="prototype">

scope="prototype">

struts.xml

"-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"

"http://struts.apache.org/dtds/struts-2.1.7.dtd">

/WEB-INF/content/error.jsp

/WEB-INF/content/welcome.jsp

/WEB-INF/content/{1}.jsp

使用自动装配

通过设置struts.objectFactory.spring.autoWire常量可以改变Spring插件额自动装配策略,该常量可以接受如下几个值:

Name:根据属性名自动装配。Spring插件会查找容器中全部Bean,找到其中id属性与Action所需的业务逻辑组件同名的Bean,将该bean实例注入到Action实例。

Type:根据属性类型自动装配。Spring插件会查找容器中全部Bean,找出其类型恰好与Action所需的业务逻辑组件相同的Bean,将该Bean实例注入到Action实例。

Auto:Spring插件会自动检测需要使用哪种自动装配方式。

Constructor:与type类似,区别是constructor使用构造器来构造注入的所需参数而不是使用设值注入方式。

web.xml

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee

http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">

org.springframework.web.context.ContextLoaderListener

struts2

org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter

struts2

/*

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee

http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">

org.springframework.web.context.ContextLoaderListener

struts2

org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter

struts2

/*

applicationcontext.xml

xmlns="http://springframework.org/schema/beans"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans-3.0.xsd">

xmlns="http://springframework.org/schema/beans"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans-3.0.xsd">

struts.xml

"-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"

"http://struts.apache.org/dtds/struts-2.1.7.dtd">

class="com.bh.action.LoginAction">

/WEB-INF/content/error.jsp

/WEB-INF/content/welcome.jsp

class="com.bh.action.LoginAction">

/WEB-INF/content/error.jsp

/WEB-INF/content/welcome.jsp

/WEB-INF/content/{1}.jsp


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:以BootStrap Tab为例写一个前端组件
下一篇:vue2.0的contextmenu右键弹出菜单的实例代码
相关文章

 发表评论

暂时没有评论,来抢沙发吧~