Regarding the Action Mapping in Struts2 - There is

2019-02-20 00:38发布

问题:

I am trying to run my struts application but I am getting a error action is not mapped I have seen the namespace it is correct but still getting the error?

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>

And my 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>

I am validating my register page and the validation XML as follows

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>

And my register.jsp as follows:

<%@ 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>

My Project Structure as Follows:

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

I am getting the error as follows?

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:

This configuration is wrong

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

use

<result>/register.jsp</result>

The redirectAction result should be used to redirect to the action, which name is used for location.

The exception comes from the s:form tag when you tried to submit it. The action Register actually packaged in namespace /. This not a default namespace and therefore should be used on the s:form tag.

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