You can just use an attribute of type Object and dynamically check if it's String or Locale or whatever.
JSP tag attributes with different value types
Question
Is it possible to have a JSP tag with different value types for its attributes?
<tag>
<name>init</name>
<tag-class>com.example.InitTag</tag-class>
<body-content>empty</body-content>
<attribute>
<name>locale</name>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
public class InitTag extends SimpleTagSupport {
private Locale locale;
public InitTag() {
setLocale(Locale.getDefault());
}
public void setLocale(String locale) {
setLocale(SetLocaleSupport.parseLocale(locale));
}
public void setLocale(Locale locale) {
this.locale = locale;
}
}
Now I would like to be able to use a Locale object as well as a String object as attribute value:
<mytag:init locale="en" />
or
<mytag:init locale="${anyLocaleObject}" />
But getting this exception: org.apache.jasper.JasperException: Unable to convert string "en" to class "java.util.Locale" for attribute "locale": Property Editor not registered with the PropertyEditorManager
Do I have to use this mentioned "Property Editor"? How to use it then?
Solution
OTHER TIPS
How about this
<tag>
<name>init</name>
<tag-class>com.example.InitTag</tag-class>
<body-content>empty</body-content>
<attribute>
<name>localeCode</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>locale</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
public class InitTag extends SimpleTagSupport {
private Locale locale;
public InitTag() {
setLocale(Locale.getDefault());
}
public void setLocaleCode(String locale) {
setLocale(SetLocaleSupport.parseLocale(locale));
}
public void setLocale(Locale locale) {
this.locale = locale;
}
}
In JSP
<mytag:init localeCode="en" />
OR
<mytag:init localeCode="{anyLocaleCode}" />
OR
<mytag:init locale="${anyLocaleObject}" />
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow