又是书上例子的错误,在jsp用标签getProperty中的属性名字必须是jb的一个私有变量吗?
做了一个显示离千禧年有多少天的jb:
源代码如下:
package com.wrox.counter;
import java.util.*;
import java.text.SimpleDateFormat;
public class CounterBean{
private Date curDate;
private SimpleDateFormat dateFormat;
private GregorianCalendar targetDate;
private String name;
public CounterBean(){
GregorianCalendar currentDate=new GregorianCalendar();
curDate=(Date)currentDate.getTime();
dateFormat=new SimpleDateFormat("EEE,dd MMMM yyyy");
targetDate=new GregorianCalendar();
targetDate.set(targetDate.YEAR,3000);
targetDate.set(targetDate.MONTH,0);
targetDate.set(targetDate.DATE,1);
targetDate.set(targetDate.AM_PM,0);
targetDate.set(targetDate.HOUR,0);
targetDate.set(targetDate.MINUTE,0);
targetDate.set(targetDate.SECOND,0);
name="the new millennium";
}
public String getTodaysDate(){
return dateFormat.format(curDate);
}
public void setTargetYear(int year){
targetDate.set(targetDate.YEAR,year);
}
public void setTargetMonth(int month){
targetDate.set(targetDate.MONTH,month);
}
public void setTargetDate(int date){
targetDate.set(targetDate.DATE,date);
}
public void setTargetAmPm(int ampm){
targetDate.set(targetDate.AM_PM,ampm);
}
public void setTargetHour(int hour){
targetDate.set(targetDate.HOUR,hour);
}
public void setTargetMinute(int minute){
targetDate.set(targetDate.MINUTE,minute);
}
public void setTargetSecond(int second){
targetDate.set(targetDate.SECOND,second);
}
public void setTargetEvent(String eventName){
name=eventName;
}
public String getTargetEvent(){
return name;
}
public String getMessage(){
Date millDate=(Date)targetDate.getTime();
int dateTest=millDate.compareTo(curDate);
switch(dateTest){
case 1:
long millisecs=(millDate.getTime())-(curDate.getTime());
long msInDay=1000*60*60*24;
long daysToGo=(long)(millisecs/msInDay);
return("only "+daysToGo+" days to go until "+name+"!!!");
case 0:
return("welcome to the new millenium!!!");
case -1:
return("sorry,counter has expired");
default:
return("counter error");
}
}
}
然后在jsp页面里如下调用:
<jsp:useBean id="counter" scope="page" class="com.wrox.counter.CounterBean"/>
<p>the current date is
<jsp:setProperty name="counter" property="todaysDate"/></p>
<p>
<jsp:getProperty name="counter" property="message"/>
</p>
当运行时获得当今日期时(getTodaysDate),为什么说找不到todaysdate这个属性呢?的确定义了这个方法了。
Can't find a method to write property 'todaysDate' of type 'java.lang.String' in a bean of type 'com.wrox.counter.CounterBean'