我目前正在尝试从枚举中创建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标签中使用枚举。
答案 0 :(得分:9)
Struts 1框架无法正常使用Java 5的功能,因为它也可以与JDK 1.4一起使用。
最新的稳定版本为Struts 1.3.10。 Struts 1.3.10的prerequisites包括Java Development Kit 1.4或更高版本。如果它在JDK 1.4上运行,则意味着它不使用包含枚举的Java 5的功能。
如果你使用至少JDK 1.5(那很好),你可以在你自己的代码中使用枚举,Struts也将在JDK 1.5上运行(因为Sun努力使它们向后兼容)但框架本身并不知道添加到该语言的新功能。因此,对于将请求参数映射到ActionForm属性等自动操作,它将无法提供正确的结果。
答案 1 :(得分:8)
我知道这是一个老问题,但我遇到了完全相同的问题,并认为我会发布我使用过的解决方法。
基本上,我将该属性声明为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;
}
}