Servlet action is not available ,到Google上搜了一把,发现无数人正处我现在遇到的这个问题的痛苦之中,在参考了几个别人的解决方法之后仍然没能解决,在即将崩溃放弃之时,一不小心找出了问题所在,并顺利解决了,不敢偷着乐,赶紧帖出来与大家分享,希望能帮到正处深受此问题困扰的同行们一点小忙。
顺便先讲讲如何把Struts和Spring给整合到一块去,提供还不会整的兄弟们参考参考,如果你只是关心上面提到的那个问题的解决,可以略过直接看最后面应该可以解决!
在Struts中集成Spring,把原来用户的请求由原来的Action直接处理变成请求先被Spring拦截,对Action所依赖的某些关系(如某个Action依赖某个Service)进行依赖注入后,再转到Action继续处理:
首先,先加载集成Spring的插件:就是在struts-config.xml配置文件中加入如下几行代码:
className ="org.springframework.web.struts.ContextLoaderPlugIn" >
< set-property property ="contextConfigLocation" value ="/WEB-INF/applicationContext.xml" />
</ plug-in >
修改前的Action的如下:
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"
input ="/WEB-INF/jsps/sysadmin/login.jsp" parameter ="method"
path ="/login" scope ="request"
type ="org.springframework.web.struts.DelegatingActionProxy" >
</ action >
2、type属性可以保留原样不变,也可以直接删掉如下:
input ="/WEB-INF/jsps/sysadmin/login.jsp" parameter ="method"
path ="/login" scope ="request" >
</ action >
保留原样的原因只是方便以后查看时容易找出对应的处理请求的具体Action类
接着在struts-config.xml配置文件中加入<controller>字节点:
下面是把<controller>字节点附近的代码一起帖出来,目的是为了让大家知道<controller>字节点的位置,以免不必要的麻烦
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中的代码:
< 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="..."之类的 !!!
通过上面几步就完成了对Struts和Spring的整合。
进入正题,解决Servlet action is not available 的问题,
其实就是一个很小的地方修改一下就OK了,当然前提是你的struts-config.xml和applicationContext.xml文件配置确认无误,还出现了上面提示的错误的话,那就恭喜你,成功离你就只有一步之遥了!!!现在只要把jsp页面的Action对应的Form表单的属性action="xxx.do?method=..."改成action="/xxx.do?method=..."就完成了,就这么简单???没错就这么简单,只要在请求路径前加个斜杠就完事!!
这个错误根源有点太离谱了,因为以前没整合Spring的时候,请求路径前面有无斜杠都运行得好好的!但还好找出来了,看到网上很多兄弟估计大多数就是这个原因,郁闷N天,我看大部分人都说的是在Struts和Spring的两个配置文件中死命找原因,但如果真的是在jsp页面出的问题,岂不南辕北辙!!