Spring MVC:无法显示下拉列表

时间:2014-08-31 10:53:43

标签: spring spring-mvc select

我没有获得专业的下拉值。其实我得到了例外

javax.servlet.ServletException: Type [java.lang.String] is not valid for option items
    org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:848)
    org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:781)
    org.apache.jsp.WEB_002dINF.views.Registration_jsp._jspService(org.apache.jsp.WEB_002dINF.views.Registration_jsp:85)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:322)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
    org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:238)
    org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:263)
    org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1208)
    org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:992)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:939)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:936)
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:827)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:697)
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:812)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
    org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:81)
root cause

javax.servlet.jsp.JspException: Type [java.lang.String] is not valid for option items
    org.springframework.web.servlet.tags.form.OptionWriter.writeOptions(OptionWriter.java:142)
    org.springframework.web.servlet.tags.form.SelectTag.writeTagContent(SelectTag.java:223)
    org.springframework.web.servlet.tags.form.AbstractFormTag.doStartTagInternal(AbstractFormTag.java:103)
    org.springframework.web.servlet.tags.RequestContextAwareTag.doStartTag(RequestContextAwareTag.java:80)
    org.apache.jsp.WEB_002dINF.views.Registration_jsp._jspx_meth_form_select_0(org.apache.jsp.WEB_002dINF.views.Registration_jsp:283)
    org.apache.jsp.WEB_002dINF.views.Registration_jsp._jspx_meth_form_form_0(org.apache.jsp.WEB_002dINF.views.Registration_jsp:144)

我理解选择标记有问题。

请帮忙

RegisterController

package net.codejava.spring.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.codejava.spring.model.User;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping(value = "/register")
public class RegisterController {

    @RequestMapping(method = RequestMethod.GET)
    public ModelAndView initForm() {
        User user = new User();
        user.setUsername("Anukul");
        ModelAndView mav = new ModelAndView("Registration");
        //=== default user name======
        //model.addAttribute("userNameDefault", "Enter Name");
        mav.addObject("userNameDefault", "Enter Name");

        //==== creating drop down list =====
        Map<String,String> profDropDown = new HashMap<String, String>();
        profDropDown.put("Lecturer", "Lecturer");
        profDropDown.put("proff", "proff");

//      //==== adding drop down to user ====
        mav.addObject("ProffesionList", profDropDown);
        mav.addObject("user",user);
        //==== user added to model =========

        return mav;
    }

    @RequestMapping(method = RequestMethod.POST)
    public String submitForm(Model model,@ModelAttribute User user) {

        model.addAttribute(user);

        // implement your own registration logic here...
        return "RegistrationSuccess";
    }

}

Registration.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
    "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Registration</title>
</head>
<body>
    <div align="center">
    <h1>reached here</h1>
        <form:form action="register" method="POST" commandName="user">
            <table border="0">
                <tr>
                    <td colspan="2" align="center"><h2>Spring MVC Form Demo - Registration</h2></td>
                </tr>
                <tr>
                    <td>User Name:</td>
                    <td><form:input path="username"  /></td>
                </tr>
                <tr>
                    <td>Password:</td>
                    <td><form:password path="password" /></td>
                </tr>
                <tr>
                    <td>E-mail:</td>
                    <td><form:input path="email" /></td>
                </tr>
                <tr>
                    <td>Birthday (mm/dd/yyyy):</td>
                    <td><form:input path="birthDate" /></td>
                </tr>
                <tr> 
                    <td>Profession:</td> 
                    <td><form:select path="profession" items="${ProffesionList}" /></td> 
                </tr> 
                <tr>
                    <td colspan="2" align="center"><input type="submit" value="Register" /></td>
                </tr>
            </table>
        </form:form>
    </div>
</body>

User.java

package net.codejava.spring.model;

import java.util.Date;

public class User {
    private String username;
    private String password;
    private String email;
    private Date birthDate;
    private String profession;

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public Date getBirthDate() {
        return birthDate;
    }

    public void setBirthDate(Date birthDate) {
        this.birthDate = birthDate;
    }

    public String getProfession() {
        return profession;
    }

    public void setProfession(String profession) {
        this.profession = profession;
    }

}
----------------

1 个答案:

答案 0 :(得分:0)

这对你有用吗<​​/ p>

<form:select id="profession" path="profession">
  <form:options items="${ProffesionList}"/>
</form:select>

或尝试使用此

<form:select id="profession" path="profession">
   <c:forEach var="prof" items="${ProffesionList}">
     <form:option value="${prof.key}" label="${prof.value}" />
   </c:forEach>
</form:select>

<强>控制器

   @RequestMapping(method = RequestMethod.POST)
   public String submitForm(Model model,@ModelAttribute User user) {

    //==== creating drop down list =====

    Map<String,String> profDropDown = new HashMap<String, String>();
    profDropDown.put("Lecturer", "Lecturer");
    profDropDown.put("proff", "proff");

    //      //==== adding drop down to user ====
    mav.addObject("ProffesionList", profDropDown);
    model.addAttribute(user);


    // implement your own registration logic here...
    return "RegistrationSuccess";
}

或添加 @ModelATtribute

@ModelAttribute("ProffesionList")
public Map<String,String> getProfessions(){
    Map<String,String> profDropDown = new HashMap<String, String>();
        profDropDown.put("Lecturer", "Lecturer");
        profDropDown.put("proff", "proff");

       return profDropDown;
}