Java 具有不同值类型的JSP标记属性
是否可以为属性使用不同值类型的JSP标记Java 具有不同值类型的JSP标记属性,java,jsp,jsp-tags,Java,Jsp,Jsp Tags,是否可以为属性使用不同值类型的JSP标记 <tag> <name>init</name> <tag-class>com.example.InitTag</tag-class> <body-content>empty</body-content> <attribute> <name>locale</name> <
<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;
}
}
初始化
com.example.InitTag
空的
场所
真的
公共类InitTag扩展了SimpleTagSupport{
私人场所;
公共InitTag(){
setLocale(Locale.getDefault());
}
公共void setLocale(字符串区域设置){
setLocale(SetLocaleSupport.parseLocale(locale));
}
公共void setLocale(区域设置){
this.locale=locale;
}
}
现在,我希望能够使用Locale对象和String对象作为属性值:
<mytag:init locale="en" />
or
<mytag:init locale="${anyLocaleObject}" />
或
但出现此异常:org.apache.jasper.jaspereException:无法将字符串“en”转换为属性“Locale”的类“java.util.Locale”:属性编辑器未向PropertyEditorManager注册
我必须使用上面提到的“属性编辑器”吗?那么如何使用它呢?您可以只使用Object类型的属性,动态检查它是String还是Locale或其他什么。您可以只使用Object类型的属性,动态检查它是String还是Locale或其他什么。您可以只使用Object类型的属性,动态检查它是String还是Locale或其他什么不管怎样。您只需使用Object类型的属性,并动态检查它是字符串还是区域设置或其他任何属性。这怎么样
<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;
}
}
初始化
com.example.InitTag
空的
本地代码
假的
真的
场所
假的
真的
公共类InitTag扩展了SimpleTagSupport{
私人场所;
公共InitTag(){
setLocale(Locale.getDefault());
}
公共void setLocaleCode(字符串区域设置){
setLocale(SetLocaleSupport.parseLocale(locale));
}
公共void setLocale(区域设置){
this.locale=locale;
}
}
在JSP中
<mytag:init localeCode="en" />
OR
<mytag:init localeCode="{anyLocaleCode}" />
OR
<mytag:init locale="${anyLocaleObject}" />
或
或
这个怎么样
<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;
}
}
初始化
com.example.InitTag
空的
本地代码
假的
真的
场所
假的
真的
公共类InitTag扩展了SimpleTagSupport{
私人场所;
公共InitTag(){
setLocale(Locale.getDefault());
}
公共void setLocaleCode(字符串区域设置){
setLocale(SetLocaleSupport.parseLocale(locale));
}
公共void setLocale(区域设置){
this.locale=locale;
}
}
在JSP中
<mytag:init localeCode="en" />
OR
<mytag:init localeCode="{anyLocaleCode}" />
OR
<mytag:init locale="${anyLocaleObject}" />
或
或
这个怎么样
<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;
}
}
初始化
com.example.InitTag
空的
本地代码
假的
真的
场所
假的
真的
公共类InitTag扩展了SimpleTagSupport{
私人场所;
公共InitTag(){
setLocale(Locale.getDefault());
}
公共void setLocaleCode(字符串区域设置){
setLocale(SetLocaleSupport.parseLocale(locale));
}
公共void setLocale(区域设置){
this.locale=locale;
}
}
在JSP中
<mytag:init localeCode="en" />
OR
<mytag:init localeCode="{anyLocaleCode}" />
OR
<mytag:init locale="${anyLocaleObject}" />
或
或
这个怎么样
<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;
}
}
初始化
com.example.InitTag
空的
本地代码
假的
真的
场所
假的
真的
公共类InitTag扩展了SimpleTagSupport{
私人场所;
公共InitTag(){
setLocale(Locale.getDefault());
}
公共void setLocaleCode(字符串区域设置){
setLocale(SetLocaleSupport.parseLocale(locale));
}
公共void setLocale(区域设置){
this.locale=locale;
}
}
在JSP中
<mytag:init localeCode="en" />
OR
<mytag:init localeCode="{anyLocaleCode}" />
OR
<mytag:init locale="${anyLocaleObject}" />
或
或
我可以,但是有没有任何干净和安全的方法?@dtrunk我不知道,也许有人能回答得更好。您是否尝试使用字符串属性并检查区域设置是否将转换为字符串?虽然它不会比Object更安全,是的,我做到了。它工作得很好。我会等一天,如果没有更好的答案,我会接受你的答案。我可以,但没有任何干净和打字安全的方法吗?@dtrunk我不知道,也许有人能回答得更好。您是否尝试使用字符串属性并检查区域设置是否将转换为字符串?虽然它不会比Object更安全,是的,我做到了。它工作得很好。我会等一天,如果没有更好的答案,我会接受你的答案。我可以,但没有任何干净和打字安全的方法吗?@dtrunk我不知道,也许有人能回答得更好。您是否尝试使用字符串属性并检查区域设置是否将转换为字符串?虽然它不会比Object更安全,是的,我做到了。它工作得很好。我会等一天,如果没有更好的答案,我会接受你的答案。我可以,但没有任何干净和打字安全的方法吗?@dtrunk我不知道,也许有人能回答得更好。您是否尝试使用字符串属性并检查区域设置是否将转换为字符串?虽然它不会比Object更安全,是的,我做到了。它工作得很好。我会等一天,如果没有更好的答案,我会接受你的答案。好主意,但这不会回答如何将一个属性与不同的值类型一起使用的问题。好吧,我认为这是真的。非常感谢。好主意,但这并不能回答如何将一个属性与不同的值类型一起使用的问题。非常感谢。好主意,但这并不能回答如何将一个属性与不同的值类型一起使用的问题。非常感谢。好主意,但这并不能回答如何将一个属性与不同的值类型一起使用的问题。非常感谢。