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