How to use an enum in Struts html:select tag
Asked Answered
G

2

12

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.

Groupie answered 5/11, 2010 at 18:53 Comment(0)
T
9

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.

Triatomic answered 5/11, 2010 at 21:48 Comment(0)
J
9

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;
    }
}
Jennette answered 17/10, 2012 at 15:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.