关于Struts2框架下jsp获取action的布尔值问题

关于Struts2框架下jsp获取action的布尔值问题

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

问题:action中的布尔值无法取值

原因:boolean型变量在MyEclipse自动生成get和set方法会发生改变,改变之后无法对应Struts2的赋值规律

解决办法:按照Struts的赋值规律修改boolean变量的get和set方法,具体修改请看下文;

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

当我在使用Struts2框架时,遇到jsp无法从action中获取布尔值的问题,情况如下:

一:定义person对象,自动生成get和set方法,如下:

 1 import java.io.Serializable;
 2 
 3 public class PersonBean implements Serializable {
 4 
 5     private String idCard;
 6     private String personName;
 7     private String personSex;
 8     private int personAge;
 9     private boolean isMarry;
10 
11     public String getIdCard() {
12         return idCard;
13     }
14 
15     public void setIdCard(String idCard) {
16         this.idCard = idCard;
17     }
18 
19     public String getPersonName() {
20         return personName;
21     }
22 
23     public void setPersonName(String personName) {
24         this.personName = personName;
25     }
26 
27     public String getPersonSex() {
28         return personSex;
29     }
30 
31     public void setPersonSex(String personSex) {
32         this.personSex = personSex;
33     }
34 
35     public int getPersonAge() {
36         return personAge;
37     }
38 
39     public void setPersonAge(int personAge) {
40         this.personAge = personAge;
41     }
42 
43     public boolean isMarry() {
44         return isMarry;
45     }
46 
47     public void setMarry(boolean isMarry) {
48         this.isMarry = isMarry;
49     }
50 
51     
52 }

二、对应的action,如下:

 1 import java.util.List;
 2 import top.malloc.pojo.PersonPOJO;
 3 import com.opensymphony.xwork2.ActionSupport;
 4 
 5 public class CityAction extends ActionSupport{
 6     private List persons;
 7     
 8     public List getPersons() {
 9         return persons;
10     }
11 
12     public void setPersons(List persons) {
13         this.persons = persons;
14     }
15     
16     public String person() throws Exception{
17         persons=new PersonPOJO().getPerson();
18         return "success";
19     }
20 
21 }
View Code

三、相应的jsp,body省略

<table border="3">
      <tr>
     <td> 姓名</td><td>性别</td><td>年龄</td><td>身份认证号</td><td>婚姻状况</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">已婚</s:if>
            <s:elseif test="#per.isMarry==false">未婚</s:elseif></td>
     </tr>
     </s:iterator>
     </table>

出现的问题:action中的布尔值无法取值,仔细查看代码,发现问题出在布尔值的get和set方法上,自动生成的get和set方法无法遵循Struts的赋值规则

错误代码如下:

     public boolean isMarry() {
      return isMarry;
     }
 
     public void setMarry(boolean isMarry) {
         this.isMarry = isMarry;
     }

修改之后:

        public boolean getIsMarry() {
        return isMarry;
    }

    public void setIsMarry(boolean isMarry) {
        this.isMarry = isMarry;
    }

运行结果如上图

 

posted @ 2015-08-19 18:30  疯狂麦洛  阅读(1221)  评论(0编辑  收藏  举报