struts 2 if:比较枚举
我有一个带有以下代码的Struts 2 jsp...
<s:iterator value="categories" id="category" status="iteratorStatus">
<s:if test='#category == "M" '> snip </s:if>
问题是类别后面的java代码是..
private static final CategoryEnum[] PRIVATE_VALUES = {A,B,C,M };
public static final List<CategoryEnum> VALUES = Collections.unmodifiableList(Arrays.asList(PRIVATE_VALUES));
public List<CategoryEnum> getCategories() {
return CategoryEnum.VALUES;
}
所以IF语句不起作用,它永远不会评估为true。 我尝试过逃避字符等,但没有成功。
我更愿意使用“类别”值回调 Action 类,并决定要做什么。例如,
<s:if test='renderCategory(#category)> snip </s:if>
但我不知道如何将#category 传递回操作。
那么任何人都可以帮助我弄清楚如何将值传回或使 Struts IF 标签与枚举一起使用。
我已经读过这篇文章:这在这里没有多大帮助,但无论如何我都会参考一下:
Struts 2:为什么 'if' 标签不评估一个字符字符串
有人可以帮助我吗?
杰夫·波特
I have a Struts 2 jsp with the following code...
<s:iterator value="categories" id="category" status="iteratorStatus">
<s:if test='#category == "M" '> snip </s:if>
The problem is the java code behind categories is..
private static final CategoryEnum[] PRIVATE_VALUES = {A,B,C,M };
public static final List<CategoryEnum> VALUES = Collections.unmodifiableList(Arrays.asList(PRIVATE_VALUES));
public List<CategoryEnum> getCategories() {
return CategoryEnum.VALUES;
}
So the IF statement does not work, it never evalutes to true.
I've tried escaping the charaters etc, but with no success.
I'd prefer to make a call back to the Action class with the 'category' value and have that decied what to do. e.g.
<s:if test='renderCategory(#category)> snip </s:if>
but I don't know how to pass the #category back to the action.
So can anyone help either me work out how to pass the value back or to make the Struts IF tag work with an enum.
I've already read this: which isn't much help here, but I'll reference iy anyway:
Struts 2: Why won't the 'if' tag evaluate a one char string
can anyone help me please?
Jeff Porter
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看来我没有查看 CategoryEnum 类(仅附加了 .class 而不是 .java)。
CategoryEnum 类虽然它是一个枚举,但值是它们自己的类,因此调用它的 toString() 将允许我比较每个类中的值。
It seem that I didn't look at the CategoryEnum class (only had the .class attached not the .java).
CategoryEnum class although it is a enum, the values are their own class, so hence calling toString() on it will allow me to compare the value inside each one.
从您的示例中我不清楚 PRIVATE_VALUES 数组是什么?看起来 A、B、C 和 M 是 CategoryEnum 的实例。在这种情况下,您将 CaterogyEnum 与 OGNL 表达式中的字符串进行比较,这就是它失败的原因。
您可以使用真正的 Java 1.5 枚举,或者在页面呈现之前在您的操作上创建一个新的字符串值列表吗?
It is not clear to me from your example what is the PRIVATE_VALUES array? it looks like A,B,C and M are instances of CategoryEnum. In that case, you are comparing a CaterogyEnum to a string in your OGNL expression, so that's why it is failing.
Can you use real Java 1.5 enums, or maby create on your action a new list of String values, before the page renders?