又是书上例子的错误,在jsp用标签getProperty中的属性名字必须是jb的一个私有变量吗?

rollrock 2004-05-01 11:21:25
做了一个显示离千禧年有多少天的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'
...全文
44 8 点赞 打赏 收藏 举报
写回复
8 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
bokei 2004-05-06
如果要SetProperty那么就必须要有setXXX,如果要GetProperty就必须要有getXXX,如果只允许读取,不允许设置,那么就不需要有setXXX,这时候这个属性就是“只读”
  • 打赏
  • 举报
回复
rollrock 2004-05-05
哦,是的,这是个小失误,不过,更重要的问题是后者,属性必须要有setXXX,message没有系统未报错,todaysDate没有就报错,说必须得有一个“写”方法,这又是为什么呢?
  • 打赏
  • 举报
回复
bokei 2004-05-05
问题在这里:
<jsp:setProperty name="counter" property="todaysDate"/>
这里用的是setProperty,只是设置Bean的属性,而不是读取Bean的属性,所以没有显示是正确的。想显示的话改成用getProperty。
先说一下setProperty和getProperty。象这样:
<jsp:setProperty name="counter" property="todaysDate"/>的时候,会调用counter的setTodaysDate方法。
<jsp:getProperty name="counter" property="message"/>的时候,会调用counter的getMessage方法。
其实就是用setProperty的时候就会调用setXXX方法,用getProperty的时候就是用getXXX方法。

  • 打赏
  • 举报
回复
wafeng 2004-05-04
关注
  • 打赏
  • 举报
回复
rollrock 2004-05-03
help me
  • 打赏
  • 举报
回复
fuzuyuan 2004-05-02
你写的类里面,并没有todaysDate这个私有变量,也没有给它set方法,当然会提示错误了!
下面是正确的代码
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";
}
private String todaysDate;/*要加上这个属性和下面的set方法*/
public void setTodaysDate(String date){
this.todaysDate=date;
}
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");
}
}
}
  • 打赏
  • 举报
回复
rollrock 2004-05-02
我用了你的方法,可以运行,但结果还是不对:
the current date is
only 363659 days to go until the new millennium!!!
而正确的结果是is后面应该是当前的日期,并且,message也没有setMessage,系统却为什么未报错,而todaysdate不set却报错,真是令我郁闷
  • 打赏
  • 举报
回复
rollrock 2004-05-02
to fuzuyuan:
标签里用的属性必须是私有变量吗?
  • 打赏
  • 举报
回复
相关推荐
发帖
Web 开发
加入

8.0w+

社区成员

Java Web 开发
申请成为版主
帖子事件
创建了帖子
2004-05-01 11:21
社区公告
暂无公告