2015-09-27 121 views
0

我正在嘗試基本教程的Spring MVC。我遇到了一個問題,我試圖將數據從HTML表單綁定到模型對象。有喜歡@ModelAttribute Spring MVC多選擇不設置ArrayList

private String studentName; 
private String studentHobby; 
private Long studentMobile; 
private Date studentDOB; 
private ArrayList<String> studentSkills; 

幾場所有的Fileds正確設置,我可以在另一個JSP妥善除了技能領域是ArrayList中顯示出來。

Name: ${student.studentName}<br> 
Hobby: ${student.studentHobby}<br> 
Mobile: ${student.studentMobile}<br> 
DOB: ${student.studentDOB}<br> 
Skills: ${student.studentSkills}<br> 

我的代碼如下:

package com.springMVC.controller; 

import java.util.ArrayList; 
import java.util.List; 

import org.springframework.stereotype.Controller; 
import org.springframework.ui.Model; 
import org.springframework.validation.BindingResult; 
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.bind.annotation.RequestParam; 
import org.springframework.web.servlet.ModelAndView; 

import com.springMVC.model.Student; 

@Controller 
public class StudentAdmissionController 
{ 

@ModelAttribute 
public void addCommonObjects(Model model) 
{ 
    model.addAttribute("headerMsg","welcome to spring learning!"); 
} 

@RequestMapping(value="/admissionForm",method=RequestMethod.GET) 
public ModelAndView getAdmissionForm() 
{ 
    System.out.println("inside getAdmission"); 
    ModelAndView model = new ModelAndView("AdmissionForm"); 
    return model; 
} 




@RequestMapping(value="submitAdmissionForm",method=RequestMethod.POST) 
public ModelAndView submitAdmissionForm(@ModelAttribute("student") Student student,BindingResult result) 
{ 
    System.out.println("inside submitAdmissionForm name: "+student.getStudentName()+" hobby: "+student.getStudentHobby()); 

    ModelAndView model = new ModelAndView("AdmissionSuccess"); 

    return model; 
} 

}

AdmissionForm.jsp:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" 
pageEncoding="ISO-8859-1"%> 
<%@ page isELIgnored="false" %> 
<!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=ISO-8859-1"> 
<title>Insert title here</title> 
</head> 
<body> 
<b>${headerMsg }</b> 
    <form action="/SpringMVCDemo/submitAdmissionForm" method="post"> 
     studentName: <input type="text" name="studentName"><br> 
     studentHobby: <input type="text" name="studentHobby"><br> 
     studentMobile: <input type="text" name="studentMobile"><br> 
     studentDOB: <input type="text" name="studentDOB"><br> 
     StudenSkills: <select name="studentSkills" multiple> 
         <option value="java">java</option> 
         <option value="Java">java2</option> 
         <option value="JAVA">java3</option> 
         </select> 
     <input type="submit"> 
    </form> 
</body> 
</html> 

AdmissionSuccess.jsp:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" 
pageEncoding="ISO-8859-1"%> 
    <%@ page isELIgnored="false" %> 

    <%@ taglib uri='http://java.sun.com/jsp/jstl/core' prefix='c'%> 
    <%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %> 
<!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=ISO-8859-1"> 
<title>Insert title here</title> 
</head> 
<body> 
<h1>${headerMsg }</h1> 
<h1>Congratulations!</h1> 
<h2>Name: ${student.studentName}</h2><br> 
<h2>Hobby: ${student.studentHobby}</h2><br> 
<h2>Mobile: ${student.studentMobile}</h2><br> 
<h2>${student.studentDOB}</h2><br> 
<h2>Skills: ${student.studentSkills}</h2><br> 
</body> 
</html> 

首先這個鏈接帶來的形式http://localhost:8080/SpringMVCDemo/admissionForm.html

而提交後相同的捕獲數據顯示在另一個jsp中。

+0

發佈Student的代碼。將studentiSkills的類型改爲'列表' –

+0

@JBNizet你讓我在那裏!它是ArrayList 。我不知道我爲什麼這樣寫。非常感謝你。請添加您的評論作爲解決方案。非常感謝你... – sAm

回答

2

studentSkills屬性必須是List<String>類型,而不是ArrayList<String>

0
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" 
pageEncoding="ISO-8859-1"%> 
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %> 
<%@ taglib uri='http://java.sun.com/jsp/jstl/core' prefix='c'%> 

<!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=ISO-8859-1"> 
<title>Insert title here</title> 
</head> 
<body> 
<h1>${headerMsg }</h1> 
<h1>Congratulations!</h1> 
<h2>Name: ${student.studentName}</h2><br> 
<h2>Hobby: ${student.studentHobby}</h2><br> 
<h2>Mobile: ${student.studentMobile}</h2><br> 
<h2>Date of Birth: ${student.studentDOB}</h2><br> 
<h2>Skills: ${student.studentSkills}</h2><br> 
</body> 
</html> 
相關問題