在JSP中将枚举值作为标签属性传递 [英] Passing a enum value as a tag attribute in JSP

查看:225
本文介绍了在JSP中将枚举值作为标签属性传递的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义的JSP标签,它使用一个枚举的参数。这种方法是使用需要这个枚举的其他类的结果。关键是我没有线索如何在EL中分配枚举值:

I have a custom JSP tag which is using a parameter which is an enum. This approach is a consequence of using other classes which need this enumeration. The point is I have no clue how to assign an enum value in the EL:

<mytaglib:mytag enumParam="${now what do I type here?}" />

到目前为止,我发现的唯一解决方法是将enumParam设置为整数,并将其转换为所需值:

The only workaround which I found so far was to make the enumParam an Integer and convert it to desired values:

<mytaglib:mytag enumParam="3" />

我相信必须有一个更好的方法来做到这一点。请帮助。

I believe there must be a better way to do it. Please help.

推荐答案

EL允许使用枚举!


有三种方法可以使用rvalue或lvalue表达式设置标签属性值:

[..]

There are three ways to set a tag attribute value using either an rvalue or lvalue expression:
[..]

仅限文字:

< some:tag value =sometext/>

<some:tag value="sometext"/>

此表达式被称为文字表达。在这种情况下,属性的String值被强制转换为属性的预期类型。文字值表达式具有特殊的语法规则。有关更多信息,请参阅文字表达。当tag属性具有枚举类型时,该属性使用的表达式必须是一个文字表达式。例如,标签属性可以使用表达式心来表示Suit.hearts。文字被强制为西装,属性获得值Suit.hearts。

This expression is called a literal expression. In this case, the attribute’s String value is coerced to the attribute’s expected type. Literal value expressions have special syntax rules. See Literal Expressions for more information. When a tag attribute has an enum type, the expression that the attribute uses must be a literal expression. For example, the tag attribute can use the expression "hearts" to mean Suit.hearts. The literal is coerced to Suit and the attribute gets the value Suit.hearts.

http://download.oracle.com/javaee/5/tutorial/doc/bnahq.html

枚举:

public Enum Color{ 
   RED, BLUE, GREEN 
}

JSP /标签文件

<mytaglib:mytag enumParam="${'RED'}" />

使用Tomcat 7.0.22以及Jetty 6.1.26进行测试。

Tested with Tomcat 7.0.22 as well as Jetty 6.1.26.

这篇关于在JSP中将枚举值作为标签属性传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