struts2/jsp 通过form表单方式或者js的方式提交请求,封装boolean值的问题

struts2 通过form表单方式或者js的方式提交请求,封装boolean值的问题

在jsp上的代码
<form name="searchform" action="">
 <input id="searchInfo.isHighSearch" name="searchInfo.isHighSearch" value="true"

type="hidden" />

然后通过struts2把参数封装在vo bean中,bean的类名是SearchInfo.java
该类中有个属性

 

private boolean isHighSearch;


用myeclipse 6.0自动生成的set/get方法是
对应的set/get方法是

 

 public boolean isHighSearch() {
  return isHighSearch;
 }

 public void setHighSearch(boolean isHighSearch) {
  this.isHighSearch = isHighSearch;
 }

 

然后在action中

SearchInfoBean searchInfoBean = new SearchInfoBean();
System.out.println(searchInfoBean.isHighSearch());

打印出来的值总是false,和表单的值不一致


后来才发现myelipse自动生成的get方法,boolean类型的,自动在属性名加上前缀"is",
上面的例子是isHighSearch,

 

解决的办法是把属性名字改为highSearch,去掉前面的is,然后就可以正确获得highSearch的值了

 jsp页面改为

<input id="searchInfo.highSearch" name="searchInfo.highSearch" value="true" type="hidden" />

 

bean类的属性改为

private boolean highSearch;

 

 public boolean isHighSearch() {
  return highSearch;
 }

 public void setHighSearch(boolean highSearch) {
  this.highSearch = highSearch;
 }

 

总结出,javabean的属性命名,如果是boolean类型的属性,不要命名为is开头,这样会避免一些不必要

的错误

你可能感兴趣的:(jsp,bean,MyEclipse,struts,input,action)