关于Struts2中的Action Mapping - 没有Action映射

时间:2014-09-02 19:28:16

标签: java jsp struts2 actionresult xml-configuration

我正在尝试运行我的struts应用程序,但是我收到错误操作未映射我已经看到命名空间它是正确但仍然得到错误?

struts.xml

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

<struts>

     <constant name="struts.enable.DynamicMethodInvocation" value="false" />
    <constant name="struts.devMode" value="false" />

    <include file="register.xml"/>

    <!-- Add packages here -->

</struts>

我的register.xml

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

<struts>

     <package name="register" namespace="/" extends="struts-default">

        <action name="Register" class="com.struts2.RegisterAction">
            <result name="input">/register.jsp</result>
            <result type="redirectAction">register.jsp</result>
        </action>

        <!-- Add actions here -->
    </package>
</struts>

我正在验证我的注册页面和验证XML,如下所示

RegisterAction-validation.xml:

<!DOCTYPE validators PUBLIC
        "-//OpenSymphony Group//XWork Validator 1.0.2//EN"
        "http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">

<validators>
    <field name="username">
        <field-validator type="requiredstring">
            <message key="requiredstring"/>
        </field-validator>
    </field>
    <field name="password">
        <field-validator type="requiredstring">
            <message key="requiredstring"/>
        </field-validator>
        <field-validator type="stringlength">
            <param name="minLength">6</param>
            <param name="trim">true</param>
            <message key="requiredpassword"/>
        </field-validator>
    </field>
    <field name="email">
        <field-validator type="requiredstring">
            <message key="requiredstring"/>
        </field-validator>
        <field-validator type="email">
            <message key="requiredemail"/>
        </field-validator>
    </field>
    <field name="gender">
        <field-validator type="requiredstring">
            <message key="requiredstring"/>
        </field-validator>
    </field>
    <field name="postalcode">
        <field-validator type="requiredstring">
            <message key="requiredstring"/>
        </field-validator>
        <field-validator type="regex">
            <param name="expression"><![CDATA[^\d*$]]></param>
            <message key="requiredinteger"/>
        </field-validator>
    </field>
</validators>

我的register.jsp如下:

<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
    <title>Validation Struts page</title>
    <s:head/>
</head>

<body>
<s:form action="Register">
    <s:textfield key="username"/>
    <s:password key="password" />
    <s:textfield key="email" />
    <s:select headerKey="" headerValue="Select Gender"
     key="gender" list="#{'M':'Male','F':'Female'}" />
    <s:textfield key="postalcode" /> 
    <s:submit/>
</s:form>
</body>
</html>

我的项目结构如下:

Strutsvalidation
--src-->
    com.struts2(package)-->
      register.xml,
      registeraction-validation.xml,
      javaclasses,
      struts.xml
--webcontent-->
    web-inf-->
      web.xml
    register.jsp(in web content)

我收到的错误如下?

There is no Action mapped for action name Register. - [unknown location]
    at com.opensymphony.xwork2.DefaultActionProxy.prepare(DefaultActionProxy.java:185)
    at org.apache.struts2.impl.StrutsActionProxy.prepare(StrutsActionProxy.java:63)
    at org.apache.struts2.impl.StrutsActionProxyFactory.createActionProxy(StrutsActionProxyFactory.java:37)
    at com.opensymphony.xwork2.DefaultActionProxyFactory.createActionProxy(DefaultActionProxyFactory.java:58)
    at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:552)
    at org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77)
    at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:99)
    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:501)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
    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:408)
    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.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Unknown Source)

1 个答案:

答案 0 :(得分:0)

此配置错误

<result type="redirectAction">register.jsp</result>

使用

<result>/register.jsp</result>

redirectAction结果应该用于重定向到操作,该名称用于定位。

当您尝试提交时,s:form标记会出现例外情况。实际打包在命名空间Register中的操作/。这不是默认命名空间,因此应该在s:form标签上使用。

<s:form namespace="/" action="Register">