没有为动作定义com.hussi.action.LoginAction和结果SUCCESS

时间:2015-11-29 18:01:54

标签: java jsp configuration struts2

我正在学习struts 2 这是我的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    id="WebApp_ID" version="2.5">
  <display-name>HussiStruts1</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <filter>
    <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>

这是我的struts.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
   "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
   "http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
    <package name="default" extends="struts-default">
        <action name="login" class="com.hussi.action.LoginAction">
            <result name="SUCCESS">/success.jsp</result>
            <result name="failure">/failure.jsp</result>
        </action>
        <action name="failure" class="com.hussi.action.LoginAction"></action>
    </package>
</struts>

这是我的动作类

package com.hussi.action;

public class LoginAction 
{
    public String execute()
    {
        System.out.println("Action : Login Action === Method : execute");
        return "SUCCESS";
    }

}

当我执行项目时,它会将index.jsp作为欢迎页面

但是当我点击网址时:http://localhost:8080/HussiStruts1/login.action

它给出了错误

  

没有为行动定义com.hussi.action.LoginAction和结果成功

也正在打印动作类中的行,这意味着达到了动作类,其结果无法找到,

我将“web”命名为我的课程文件夹 在我的struts.xml中,我试过了

<result name="SUCCESS">/success.jsp</result>
<result name="SUCCESS">success.jsp</result>
<result name="SUCCESS">web/success.jsp</result>
<result name="SUCCESS">/web/success.jsp</result>
<result name="SUCCESS">./web/success.jsp</result>

似乎都不起作用

这是整个日志

Action : Login Action === Method : execute
Nov 30, 2015 12:03:16 AM com.opensymphony.xwork2.util.logging.commons.CommonsLogger warn
WARNING: Could not find action or result
No result defined for action com.hussi.action.LoginAction and result SUCCESS
    at com.opensymphony.xwork2.DefaultActionInvocation.executeResult(DefaultActionInvocation.java:363)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:265)
    at com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:163)
    at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
    at com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:249)
    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:236)
    at com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:122)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
    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:236)
    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:236)
    at com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:148)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
    at org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:93)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
    at org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:235)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
    at com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.java:89)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
    at com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept(ScopedModelDrivenInterceptor.java:128)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
    at org.apache.struts2.interceptor.ProfilingActivationInterceptor.intercept(ProfilingActivationInterceptor.java:104)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
    at org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:267)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
    at com.opensymphony.xwork2.interceptor.ChainingInterceptor.intercept(ChainingInterceptor.java:126)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
    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:236)
    at com.opensymphony.xwork2.interceptor.I18nInterceptor.intercept(I18nInterceptor.java:148)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
    at org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:164)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
    at com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:128)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
    at com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:176)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
    at org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:52)
    at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:468)
    at org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77)
    at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:76)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:217)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:673)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1500)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1456)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:745)

任何建议??

的success.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>success</title>
</head>
<body>

</body>
</html>

3 个答案:

答案 0 :(得分:0)

  • 也许你应该在你的库中添加更多jar,对我来说,我创建了一个包含这些jar的struts2库:

enter image description here

enter image description here

  • 也许你应该添加struts 2在Eclipse中的Web部署程序集中部署路径,如下图所示: enter image description here

答案 1 :(得分:0)

  

没有为动作定义com.hussi.action.LoginAction和结果SUCCESS

表示struts无法为<result name="SUCCESS">中的登录操作找到结果struts.xml

在您提供的代码中,结果已定义,因此您可以检查:

  • 你实际上已经保存了两个文件(动作类和struts.xml)
  • 部署.war时留下的任何旧文件 - 尝试清理项目(以及服务器的deploy目录)
  • 也许字符串&#34; SUCCESS&#34; (在动作或xml中)包含一些不可打印的字符(因此它们看起来相同但实际上是不同的)

答案 2 :(得分:0)

错误在于SUCCESS结果的非标准命名,而在第二个操作中缺少定义。

当您执行extends ActionSupportimplements Action时,您可以通过适当的常量返回默认结果,例如。 SUCCESSERRORINPUTNONE

SUCCESS常量映射到"success"字符串,即小写

您将手动返回字符串"SUCCESS",即大写,将其映射到struts.xml和操作中;它有效,但它绝对是一个不好的做法:

<action name="login" class="com.hussi.action.LoginAction">
    <result name="SUCCESS">/success.jsp</result>
    <result name="failure">/failure.jsp</result>
</action>

然后您将第二个动作映射到同一个Action类,而不定义任何结果!

<action name="failure" class="com.hussi.action.LoginAction"></action>

此无结果操作会调用execute()方法,该方法会尝试返回"SUCCESS",而不会找到任何匹配项。

然后你也可以复制失败行动的结果

<action name="login" class="com.hussi.action.LoginAction">
    <result name="SUCCESS">/success.jsp</result>
    <result name="failure">/failure.jsp</result>
</action>
<action name="failure" class="com.hussi.action.LoginAction">
    <result name="SUCCESS">/success.jsp</result>
    <result name="failure">/failure.jsp</result>
</action>

但这都不是最佳做法;如果您需要在发生故障时重定向到登录操作,只需使用redirectAction结果,就可以使用某些内容(以下示例中的内容将隐含foo()返回{ {1}}或SUCCESS}喜欢:

"success"

但最后,如果您的登录操作有<action name="login" class="com.hussi.action.LoginAction"> <result name="SUCCESS">/success.jsp</result> <result name="failure">/failure.jsp</result> </action> <action name="failure" class="com.hussi.action.LoginAction" method="foo"> <result type="redirectAction">login.action</result> </action> 结果,为什么您应该failure.jsp返回登录操作或登录,则根本不清楚结果。尝试清楚地定义您需要的内容,并避免自定义,伪装的结果。