Ich weiß, dass dies eine alte Frage ist, aber ich hatte genau das gleiche Problem und dachte, ich würde die Problemumgehung posten, die ich verwendet habe.
Grundsätzlich deklariere ich die Eigenschaft als String und verwenden Sie die Enum.valueOf()
zu übersetzen.
Hier ist meine Klasse Action:
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;
}
}
Und hier ist die 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>
Und schließlich die Aktion:
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;
}
}