On the boolean problem of getting action for JSP under STRUTS2 framework

Source: Internet
Author: User

On the boolean problem of getting action for JSP under STRUTS2 framework

--------------------------------------------------------------------------------------------------------------- -----

Problem: The Boolean value in action cannot be evaluated

Cause: The Boolean variable is automatically generated in the MyEclipse get and set methods will change, after the change cannot correspond to the STRUTS2 assignment law

Workaround: Modify the Get and set methods of the Boolean variable according to the assignment rule of struts, see below for specific modification;

--------------------------------------------------------------------------------------------------------------- --------

When I was using the STRUTS2 framework, I encountered a problem where the JSP could not get a Boolean value from the action, as follows:

One: Define the Person object and automatically generate the get and set methods as follows:

1 Importjava.io.Serializable;2 3  Public classPersonbeanImplementsSerializable {4 5     PrivateString Idcard;6     PrivateString personname;7     PrivateString Personsex;8     Private intpersonage;9     Private BooleanIsmarry;Ten  One      PublicString Getidcard () { A         returnIdcard; -     } -  the      Public voidSetidcard (String idcard) { -          This. Idcard =Idcard; -     } -  +      PublicString Getpersonname () { -         returnPersonName; +     } A  at      Public voidsetpersonname (String personname) { -          This. PersonName =PersonName; -     } -  -      PublicString Getpersonsex () { -         returnPersonsex; in     } -  to      Public voidsetpersonsex (String personsex) { +          This. Personsex =Personsex; -     } the  *      Public intGetpersonage () { $         returnpersonage;Panax Notoginseng     } -  the      Public voidSetpersonage (intpersonage) { +          This. personage =personage; A     } the  +      Public BooleanIsmarry () { -         returnIsmarry; $     } $  -      Public voidSetmarry (BooleanIsmarry) { -          This. Ismarry =Ismarry; the     } - Wuyi      the}

Second, the corresponding action, as follows:

1 Importjava.util.List;2 ImportTop.malloc.pojo.PersonPOJO;3 ImportCom.opensymphony.xwork2.ActionSupport;4 5  Public classCityactionextendsactionsupport{6     PrivateList persons;7     8      PublicList getpersons () {9         returnpersons;Ten     } One  A      Public voidsetpersons (List persons) { -          This. Persons =persons; -     } the      -      PublicString person ()throwsexception{ -persons=NewPersonpojo (). Getperson (); -         return"Success"; +     } -  +}
View Code

Third, the corresponding jsp,body omitted

<table border= "3" >
<tr>
<td> name </td><td> sex </td><td> Age </td><td> identification number </td><td> marital status </td>
</tr>
<s:iterator value= "Persons" var= "per" >
<tr>
<td><s:property value= "#per. PersonName"/></td>
<td><s:property value= "#per. Personsex"/></td>
<td><s:property value= "#per. personage"/></td>
<td><s:property value= "#per. Idcard"/></td>
<td><s:if test= "#per. Ismarry==true" > married </s:if>
<s:elseif test= "#per. Ismarry==false" > Unmarried </s:elseif></td>
</tr>
</s:iterator>
</table>

Problem: The Boolean value in action cannot be evaluated, look closely at the code, and find that the problem is on the get and set methods of the Boolean value, and the automatically generated get and set methods cannot follow the assignment rules of struts

The error code is as follows:

      Public Boolean Ismarry () {      return  ismarry;     }        Public void Setmarry (boolean  ismarry) {         this. Ismarry= ismarry;     } 

After modification:

         Public Boolean Getismarry () {        return  ismarry;    }      Public void Setismarry (boolean  ismarry) {        this. Ismarry= ismarry;    }

Running results such as

On the boolean problem of getting action for JSP under STRUTS2 framework

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.