繁体   English   中英

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

[英]How to use an enum in Struts html:select tag

我目前正在尝试从枚举中创建一个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 select标签中使用枚举。

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

基本上,我将属性声明为String并使用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>

最后行动:

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

Struts 1框架无法正常使用Java 5的功能,因为它也可以与JDK 1.4一起使用。

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

如果你至少使用JDK 1.5,那么你可以在你自己的代码中使用枚举(那很好),Struts也可以在JDK 1.5上运行(因为Sun很难让它们向后兼容)但框架本身并不知道新功能添加到语言中。 因此,对于将请求参数映射到ActionForm属性等自动操作,它将无法提供正确的结果。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM