2015-11-11 40 views
1

我試圖弄清楚我無法從控制器到我的jsp頁面獲取arraylist是什麼錯誤。在這裏,我發佈我的代碼,如果有人可以找出問題的可能性。提前致謝。從控制器獲取模型到jsp頁面

的web.xml:

<?xml version="1.0" encoding="UTF-8"?> 
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" 
    id="StudentWebApp" version="3.0"> 

    <display-name>Archetype Created Web Application</display-name> 
    <servlet> 
    <servlet-name>mvc-dispatcher-servlet</servlet-name> 
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
    <init-param> 
      <param-name>contextConfigLocation</param-name> 
      <param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value> 
     </init-param> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 
    <servlet-mapping> 
     <servlet-name>mvc-dispatcher-servlet</servlet-name> 
     <url-pattern>/</url-pattern> 
    </servlet-mapping> 
</web-app> 

MVC-調度員的servlet:

<beans xmlns="http://www.springframework.org/schema/beans" 
xmlns:context="http://www.springframework.org/schema/context" 
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context 
     http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> 

    <context:component-scan base-package="cz.webapp.student"/> 

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
    <property name="prefix"> 
     <value>/WEB-INF/view/</value> 
    </property> 
    <property name="suffix"> 
     <value>.jsp</value> 
    </property> 
    </bean> 
</beans> 

控制器:

/* 
* To change this license header, choose License Headers in Project Properties. 
* To change this template file, choose Tools | Templates 
* and open the template in the editor. 
*/ 
package cz.webapp.student.controllers; 

import cz.webapp.student.entity.Student; 
import cz.webapp.student.service.StudentService; 
import java.util.List; 
import java.util.Map; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.stereotype.Component; 
import org.springframework.stereotype.Controller; 
import org.springframework.web.bind.annotation.RequestMapping; 
import static org.springframework.web.bind.annotation.RequestMethod.GET; 
import org.springframework.web.bind.annotation.RequestParam; 

/** 
* 
* @author Jenda 
*/ 

@Component 
@RequestMapping("/StudentWebApp/*") 
public class StudentController { 

@Autowired 
StudentService studentServiceImpl; 



@RequestMapping(value="/students", method=GET) 
public String showAllStudent(Map<String, Object> model){ 
List<Student> studentList = studentServiceImpl.findAll(); 


///Zkouška dat 
studentList.add(new Student(1,"Martina", 25)); 

model.put("students", studentList); 


return "StudentWebApp/index"; 

} 
} 

的index.jsp:

回答

0

你的返回類型showAllStudent應該是cha一直到ModelAndView

@RequestMapping(value="/students", method=GET) 
public ModelAndView showAllStudent(Map<String, Object> model){ 
    List<Student> studentList = studentServiceImpl.findAll(); 
    studentList.add(new Student(1,"Martina", 25)); 
    model.put("students", studentList); 
    return new ModelAndView("StudentWebApp/index", model); 
} 
相關問題