【问题标题】:Get model from controller to jsp page从控制器获取模型到jsp页面
【发布时间】:2016-02-12 21:14:56
【问题描述】:

我试图找出无法从控制器获取数组列表到我的 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-dispatcher-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:

【问题讨论】:

    标签: spring jsp spring-mvc servlets web-applications


    【解决方案1】:

    showAllStudent 中的返回类型应更改为 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);
    }
    

    【讨论】:

      猜你喜欢
      • 2015-10-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-26
      • 1970-01-01
      • 2015-07-24
      • 1970-01-01
      相关资源
      最近更新 更多