Win a copy of Java Mock Exams (software) this week in the Programmer Certification (OCPJP) forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

setters and getters for field in customTag question

Denis Yuzvyk
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;}

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 ?

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) %>" />
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!