Integer re-display after struts form verification fails

Source: Internet
Author: User

Through the project exercise completed by Wang Tao, I found a problem that is re-displayed after the Form Verification in struts fails: assume that the actionform bean has an integer type attribute, if it is defined as int, the default value of this attribute is 0. You can use a text box on the form page to enter the value of this attribute,When no content is entered in the text box,The parameter value sent to the Web server is an empty string "";When other attributes in actionform bean fail to be verified, return to the form page and re-display it. At this time, the content displayed in the text box corresponding to the integer field is 0, instead of the original one (without any content ).
Therefore, we have thought of the method used in spring MVC: Define the integer type attribute as the integer type. In this way, the default value of this attribute is null, with the help of <HTML: property> can display null as a Null String. However, when other attributes in actionform bean fail to be verified and returned to the form page for re-display, the content displayed in the text box corresponding to the integer type property is still 0. This indicates that when struts assembles the request parameters into the actionform bean object, it will also set the Request Parameters whose content is an empty string "" (This occurs when no content is entered in the text box.) assemble it into actionform bean and convert the Null String "" into an integer with a value of 0. In this case, we thought of a compromise solution to design the attribute originally belonging to an integer as a string type, but we thought it was not ideal, I want to know how experienced friends can handle this situation? I would like to ask you.

The following is a complete description of the problem:
Suppose there is a form for registering a course, which contains fields such as "Course name" and "Credits". Obviously, "Course name" is of the string type, and "Credits" is of the integer type, the actionform corresponding to this form can be designed as follows:
Public class registercourseform extends actionform
{
Private string name;

Public String getname ()
{Return name;
}

Public void setname (string name ){
This. Name = Name;
}

Private integer score;

Public integer getscore ()
{
Return score;
}
Public void setscore (integer score)
{
This. Score = score;
}

Public actionerrors validate (actionmapping mapping, httpservletrequest request)
{
If ("". Equals (name ))
{
Actionerrors errors = new actionerrors ();
Errors. Add ("name", new actionmessage ("required. Name "));
Return errors;
}
Return NULL;
}

}
The main content of the registration course form page is as follows:
<% @ Taglib uri = "http://jakarta.apache.org/struts/tags-bean" prefix = "Bean" %>
<% @ Taglib uri = "http://jakarta.apache.org/struts/tags-html" prefix = "html" %>
 
<HTML: Form Action = "/registercourse">
Name: <HTML: Text property = "name"/> <HTML: errors property = "name"/> <br/>
Score: <HTML: Text property = "score"/> <HTML: errors property = "score"/> <br/>
<HTML: Submit/>
</Html: Form>

To make this use case run and simplify the code of the entire program, we directly use the forwardaction provided by struts. You can also use myeclipse to automatically create actions without any business logic for us. The configuration snippet in the struts-config.xml file is as follows:
<Form-bean name = "registercourseform" type = "cn. itcast. Form. registercourseform"/>
......
<Action parameter = "/WEB-INF/JSP/registercourse. jsp"
Path = "/Index" type = "org. Apache. Struts. Actions. forwardaction"/>

<Action
Input = "/WEB-INF/JSP/registercourse. jsp"
Name = "registercourseform"
Path = "/registercourse"
Scope = "request"
Type = "org. Apache. Struts. Actions. forwardaction"
Parameter = "/WEB-INF/JSP/registercourse. jsp"
/>

(1) first, enter http: // localhost: 8080/coursemanager/index. Do in the address bar of the browser, and submit the request to the first action for processing. The registercourse. jsp page is displayed in the browser;
(2) directly submit the form without entering any content on the registration form page. At this time, the form verification fails (that is, registercourseform. the validate method returns the actionerrors object), and the browser re-displays the registercourse. JSP page. Registercourse. in addition to the error description, the JSP page also displays the last entered content in the form. The problem we encountered was that the last time we submitted the form, we did not score) enter any content in the text box, but a 0 value is added to the score text box this time.
I guess this is because when struts assembles request parameters into the registercourseform object, it splits the Request Parameters of the Null String "" (This happens when no content is entered in the text box) it is converted into integer data with 0 values. View the source code of struts and learn that the method calling process of assembling request parameters into formbean is as follows:
Actionservlet. doget/dopost --> actionservlet. Process --> requestprocessor. Process -->
Requestprocessor. processactionform, requestprocessor. processpopulate --> requestutils. populate -->
Beanutils. populate --> beanutilsbean. setproperty (Object bean, string name, object value)

It can be seen that beanutilsbean is finally called when the request parameters are assembled into the formbean attributes. the setproperty method may be caused by beanutilsbean. the setproperty method converts an empty string "" to 0 when assigning values to an integer attribute of JavaBean. Beanutilsbean class is located in the Commons-beanutils package of Apache, you can find the commons-beanutils.jar package and related dependent packages from the lib directory of the struts installation package. I arranged for Wang Tao to write a program to verify the effect:

Package com. Tony;
Public class testinteger {

Public static void main (string [] ARGs ){

Student = new student ();
Try {
Beanutilsbean. getinstance (). setproperty (student, "name", "Tony ");
Beanutilsbean. getinstance (). setproperty (student, "Age ","");
System. Out. Print (student );
} Catch (exception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
}
}
}

Package com. Tony;
Public class student {
Private string name;
Private integer age;
Public integer getage (){
Return age;
}
Public void setage (integer age ){
This. Age = age;
}
Public String getname (){
Return name;
}
Public void setname (string name ){
This. Name = Name;
}
Public String tostring ()
{
Return this. Name + ":" + this. Age;
}
}
The final output result of the program is Tony: 0, which indicates that the beanutilsbean. setproperty method actually converts the Null String "" to the integer type 0.

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.