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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4109103/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 04:48:01  来源:igfitidea点击:

How to use an enum in Struts html:select tag

javajspselectenumsstruts

提问by Drahakar

I am currently trying to create a html:selecttag from an enum so it could be set in a specific object:

我目前正在尝试html:select从枚举创建一个标签,以便可以在特定对象中设置它:

class someClass {
    SomeEnum someProperties = null;
    public getSomeProperties() { return someProperties; }
    public setSomeProperties(SomeEnum e) { someProperties = e; }

The JSP with Struts tags:

带有 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>

But I am currently getting a "Cannot invoke someClass.setProperties - argument type mismatch" exception.

但我目前收到“无法调用 someClass.setProperties - 参数类型不匹配”异常。

Is there a way to use an enum in a Struts select tag.

有没有办法在 Struts 选择标签中使用枚举。

采纳答案by Drahakar

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.

Struts 1 框架不能与 Java 5 的特性一起正常工作,因为它也被设计为与 JDK 1.4 一起工作。

The latest stable release is Struts 1.3.10. The prerequisitesfor 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.

最新的稳定版本是Struts 1.3.10。Struts 1.3.10的先决条件包括 Java Development Kit,版本 1.4 或更高版本。如果它在 JDK 1.4 上运行,则意味着它不使用 Java 5 的功能,其中包括枚举。

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.

如果您至少使用 JDK 1.5(这很好),您可以在自己的代码中使用枚举,Struts 也将在 JDK 1.5 上运行(因为 Sun 非常努力地使它们向后兼容)但框架本身并不了解新功能添加到语言中。因此,对于像将请求参数映射到 ActionForm 属性这样的自动操作,它不会提供正确的结果。

回答by FGreg

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.

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

Here is my ActionForm class:

这是我的 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;
    }
}

And here is the JSP:

这是 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;
    }
}