How to display a validation message in a specific <div> element in Struts2
I am new to Struts 2 and run into this problem while maintaining my page layout:
<s:form action="abc.action"><br> <s:textfield key="name" label="Name" /><%--here I need to display errormessage for `name`--%> <br> <s:textfield key="email" label="Email" /><%--here I need to display errormessage for `email`--%> <br> <s:submit> </s:form> I am using xml-validator for my action class, this works fine. but validation error messages are displayed above the field and the text field. but I want it to appear after the corresponding text box (inside another html container). Please inform.
If you're used to writing HTML, switch to a simple theme.
In struts.xml, probably the best place:
<struts> <constant name="struts.ui.theme" value="simple" /> </struts> Then just use the fielderror tag to put the error in the field you want.
Familiar with Struts2 tags: http://struts.apache.org/release/2.3.x/docs/tag-reference.html
This is the default value according to the default Struts2 pattern. To change it, see http://www.mkyong.com/struts2/working-with-struts-2-theme-template/
You can also use the Struts2 Validation Framework.
Framework validation comes with a set of useful procedures for automatic form validation and can handle both server-side and client-side validation . If any validation does not exist, you can create your own validation logic by implementing the java interface.
com.opensymphony.xwork2.Validator Validator uses XML configuration files to determine which validation procedures should be installed and how they should be applied to a given application. The validators.xml file contains all common validator declarations. If validators.xml is not in the classpath, the validation file is loaded from the path by default
com/opensymphony/xwork2/validator/validators/default.xml Validator Area There are two types of validators in the Struts2 validation platform.
- Field validators
- Non-polarity validators
Field validators , as indicated in the name, act on the individual fields available through the action. On the contrary, the validator is more general and can perform checks in the context of a complete action that includes a rule of several fields (or even without a field at all). Most validations can be defined for each field. This should be preferable, if possible, when checking a non-field, since validation validation messages are tied to the corresponding field and will be presented next to the corresponding input element in the corresponding view.
<validators> <field name="bar"> <field-validator type="required"> <message>You must enter a value for bar.</message> </field-validator> </field> </validators> Fieldless validators only add activity level messages. Non-field validators are mainly domain-specific and therefore offer custom implementations. The most important standard non-field validator provided by XWork is ExpressionValidator.
<validators> <validator type="expression"> <param name="expression">foo lt bar</param> <message>Foo must be greater than Bar.</message> </validator> </validators> For more information, visit this link struts2 check link