Struts集成Spring出错:Servlet action is not available 的解决

本文详细介绍如何将Struts与Spring框架整合,并解决常见的Servletactionisnotavailable问题。通过步骤说明,帮助读者实现两个框架之间的无缝对接。

Servlet action is not available

先讲讲如何把StrutsSpring给整合到一块去,提供还不会整的兄弟们参考参考,如果你只是关心上面提到的那个问题的解决,可以略过直接看最后面应该可以解决!

      Struts中集成Spring,把原来用户的请求由原来的Action直接处理变成请求先被Spring拦截,对Action所依赖的某些关系(如某个Action依赖某个Service)进行依赖注入后,再转到Action继续处理:

首先,先加载集成Spring的插件:就是在struts-config.xml配置文件中加入如下几行代码:

<plug-in
       
className="org.springframework.web.struts.ContextLoaderPlugIn">
       
<set-property property="contextConfigLocation" value="/WEB-INF/applicationContext.xml" />
</plug-in>

修改前的Action的如下:

<action attribute="LoginForm" name="LoginForm"
     input
="/WEB-INF/jsps/sysadmin/login.jsp" parameter="method"
     path
="/login" scope="request"
     type
="com.custudio.cdman.ui.admin.struts.action.LoginAction">
</action>

Action的修改通常有以下两种做法:

1、把type属性改为"org.springframework.web.struts.DelegatingActionProxy"

<action attribute="LoginForm" name="LoginForm"
     input
="/WEB-INF/jsps/sysadmin/login.jsp" parameter="method"
     path
="/login" scope="request"
     type
="org.springframework.web.struts.DelegatingActionProxy">
</action>

2type属性可以保留原样不变,也可以直接删掉如下:

<action attribute="LoginForm" name="LoginForm"
     input
="/WEB-INF/jsps/sysadmin/login.jsp" parameter="method"
     path
="/login" scope="request">
</action>

保留原样的原因只是方便以后查看时容易找出对应的处理请求的具体Action

接着在struts-config.xml配置文件中加入<controller>字节点

下面是把<controller>字节点附近的代码一起帖出来,目的是为了让大家知道<controller>字节点的位置,以免不必要的麻烦

<action forward="/WEB-INF/jsps/cdmgr/cdMgr.jsp"
            path
="/showCDMgrMain" />
        
<action forward="/WEB-INF/jsps/datamgr/dataMgr.jsp"
            path
="/showDataMgrMain" />
    
</action-mappings>
    
    
<controller
        
processorClass="org.springframework.web.struts.DelegatingRequestProcessor" />
        
    
<message-resources
        
parameter="com.custudio.cdman.ui.resource.ApplicationResources" />
    
<plug-in className="org.apache.struts.validator.ValidatorPlugIn">
        
<set-property property="pathnames"
            value
="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml" />
    
</plug-in>
    
<plug-in
        
className="org.springframework.web.struts.ContextLoaderPlugIn">
        
<set-property property="contextConfigLocation"
            value
="/WEB-INF/applicationContext.xml" />
    
</plug-in>

 

到 此struts-config.xml文件修改完成,接着就是如何让Spring在拦截用户的请求了,其实很新简单,只要把请求对应Action注册成 Spring的一个bean就可以了,仍以上面的那个Action为例,下面是applicationConfig.xml中的代码:

<!-- Service -->
    
<bean id="IOperatorService"
        class
="com.custudio.cdman.bussiness.sysadmin.OperatorServiceImpl"
        abstract
="false" singleton="true" lazy-init="default"
        autowire
="default" dependency-check="default">
        
<property name="operatorDAO">
            
<ref bean="OperatorDAO" />
        
</property>
    
</bean>
    
    
<!-- Struts Action -->    
    
    
<bean name="/login"
        class
="com.custudio.cdman.ui.admin.struts.action.LoginAction"
        abstract
="false" singleton="true" lazy-init="default"
        autowire
="default" dependency-check="default">
        
<property name="operatorService">
            
<ref bean="IOperatorService" />
        
</property>
    
</bean>

注意 这个bean用的是name="/login",不能用id="..."之类的 !!!

通过上面几步就完成了对StrutsSpring的整合。

进入正题,解决Servlet action is not available 的问题,

其实就是一个很小的地方修改一下就OK了,当然前提是你的struts-config.xmlapplicationContext.xml文件配置确认无误,还出现了上面提示的错误的话,那现在试着把jsp页面的Action对应的Form表单的属性action="xxx.do?method=..."改成action="/xxx.do?method=..."

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值