如何在 Struts html:select 标签中使用枚举
我目前正在尝试从枚举创建 html:select
标记,以便可以在特定对象中设置它:
class someClass {
SomeEnum someProperties = null;
public getSomeProperties() { return someProperties; }
public setSomeProperties(SomeEnum e) { someProperties = e; }
带有 Struts 标记的 JSP:
<html:select name="someForm" property="someInstance.someProperties" >
<html:option value="${someEnum.STANDARD}"><bean:message key="i18nkeystd"/>
<html:option value="${someEnum.PREVENTIVE} "><bean:message key="i18nkeyprev"/>
</html:select>
但我目前收到“无法调用 someClass.setProperties -参数类型不匹配”异常。
有没有办法在 Struts 选择标签中使用枚举。
I am currently trying to create a html:select
tag from an enum so it could be set in a specific object:
class someClass {
SomeEnum someProperties = null;
public getSomeProperties() { return someProperties; }
public setSomeProperties(SomeEnum e) { someProperties = e; }
The JSP with Struts tags:
<html:select name="someForm" property="someInstance.someProperties" >
<html:option value="${someEnum.STANDARD}"><bean:message key="i18nkeystd"/>
<html:option value="${someEnum.PREVENTIVE} "><bean:message key="i18nkeyprev"/>
</html:select>
But I am currently getting a "Cannot invoke someClass.setProperties - argument type mismatch" exception.
Is there a way to use an enum in a Struts select tag.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Struts 1 框架无法正确使用 Java 5 的功能,因为它也被设计为与 JDK 1.4 一起使用。
最新的稳定版本是 Struts 1.3.10。 Struts 1.3.10 的先决条件包括 Java 开发工具包版本 1.4或稍后。如果它在 JDK 1.4 上运行,则意味着它不使用 Java 5 的功能,其中包括枚举。
如果您至少使用 JDK 1.5,则可以在自己的代码中使用枚举(这很好),Struts 也将在 JDK 1.5 上运行(因为 Sun 非常努力地使它们向后兼容),但框架本身并不了解新功能添加到语言中。因此,对于将请求参数映射到 ActionForm 属性等自动操作,它不会提供正确的结果。
A Struts 1 framework won't properly work with features of Java 5 because it was designed to work with a JDK 1.4 also.
The latest stable release is Struts 1.3.10. The prerequisites for Struts 1.3.10 include a Java Development Kit, version 1.4 or later. If it runs on JDK 1.4 it means it does not use features of Java 5, which includes enums.
You can use enums in your own code if you use at least JDK 1.5 (that's fine), Struts will also run on JDK 1.5 (since Sun tried really hard to make them backward compatible) but the framework itself does not know about the new features added to the language. So for automatic operations like mapping request parameters to ActionForm properties it will not deliver the proper result.
我知道这是一个老问题,但我遇到了完全相同的问题,并且认为我会发布我使用的解决方法。
基本上,我将属性声明为字符串并利用
Enum.valueOf()
进行翻译。这是我的 ActionForm 类:
这是 JSP:
最后是 Action:
I know this is an old question but I had the exact same problem and thought I would post the workaround that I used.
Basically, I declare the property as a String and utilize the
Enum.valueOf()
to translate.Here is my ActionForm class:
And here is the JSP:
And finally the Action: