Spring表单:“Type [java.lang.String]对选项”

时间:2015-08-21 13:32:08

标签: java spring jsp spring-mvc spring-tld

我有一个旧的MVC应用程序。

它有一个控制器,其方法如下:

@RequestMapping(method=RequestMethod.GET)
public ModelAndView handleGet(@RequestParam(value="userId") String userId){
    ModelMap model = new ModelMap();
    List<Long> relatedIds = getListOfIds(userId)
    RelatedIdFormHolder();
    formHolder.setRelatedIds(relatedIds);
    model.put("relatedIdFormHolder", formHolder);
    return new ModelAndView("relatedIds.jsp", model);
}

我有一个带有以下形式的.jsp:

<form:form method="post" commandName="relatedIdFormHolder" id="relatedIdFormHolder" onsubmit="submit_form()">
    <form:select path="selectedId">
        <form:options items="relatedIds"></form:options>
    </form:select>
</form:form>

我有一个POJO:

public class RelatedIdFormHolder {

    public List<Long> getRelatedIds() {
        return relatedIds;
    }
    public void setRelatedIds(List<Long> relatedIds) {
        this.relatedIds = relatedIds;
    }
    public String getSelectedId() {
        return selectedId;
    }
    public void setSelectedId(String selectedId) {
        this.selectedId = selectedId;
    }
    List<Long> relatedIds;
    String selectedId; 
}

我访问了正确的URL,并且映射到了正确的控制器,然后映射到正确的.jsp,但JSP在渲染时抛出了错误:

ERROR: error500 - javax.servlet.ServletException: javax.servlet.jsp.JspException: Type [java.lang.String] is not valid for option items

我已经尝试从视图中来回传递relatedIds和selectedID作为字符串和Longs,但是得到了同样的问题。

什么给了,这应该是直截了当的,我错过了什么?

1 个答案:

答案 0 :(得分:3)

我认为你的形式应该看起来像这样

<form:option items="${formHandler.relatedIds}"></form:option>

请注意,您需要${}