3

I'm trying to learn how to operate with links using namespaces, actions and Struts tags.

I have a simple login form in index.jsp page:

<s:form action="login" method="POST" namespace="/welcome">
    <s:textfield name="email" label="e-mail" type="email"></s:textfield>
    <s:password name="password" label="Password" type="password"></s:password>
    <s:submit value="Log-in"></s:submit>
</s:form>

<s:url var="url" namespace="/client" action="register"></s:url>
<p>
    <s:a label="Register" href="#url" />
</p>

And following mapping in struts.xml:

  <struts>
    <constant name="struts.devMode" value="true" />
    <package name="welcome" namespace="/welcome" extends="struts-default">
        <action name="index">
            <result>/index.jsp</result>
        </action>
    </package>

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

        <action name="register"
            class="magazine.action.client.RegisterClientAction"
            method="execute">
            <result name="input" type="redirect">/index.jsp</result>
        </action>

        <action name="login"
            class="magazine.action.client.LoginClientAction"
            method="execute">
            <result name="input" type="redirect">/WEB-INF/view/client/view.jsp
            </result>
            <result name="error" type="redirect">/index.jsp</result>
        </action>
    </package>

</struts>

The index.jsp is displaying, but I'm getting in debug mode is: No configuration found for the specified action.

And register link also appears, but it's broken. I saw similar posts and main goal in answers was to check namespaces and syntax issues. I'm a starter in Struts 2, but didn't see that problems in my code, maybe I'm using it in the wrong way ?

4

3 回答 3

3

问题是将操作映射到相应的命名空间

在您的 HTML 表单标签中,您提到您的操作是登录,并且欢迎使用命名空间。像这样,

<form action="login" id="myForm" method="post" namespace="/welcome">

但是根据您与命名空间映射的包welcome没有调用的动作login,但它在另一个包中可用。您需要做的就是将登录操作复制并粘贴到欢迎包中,或者在欢迎包中创建名称为 login 的新操作。

于 2015-03-02T10:14:24.763 回答
1

您会看到此警告,通知像您这样的开发人员您可能使用了不正确的表单标记映射。仅当您使用 时才会显示这些消息struts.devMode=true。您在表单标签中使用了错误的命名空间值。改变

<s:form namespace="/client" action="login" method="POST">
    <s:textfield name="email" label="e-mail" type="email"/>
    <s:password name="password" label="Password" type="password"/>
    <s:submit value="Log-in"/>
</s:form>

锚标签可以使用动作名称和命名空间生成没有 url 的链接。例如

<s:a namespace="/client" action="register">Register</s:a>

但是,如果您需要构建 url,则使用 OGNL 语法来强制评估href属性中的表达式。

<s:url var="url" namespace="/client" action="register"/>
<p>
    <s:a href="%{#url}">Register</s:a>
</p>
于 2015-03-02T11:41:11.287 回答
0

我想问题出在你的锚标签上,对吧?如果是这样,试试这个,

<a href="<s:url namespace="/client" action="register"/>">Register</a>

改变你 struts.xml

<action name="register"
        class="magazine.action.client.RegisterClientAction"
        method="execute">
        <result name="input">/index.jsp</result>                
</action>

type="redirect" 用于重定向到其他操作,而您正在重定向到 index.jsp

于 2015-03-02T09:31:06.310 回答