Hi Everyone,
I am new to Struts 2 and trying to make the switch from Struts 1.
I cannot figure out how to get the validation to display the
messages/errors in the input form with the use of the s:actionerror or
s:actionmessage tags. It never outputs anything despite the validation
appearing to work.
I have searched and searched and cannot find an example of this
anywhere. I simple want the user to be directed back to the form if
there are errors and display all errors at the top of the form.
What I have below is displaying field specific error message beside
the field in the form with struts 2 tags ... which is not what I need.
I am using Struts 2.1.8.
I have the following in my jsp.
errors
<s:actionerror />
messages
<s:actionmessage />
<h2>Form with Struts 2 tags.</h2>
<s:form action="simpleAction" validate="true">
<s:textfield label="firstname" name="firstname"></s:textfield>
<s:submit label="Save" name="Save"></s:submit>
</s:form>
<h2>Form without Struts 2 tags.</h2>
<form action="<s:url action="simpleAction"/>" method="post">
<input type="text" name="firstname"><br>
<input type="submit" value="Save"/>
</form>
And my action class:
public class SimpleAction extends ActionSupport {
static Logger logger = Logger.getLogger(SimpleAction.class);
private String firstname;
public String getFirstname() {
return this.firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String execute() throws Exception {
logger.debug("execute SimpleAction");
return SUCCESS;
}
}
And my struts.xml
<action name="simpleAction" class="com.myapp.action.SimpleAction">
<result name="success">/WEB-INF/jsp/simpleActionSuccess.jsp</result>
<result name="input">/WEB-INF/jsp/simpleActionForm.jsp</result>
</action>
And my SimpleAction-validation.xml which is in the same directory as
the action class.
<!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="firstname">
<field-validator type="requiredstring">
<message>You must enter a firstname</message>
</field-validator>
</field>
</validators>
Cheers,
Carl.
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscr...@struts.apache.org
For additional commands, e-mail: user-h...@struts.apache.org