Paul
Paul

Reputation: 3582

Spring forms: "Type [java.lang.String] is not valid for option items"

I have an old MVC application.

It has a controller with a method like:

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

I have a .jsp with a form like:

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

and I have a POJO like:

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

I visit the correct URL, and am mapped to the correct controller, then to the correct .jsp, but the JSP throws an error while rendering that says:

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

I've tried passing both the relatedIds and the selectedID back and forth from the view both as strings and Longs, but get the same issue.

What gives, this should be straightfoward, what am I missing?

Upvotes: 3

Views: 8198

Answers (1)

Imrank
Imrank

Reputation: 1019

I think you form should look something like this

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

Please keep in mind that you need the ${}.

Upvotes: 4

Related Questions