小编典典

关于Struts2中的动作映射-没有动作映射

jsp

我正在尝试运行我的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)

阅读 215

收藏
2020-06-08

共1个答案

小编典典

此配置错误

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

采用

<result>/register.jsp</result>

redirectAction结果应该被用来重定向到行动,这名用于位置。

s:form尝试提交时,异常来自标记。该操作Register实际上打包在namespace中/。这不是默认的名称空间,因此应在s:form标记上使用。

<s:form namespace="/" action="Register">
2020-06-08