在JavaBean的setter方法中,要求setXXX(ClassType xXX)的参数与XXX变量的类型ClassType一致,否则报错:
Cannot find a method to write property [类型] of type [类型] in a bean of type
Eg:举一个错例
(1)JavaBean定义:(注意setter方法是错的)
package com.question;
public class Demo {
private int index;
public void setIndex(String index) { // 注意:正确的参数应该是int index
this.index = 100; // 这里不准备用传入的参数赋值
}
public int getIndex() {
return index;
}
}
(2)在jsp页面使用JavaBean:(这里是正确写法)
<jsp:useBean id="demo" class="com.question.Demo"/>
<jsp:setProperty name="demo" property="index" value="4444"/>
<jsp:getProperty name="demo" property="index"/>
(3)运行报错:
Type Exception Report
Message Cannot find a method to write property [index] of type [int] in a bean of type [com.question.Demo]
Description The server encountered an unexpected condition that prevented it from fulfilling the request.
(4)解决方法:
将JavaBean的setter方法修正即可