Could not find action or result常见情况分析

使用struts2,遇到Could not find action or result这种情况是常事。

=======1.struts.xml配置的action与我们访问的action或返回的result确实没有匹配导致。

我的struts.xml配置如下,action 的name为login,如果不小心把action访问输成loginn.action,多了个n别的,确实找不到action,报错情理之中,很好理解,一看就能找到错误.

如果输入action访问正确,还报错,一种简单情况就是result是别的,struts.xml配置中确实没有,也报错,解决很简单,result对应上就行。

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
	"http://struts.apache.org/dtds/struts-2.1.7.dtd">

<struts>

    <package name="login" extends="struts-default">
    	<!-- 定义拦截器 -->
    	<!-- <interceptors>
    		<interceptor name="security" class="com.lanhuigu.interceptor.SecurityInterceptor"/>
    	</interceptors> -->
        <action name="login" class="com.lanhuigu.action.LoginAction" method="login">
        	<!-- 使用自定义拦截器,不用默认拦截器 -->
        	<!-- <interceptor-ref name="security"></interceptor-ref> -->
        	<!-- 登录成功,跳转到成功页面的首页 -->
            <result name="success">/index.jsp</result>
            <!-- 登录失败,跳转到登录页面,重新登录 -->
            <result name="error">/login/jsp/login.jsp</result>
            <!-- <result name="input">/login/jsp/login.jsp</result> -->
        </action>
    </package>

</struts>
======2.我们的action输入正确,返回结果经过无数次查看都是对的,就是报找不到action或者没有对应的result结果,我们也有解决办法.

++++2.1 查看项目下的struts.xml文件是否被编译上,有时我们分模块开发,细心检查自己的struts-xxx.xml是否被引入struts.xml中,即看你的

struts.xml中是否通过include引入子模块的struts-xxx.xml文件。

++++2.2 如果你采用以上手段,问题还没被解决,接着往下看,或许能给你点灵感。

        人生最纠结的是,struts.xml文件等都正常,action,result都没错,访问action时,不进入action方法,直接给你返回input,而你的struts.xml文件的result确实没有一个叫name为input的对应,报错是必然的,如果你给加个input的result,确实可以给你指定映射视图,否则,直接给你来个Could not find action or result.....有没有无语,以下为详细错误。

!!!!!!!!!!!!!!!!!!!!!!

警告: Could not find action or result
No result defined for action com.lanhuigu.action.LoginAction and result input
    at com.opensymphony.xwork2.DefaultActionInvocation.executeResult(DefaultActionInvocation.java:364)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:266)
    at com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:252)
    at org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:68)
    at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    at com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:122)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:195)
    at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:195)
    at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    at com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:179)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    at org.apache.struts2.interceptor.MultiselectInterceptor.intercept(MultiselectInterceptor.java:75)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    at org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:94)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    at org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:235)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    at com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.java:89)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    at com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept(ScopedModelDrivenInterceptor.java:130)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    at org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:267)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    at com.opensymphony.xwork2.interceptor.ChainingInterceptor.intercept(ChainingInterceptor.java:126)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    at com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:138)
    at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    at com.opensymphony.xwork2.interceptor.I18nInterceptor.intercept(I18nInterceptor.java:165)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    at org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:164)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    at com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:179)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    at com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:176)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    at org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:52)
    at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:488)
    at org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77)
    at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:91)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:503)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:421)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1070)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:611)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:619)
错误原因:

系统内部异常,也就是你写sturts的action时,继承了ActionSupport类,而ActionSupport类中的validate()方法对传入参数校验异常,ActionSupport中的方法如下:

/**
     * A default implementation that validates nothing.
     * Subclasses should override this method to provide validations.
     */
    public void validate() {
    }

详细可以看源码,但是这玩意是罪魁祸首。

报的错误代码行如下,就是我们看到错误的第一行,直接抛异常。

Could not find action or result常见情况分析_第1张图片

解决办法:

1.如果我们的action中重写了validata()方法,一定写好了,抛异常就报错,现在jquery-validata...校验的都很强大,没有必要用他,直接注释。

2.自己定义拦截器,不用sturts拦截器,对登录页面自行拦截处理。

<span style="color:#333333;"><?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
	"http://struts.apache.org/dtds/struts-2.1.7.dtd">

<struts>

    <package name="login" extends="struts-default">
    	<!-- 定义拦截器 -->
    	<<interceptors>
    		<interceptor name="security" class="com.lanhuigu.interceptor.SecurityInterceptor"/>
    	</interceptors> 
        <action name="login" class="com.lanhuigu.action.LoginAction" method="login">
        	<!-- 使用自定义拦截器,不用默认拦截器 -->
        	<interceptor-ref name="security"></interceptor-ref>
        	<!-- 登录成功,跳转到成功页面的首页 -->
            <result name="success">/index.jsp</result>
            <!-- 登录失败,跳转到登录页面,重新登录 -->
            <result name="error">/login/jsp/login.jsp</result>
            <!-- <result name="input">/login/jsp/login.jsp</result> -->
        </action>
    </package>

</struts>
</span>

自己定义拦截器,aop的思想,自己处理,自己校验判断,就避免代码进入struts拦截器,出现一堆系统内部异常,找问题费劲。

注意:一般不建议自定义拦截器,struts-default.xml中已经为struts2框架准备了'足够'的拦截器,不要给自己添麻烦,增加编码繁琐


你可能感兴趣的:(struts2,Interceptor,find,result,could,not,actio)