Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

setters and getters for field in customTag question

 
Denis Yuzvyk
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi , i have question :
My custom tag has field
private Locale locale;
//and getters ,setters for it
public Locale getlocale(){return locale;}
public void setlocale(Locale l ) { locale = l;}

tdl:
<attribute>
<name>locale</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
in jsp page i have:
<prefix:tag locale="ua/UA" > body </prefix:tag>
I wont that that tag create locale with given string ....
but for thes i need setter with such signature :
public void setLocale (String loc){ locale = new Locale(loc);}
but with such signature Tamcat says:
Unable to find setter method for attribute: locale

//another example


foe example ,if i have field:
private int number;
//so why with this setter in tomcat work ok
setNumber(int i){
munber = i;
}
//and with this setter is error :
//cannot find setter for field : number
setNumber(String str){
number= Integer.parseInt(str);
}


how to deal with this ?


Thx.
 
M. Gagnon
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It looks like the parameter type in the setter method and the type for the corresponding data member have to be the same (or perhaps compatible types in an inheritance hierarchy). From an example in ProJSP by Apress (excellent JSP book, by the way):

"... the instance variable and the setter method are defined to take Collection rather than String, and at request time the setter method will be called with a Collection of ... objects." The author hasn't stated explicitly that the rule is to match type, but he's kind of implying that. And yes, you can pass an object other than type String. I've done it.

How about:

<yourLib:yourTag yourAttr="<%= new Locale(yourString) %>" />
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic