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