spring与struts整合问题

川籍黑人 2008-11-19 04:24:13

applicationContext.xml配置如下

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.0.xsd ">

<bean name="/login" class="com.maesinfo.action.UserLevelAction">
<property name="userservice">
<ref bean="userservice" />
</property>
</bean>

<bean id="userservice"
class="com.maesinfo.service.UserServiceImpl">
<property name="userleveldao">
<ref bean="userleveldao" />
</property>
<property name="userdao">
<ref bean="userdao" />
</property>
</bean>

<bean id="userleveldao" class="com.maesinfo.dao.UserlevelDAO" />
<bean id="userdao" class="com.maesinfo.dao.UserDAO" />

<bean id="checkUser" class="com.maesinfo.util.CheckUserLevel" />

<aop:config>
<aop:pointcut id="allcheckmothod" expression="execution(* com.maesinfo.service.UserService.check*(..))"/>
<aop:aspect id="checkuser" ref="checkUser">
<aop:before method="checkLevel" pointcut-ref="allcheckmothod"/>
</aop:aspect>
</aop:config>
</beans>

struts-config.xml配置如下
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">

<struts-config>
<action-mappings>
<action path="/login"
parameter="login"
type="org.springframework.web.struts.DelegatingActionProxy"
scope="request">
<forward name="success" path="/index.jsp"/>
</action>
</action-mappings>
<message-resources
parameter="com.yourcompany.struts.ApplicationResources" />
<!--
spring与struts整合:把struts的Action托管给spring
<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation"
value="classpath:applicationContext.xml"/>
</plug-in>
-->
</struts-config>

使用的是DelegatingActionProxy整合的方式!
在struts-config.xml中没有配置下面的代码,程序运行起来没有错误,是怎么回事?
我在一些书上看,下面的代码是必须配置的。希望明白的帮忙解答一下!感激不尽!
<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation"
value="classpath:applicationContext.xml"/>
</plug-in>
...全文
633 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
sshosdn 2008-12-06
  • 打赏
  • 举报
回复
这些都是javaWeb的基础, 首先你要清楚web.xml , struts-config.xml , applicationContext.xml 它们都是谁的配值文件? 在web.xml中加入是告诉这个web工程都引用了哪些框架技术, 在struts-config.xml中加入才是SS的整合.
SINCE1978 2008-11-25
  • 打赏
  • 举报
回复
Landor2004解释的好,但是根据Landor2004的解释,先把WebApplicationContext都称作wac:

我们一般用spring整合structs是会另起action-context.xml配置文件的,这样以后spring是否会对该配置新建一个模块wac?这个模块wac为全局wac的子这样?
所以我猜想是否因为楼主的structs的action配置并未另起配置文件而是都配在
applicationContext.xml这一个配置文件中所以导致不加plug-in配置亦可?因为:

我怀疑DelegatingActionProxy类的execute方法中通过DelegatingActionUtils来获取wac的时候,这个wac是分模块的,楼主没有分模块那么获取的就是全局wac(或者因为模块wac为null就会默认使用全局wac)。在全局wac中能够取到所需action bean所以定位正确。


ContextLoaderPlugIn.SERVLET_CONTEXT_PREFIX 和
WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE
是不是一样的啊?当modulePrefix为空的时候取得的就是全局wac
NickCheng 2008-11-24
  • 打赏
  • 举报
回复
阅!
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 LW_java 的回复:]

--------------------------------------------------------------------------------

不是,两个都要加,作用不一样,web.xml中的是为了在项目启动时导入和创建Spring配置的bean,struts-config.xml中的配置是为了在提交进入struts的Action时,能找到该Action对应的spring中配置的bean
回答者:mavisliky@sohu.com - 头衔:软件工程师 2008-11-20 10:39:13

---------------------------------------------------…
[/Quote]

楼主引用得红色字体得说法是正确得,两者的作用不一样的。

web.xml中的是为了在项目启动时导入和创建Spring配置的bean,
struts-config.xml中的配置是为了在提交进入struts的Action时,能找到该Action对应的spring中配置的bean

