This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
The moose likes JSP and the fly likes setters and getters for field in customTag question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "setters and getters for field in customTag question" Watch "setters and getters for field in customTag question" New topic

setters and getters for field in customTag question

Denis Yuzvyk

Joined: Dec 06, 2004
Posts: 22
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

Joined: Feb 18, 2004
Posts: 68
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) %>" />
I agree. Here's the link:
subject: setters and getters for field in customTag question
It's not a secret anymore!