andy007
andy007

Reputation: 917

Form validation in Struts2

Page contains form which looks like below.

1) If I insert to form validate="true", then after page load this form submit request to mail action (at this point validation xml was not created yet).

2) After validation xml was created (in the same pakage with action), then action which control request to this page returns 404 page.

Did I miss something?

<s:form action="mail" method="post">
<s:textfield name="name" key="Your name" size="20" />   
<div style="clear: both;margin-top:10px"></div>
<s:textarea label="Comment" name="comment" cols="65" rows="5"/>
<s:submit method="mail" key="Send" align="left" 
    style="width:100px; height:35px; margin-top:20px"/>
</s:form>

validation xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE validators PUBLIC "-//Apache Struts//XWork Validator 1.0.3//EN"
    "http://struts.apache.org/dtds/xwork-validator-1.0.3.dtd">
<validators>
    <field name="name">
        <field-validator type="required">
            <message>Please enter a user name</message>
        </field-validator>
    </field>
    <field name="comment">
        <field-validator type="required">
            <message>Please enter your message</message>
        </field-validator>
    </field>
</validators>

If I do not add validation then all is working as it should.

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.devMode" value="true" />

  <constant name="struts.custom.i18n.resources" value="ApplicationResources" />

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

        <global-results>
            <result name="Exception">/error404.jsp</result>
        </global-results>

        <global-exception-mappings>
            <exception-mapping exception="java.lang.Throwable" result="Exception" />
        </global-exception-mappings>

        <!-- loads page -->
        <action name="main" 
            class="com.user.action.LoginAction" method="main">
            <result name="success">/main.jsp</result>
        </action>

        <!-- mail action -->
        <action name="mail" class="com.user.action.LoginAction" method="mail" >
            <result name="success">/main.jsp</result>
        </action>       

  </package>
</struts>

Upvotes: 2

Views: 1407

Answers (1)

Andrea Ligios
Andrea Ligios

Reputation: 50203

INPUT is one of the predefined results made available by Struts2;

Basically, if your Action sends parameters that are incorrect (conversion errors, like if you send a "abc" to an Integer Action variable), or that don't pass the validation, the Workflow Interceptor return the INPUT result and follow the path specified in the struts configuration for that Action.

Your problem is that you have not defined any INPUT result for your Actions, while you always should.

You can also set a global input result as a fallback, but be careful with that... generally, the page you want to go in case of an INPUT is the same from where the request has been sent.

In case of an INPUT result, your Action method (eg. execute()) is not executed, so if you load common data in that method, like select boxes content, it won't be available anymore.

Read this answers to fully understand what this implies and how to make it work:

Upvotes: 1

Related Questions