或者你试试上面两种分别配置一项看行不行!
zxdzhou 2008-11-23
  • 打赏
  • 举报
回复
学习
Landor2004 2008-11-21
  • 打赏
  • 举报
回复
在web.xml中需要配置ContextLoaderListener或者ContextLoaderServlet的,看下ContextLoaderPlugIn的code如下
......
/**
* Struts 1.1+ PlugIn that loads a Spring application context for the Struts
* ActionServlet. This context will automatically refer to the root
* WebApplicationContext (loaded by ContextLoaderListener/Servlet) as parent.
......
*/
public class ContextLoaderPlugIn implements PlugIn {
......

public final void init(ActionServlet actionServlet, ModuleConfig moduleConfig) throws ServletException {
......
try {
this.webApplicationContext = initWebApplicationContext();//调用initWebApplicationContext方法
onInit();
}
......
}

}

而在initWebApplicationContext的源码中,是通过WebApplicationContext parent = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
来取得WebApplicationContext的

而WebApplicationContextUtils中是通过Object attr = sc.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
来获取WebApplicationContext的,这里是通过ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE这个键值来取得

而在ContextLoaderServlet或者ContextLoaderListener里面调用了ContextLoader
在ContextLoader中,初始化了WebApplicationContext并且以键值ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE存入servletContext中,代码如下
servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.context);
所以ContextLoaderPlugIn之前也要在web.xml中声明ContextLoaderServlet或者ContextLoaderListener来初始化WebApplicationContext
tiyuzhongxin789 2008-11-21
  • 打赏
  • 举报
回复
应该是不能运行的
plug-in节点 是spring给struts注册的一个插件 这样 两者就可以无缝集成了
川籍黑人 2008-11-21
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 java05 的回复:]
其实两者区别就是 一个是用tomcat加载spring容器 一个是用Stuts加载配置文件。。
为什么选择后者呢,个人理解:如果你配置在web.xml中,那你的系统就一定要运行在tomcat下了··换服务器很麻烦。如果写在struts中,那只用struts启动了,spring就可以加载。这样配置的好处就是两者的整合做到与容器无关,
[/Quote]


配置在web.xml中,换成其它服务器加载的时候不也是先加载web.xml文件吗?

川籍黑人 2008-11-21
  • 打赏
  • 举报
回复

--------------------------------------------------------------------------------

不是,两个都要加,作用不一样,web.xml中的是为了在项目启动时导入和创建Spring配置的bean,struts-config.xml中的配置是为了在提交进入struts的Action时,能找到该Action对应的spring中配置的bean
回答者:mavisliky@sohu.com - 头衔:软件工程师 2008-11-20 10:39:13

--------------------------------------------------------------------------------

上面的这段话在智囊团上面看到了!

他这两个都要加,是必须要加,还是可选?

两个都加了有什么好处?

大家说一下!谢谢了,我会给帖子加分!来了说出自己的看法,我都会给分的!
zidasine 2008-11-21
  • 打赏
  • 举报
回复
<action-mappings>
<action path="/login"
parameter="login"
type="org.springframework.web.struts.DelegatingActionProxy"
scope="request">
<forward name="success" path="/index.jsp"/>
</action>
</action-mappings>

你在action的配置里面指定了 type="org.springframework.web.struts.DelegatingActionProxy"就可以不用配置那段了
java05 2008-11-21
  • 打赏
  • 举报
回复
其实两者区别就是 一个是用tomcat加载spring容器 一个是用Stuts加载配置文件。。
为什么选择后者呢,个人理解:如果你配置在web.xml中,那你的系统就一定要运行在tomcat下了··换服务器很麻烦。如果写在struts中,那只用struts启动了,spring就可以加载。这样配置的好处就是两者的整合做到与容器无关,
Jerry0006 2008-11-21
  • 打赏
  • 举报
回复
加载spring的配置文件一般有以下一些方法:
一. 采用ContextLoaderListener创建ApplicationContext
<context-param>
<!-- 参数名为contextConfigLocation -->
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

二. 采用load-on-startup Servlet创建ApplicationContext
<context-param>
<!-- 参数名为contextConfigLocation -->
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>

