How to customize error message display format/style after Struts2 form validation

Source: Internet
Author: User
Struts 2.1.8.1 + myeclipse 8.6 + Tomcat 7.0.2 premises

Please build a good Struts2 Web project, this should be very simple, not in the narrative, this article is mainly to do is how to display the error message struts, then here is an action class to simulate the resulting error. Take a look at the action class below. (source code will be provided in the following)

public class displayerrorinfoaction extends actionsupport

{

    private static final long serialversionuid = -2690064846056775963l;

 

    @Override

    Public String execute () throws Exception

    {

      /**

         * Add some fielderror for testing, please write Validater method

        */

If necessary        this . Addfielderror ("Displayerrorinfo", "error! Please check it. ");

       this . Addfielderror ("User.username", "wrong name");

       return INPUT;

   }

}

Programme The first scenario: use the content of the OGNL stack (valuestack) (emphasis recommended)

The value of errors and fielderrors in the value Stack is obtained primarily through OGNL. Here first to introduce the next S:debug label.

The Debug tab is primarily used for auxiliary testing, which generates a hyperlink on the page that allows you to view all the value information in the Valuestack and stack context. can obtain the corresponding value through the OGNL s:property (Smooth reminder: S:debug use please put in the s:form outside, put in inside has what effect, please everybody try to know. Enclose the code used in this case.

Note: Please be aware that the error code contains '. ' (similar to the User.username) method of taking values:

<s:property value= "errors[' User.username '][0]"/> and <s:property value= "fielderrors[' user.username '][0 ']" >

(On-line "Struts2 Property Label value Attribute" solution)


<body>

<s:form action= "Displayerrorinfo" method= "POST" theme= "simple" >

<H1 align= "center" style= "Color:blue" >

Displayerrorinfoforstruts2demo1 (use OGNL to get the content of the stack, recommended)

<H2 dir= "RTL" >

Smooth Finishing

1.1.All Errorinfos (MAP): &nbsp;<s:property value= "Errors"/><br>

1.2.All Fielderrorsinfos (MAP): &nbsp;<s:property value= "Fielderrors"/><br>

2.1.DisplayErrorInfo in errors (errors.displayerrorinfo[0]):<s:textfield/>

<font color= "Red" >

<s:property value= "errors.displayerrorinfo[0]"/>

</font><br>

2.2.DisplayErrorInfo in errors (errors[' Displayerrorinfo '][0]):<s:textfield/>

<font color= "Red" >

<s:property value= "errors[' Displayerrorinfo '][0]"/>

</font>

<br>

&nbsp;&nbsp;<font color= "Green" > Note: When you get the value, you can customize it in your own format </font>

<br>

2.3.DisplayErrorInfo in errors (generally not used to [1], here is only testing):<s:textfield/>

<font color= "Red" >

<s:property value= "errors.displayerrorinfo[1]"/>

</font>

<br>

3.1.DisplayErrorInfo in Fielderrors (fielderrors.displayerrorinfo[0]):<s:textfield/>

<font color= "Red" >

<s:property value= "fielderrors.displayerrorinfo[0]"/>

</font>

<br>

3.2.DisplayErrorInfo in Fielderrors (fielderrors[' Displayerrorinfo '][0]):<s:textfield/>

<font color= "Red" >

<s:property value= "fielderrors[' Displayerrorinfo '][0]"/>

</font>

<br>

&nbsp;&nbsp;<font color= "Green" > Note: It is recommended to use Fielderrors value, which is used in the action this.addfielderror</font>

<br>

4.0.displayerrorinfo-user.username (correct expression. errors[' User.username '][0]):<s:textfield/>

<font color= "Red" >

<s:property value= "errors[' User.username '][0]"/>

</font>

<br>

4.1.displayerrorinfo-user.username (correct expression. fielderrors[' User.username '][0]):<s:textfield/>

<font color= "Red" >

<s:property value= "fielderrors[' User.username '][0]"/>

</font>

<br>

<font color= "Blue" >

4.2.displayerrorinfo-user.username (wrong expression 1.errors.%{user.username}[0]):<s:textfield/>

<font color= "Red" >

<s:property value= "errors.%{user.username}[0]"/>

</font>

<br> 4.3.displayerrorinfo-user.username (wrong expression 2.errors.# {user.username}[0]):<s:textfield/>

This method itself has a problem with the language, please see the specification of JSP specification

<br> 4.4.displayerrorinfo-user.username (wrong expression 3.%{errors.user.username}[0]):<s:textfield/>

<font color= "Red" >

<s:property value= "%{errors.user.username}[0]"/>

</font>

<br> 4.5.displayerrorinfo-user.username (wrong expression 4.errors.user.username):<s:textfield/>

<font color= "Red" >

<s:property value= "Errors.user.username"/>

</font>

<br>

</font>

</s:form>

<s:debug/>

</body>

The effect is as follows:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.