如何在 Struts html:select 标签中使用枚举

发布于 2024-10-01 00:57:07 字数 689 浏览 2 评论 0原文

我目前正在尝试从枚举创建 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

同尘 2024-10-08 00:57:07

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.

半岛未凉 2024-10-08 00:57:07

我知道这是一个老问题,但我遇到了完全相同的问题,并且认为我会发布我使用的解决方法。

基本上,我将属性声明为字符串并利用 Enum.valueOf() 进行翻译。

这是我的 ActionForm 类:

package example;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

public class SearchByForm extends ActionForm{

    private static final long serialVersionUID = 5609098522501926807L;

    private String selectedOption;

    public enum SearchByOptions{
        NONE("-- Select One --"),
        OPTION1("Option 1"),
        OPTION2("Option 2"),
        OPTION3("Option 3"),
        OPTION4("Option 4"),
        OPTION5("Option 5");

        private String displayText;

        private SearchByOptions(String displayText){
            this.displayText = displayText;
        }

        public String getDisplayText(){
            return this.displayText;
        }
    }

    /**
     * @param selectedOption the selectedOption to set
     */
    public void setSelectedOption(String selectedOption) {
        this.selectedOption = selectedOption;
    }

    /**
     * @return the selectedOption
     */
    public String getSelectedOption() {
        return selectedOption;
    }

    public void reset(ActionMapping mapping, ServletRequest request)
    {
        setSelectedOption(SearchByOptions.NONE.toString());
    }

    @Override
    public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {

        ActionErrors errors = new ActionErrors();

        if( SearchByOptions.valueOf(getSelectedOption()) == SearchByOptions.NONE)
        {
           errors.add("selectedOption", new ActionMessage("error.common.html.select.required"));
        }

        return errors;
    }
}

这是 JSP:

<html>
    <body>
        <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
        <%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html"%>

        <%
            pageContext.setAttribute("searchByOptions", example.SearchByForm.SearchByOptions.values());
        %>

        <div class="searchByInput"> 
            <html:form action="submitSearchBy">
                <html:select property="selectedOption">
                    <c:forEach var="searchByOption" items="${searchByOptions}">
                        <jsp:useBean id="searchByOption" type="example.SearchByForm.SearchByOptions"/>
                        <html:option value="${searchByOption}"><%= searchByOption.getDisplayText()%></html:option>
                    </c:forEach>
                </html:select> 
                <html:submit/>
            </html:form>
        </div>
    </body>
</html>

最后是 Action:

package example;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public class SubmitSearchByAction extends Action{

    public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,HttpServletResponse response) throws Exception {
        ActionForward forwardAction;

        SearchByForm searchForm = (SearchByForm )form;

        switch(SearchByOptions.valueOf(searchForm.getSelectedOption())){
            case OPTION1:
                forwardAction = mapping.findForward(SearchByOptions.OPTION1.toString());
                break;
            case OPTION2:
                forwardAction = mapping.findForward(SearchByOptions.OPTION2.toString());
                break;
            case OPTION3:
                forwardAction = mapping.findForward(SearchByOptions.OPTION3.toString());
                break;
            case OPTION4:
                forwardAction = mapping.findForward(SearchByOptions.OPTION4.toString());
                break;
            case OPTION5:
                forwardAction = mapping.findForward(SearchByOptions.OPTION5.toString());
                break;
        }
        return forwardAction;
    }
}

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:

package example;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

public class SearchByForm extends ActionForm{

    private static final long serialVersionUID = 5609098522501926807L;

    private String selectedOption;

    public enum SearchByOptions{
        NONE("-- Select One --"),
        OPTION1("Option 1"),
        OPTION2("Option 2"),
        OPTION3("Option 3"),
        OPTION4("Option 4"),
        OPTION5("Option 5");

        private String displayText;

        private SearchByOptions(String displayText){
            this.displayText = displayText;
        }

        public String getDisplayText(){
            return this.displayText;
        }
    }

    /**
     * @param selectedOption the selectedOption to set
     */
    public void setSelectedOption(String selectedOption) {
        this.selectedOption = selectedOption;
    }

    /**
     * @return the selectedOption
     */
    public String getSelectedOption() {
        return selectedOption;
    }

    public void reset(ActionMapping mapping, ServletRequest request)
    {
        setSelectedOption(SearchByOptions.NONE.toString());
    }

    @Override
    public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {

        ActionErrors errors = new ActionErrors();

        if( SearchByOptions.valueOf(getSelectedOption()) == SearchByOptions.NONE)
        {
           errors.add("selectedOption", new ActionMessage("error.common.html.select.required"));
        }

        return errors;
    }
}

And here is the JSP:

<html>
    <body>
        <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
        <%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html"%>

        <%
            pageContext.setAttribute("searchByOptions", example.SearchByForm.SearchByOptions.values());
        %>

        <div class="searchByInput"> 
            <html:form action="submitSearchBy">
                <html:select property="selectedOption">
                    <c:forEach var="searchByOption" items="${searchByOptions}">
                        <jsp:useBean id="searchByOption" type="example.SearchByForm.SearchByOptions"/>
                        <html:option value="${searchByOption}"><%= searchByOption.getDisplayText()%></html:option>
                    </c:forEach>
                </html:select> 
                <html:submit/>
            </html:form>
        </div>
    </body>
</html>

And finally the Action:

package example;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public class SubmitSearchByAction extends Action{

    public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,HttpServletResponse response) throws Exception {
        ActionForward forwardAction;

        SearchByForm searchForm = (SearchByForm )form;

        switch(SearchByOptions.valueOf(searchForm.getSelectedOption())){
            case OPTION1:
                forwardAction = mapping.findForward(SearchByOptions.OPTION1.toString());
                break;
            case OPTION2:
                forwardAction = mapping.findForward(SearchByOptions.OPTION2.toString());
                break;
            case OPTION3:
                forwardAction = mapping.findForward(SearchByOptions.OPTION3.toString());
                break;
            case OPTION4:
                forwardAction = mapping.findForward(SearchByOptions.OPTION4.toString());
                break;
            case OPTION5:
                forwardAction = mapping.findForward(SearchByOptions.OPTION5.toString());
                break;
        }
        return forwardAction;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文