<servlet>
<servlet-name>context</servlet-name>
<servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

三. 采用struts插件的方式创建ApplicationContext实例

在struts-config.xml中添加以下代码:
<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation"
value="/WEB-INF/applicationContext.xml" />
</plug-in>
使用其中一种就可以正常加载。
另:如果采用第三种的话,那么就和struts绑定了,不建议采用这种方法。
Landor2004 2008-11-21
  • 打赏
  • 举报
回复
<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">也要写的原因是:
在ContextLoaderPlugIn中还有一段代码
protected WebApplicationContext initWebApplicationContext() throws BeansException, IllegalStateException {
......
WebApplicationContext parent = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
WebApplicationContext wac = createWebApplicationContext(parent);
......
// Publish the context as a servlet context attribute.
String attrName = getServletContextAttributeName();
getServletContext().setAttribute(attrName, wac);
......
}
public String getServletContextAttributeName() {
return SERVLET_CONTEXT_PREFIX + getModulePrefix();
}
在这里把WebApplicationContext以键值 SERVLET_CONTEXT_PREFIX + getModulePrefix() 存入ServletContext中
当在struts-config.xml中配置action的type="org.springframework.web.struts.DelegatingActionProxy"是的时候
在DelegatingActionProxy类的execute方法中通过DelegatingActionUtils,来获取WebApplicationContext

在这里就是通过wac = (WebApplicationContext) actionServlet.getServletContext().getAttribute(
ContextLoaderPlugIn.SERVLET_CONTEXT_PREFIX + modulePrefix);
来获取的,这里的key就是ContextLoaderPlugIn.SERVLET_CONTEXT_PREFIX + modulePrefix

跟到这里,会发现
1 DelegatingActionProxy中的wac是通过ContextLoaderPlugIn.SERVLET_CONTEXT_PREFIX + modulePrefix得到

2 全局的wac是通过WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE得到

而这两个应该是不同的,继续往里跟就能看出来,所以如果不设置plugin的话,DelegatingActionProxy就得不到wac
这是我通过源码看出来的,结论是要设置。
如果有什么不正确的地方,希望大家继续讨论!
川籍黑人 2008-11-20
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 Landor2004 的回复:]
应该不能运行的
[/Quote]

可以运行,在web.xml中加入这段代码是不是在struts-config.xml中就不加入<plug-in>那段代码了?
这两种整合有什么不同?
谢谢!
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>

<servlet>
<servlet-name>SpringContextServlet</servlet-name>
<servlet-class>
org.springframework.web.context.ContextLoaderServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
川籍黑人 2008-11-20
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 qustzhangke 的回复:]
两种加载spring容器的方式,以struts的plugin的方式,让spring接管struts的action,至此这些action就存在于spring的配置文件中,并且可以进行任何spring bean的注入
[/Quote]

那在web.xml中配置呢?
在web.xml中配置也可以进行注入?
两者有什么区别呢?

我看好多资料都是介绍的plgu-in的方式加载spring容器!

为什么不选择在web.xml中加载spring容器呢?

qustzhangke 2008-11-20
  • 打赏
  • 举报
回复
两种加载spring容器的方式,以struts的plugin的方式,让spring接管struts的action,至此这些action就存在于spring的配置文件中,并且可以进行任何spring bean的注入
冀章的小目标 2008-11-20
  • 打赏
  • 举报
回复
应该是不能运行的
plug-in节点 是spring给struts注册的一个插件 这样 两者就可以无缝集成了
Landor2004 2008-11-19
  • 打赏
  • 举报
回复
应该不能运行的
川籍黑人 2008-11-19
  • 打赏
  • 举报
回复
还有web.xml的配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.4"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
<servlet-name>action</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>
<init-param>
<param-name>debug</param-name>
<param-value>3</param-value>
</init-param>
<init-param>
<param-name>detail</param-name>
<param-value>3</param-value>
</init-param>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>


<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>

<servlet>
<servlet-name>SpringContextServlet</servlet-name>
<servlet-class>
org.springframework.web.context.ContextLoaderServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>



</web-app>

67,538

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