【问题标题】:Spring MVC model attribute value not printingSpring MVC模型属性值不打印
【发布时间】:2013-03-22 07:20:07
【问题描述】:

我刚开始学习spring mvc。尝试遵循 hello world 示例。 一切似乎都工作正常,除了 jsp 视图无法接收控制器中设置的模型属性值。

这是一些代码。

控制器:

package org.home.webapp.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping("/welcome")
public class HelloController {

@RequestMapping(method = RequestMethod.GET)
public String printWelcome(ModelMap model) {

    model.addAttribute("message", "Spring 3 MVC Hello World");
    return "hello";

}
}

查看

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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>Hello World</title>
</head>
<body>
<h1>Message : ${message}</h1>
</body>
</html>

web.xml

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.5//EN"
 "http://java.sun.com/dtd/web-app_2_5.dtd" >

 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
 version="2.5" xmlns="http://java.sun.com/xml/ns/javaee">

<display-name>Spring MVC Web Application</display-name>

<servlet>
    <servlet-name>mvc-dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>mvc-dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>
</context-param>

<listener>
    <listener-class>
                  org.springframework.web.context.ContextLoaderListener
            </listener-class>
</listener>
</web-app>

mvc-dispatcher-servlet.xml

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
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.2.xsd
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.2.xsd">

<context:component-scan base-package="org.home.webapp.controller" />

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

</beans>

【问题讨论】:

  • 尝试使用Model 而不是ModelMap..一切看起来都很好..您在WEB-INF/pages 中有hello.jsp 对吗?
  • 我猜代码来自here :) .. 确保hello.jsp 出现在正确的位置
  • 使用模型而不是模型映射没有帮助。 jsp 的位置正确。
  • 这可能是因为使用了旧的 DTD。您可以通过启用表达式语言来解决它。您可以将以下代码放在 head 部分以打印值

标签: spring jsp spring-mvc


【解决方案1】:

使用 有所帮助。

【讨论】:

    【解决方案2】:

    我们需要包含 org.springframework.web.servlet.ModelAndView 而不是 org.springframework.web.portlet.ModelAndView;

    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.servlet.ModelAndView;
    
    @Controller
    public class HelloWorldController {
    
        @RequestMapping("/hello")
        public ModelAndView hello()
        {
            System.out.println("Just Test iNside");
            String message = "HELLO SPRING MVC HOW R U";  
            return new ModelAndView("hello", "message", message);  
        }
    

    【讨论】:

      【解决方案3】:

      使用以下代码解决了我的问题,可能对您有所帮助

      Web.xml

      <web-app id="WebApp_ID" version="2.4" 
      xmlns="http://java.sun.com/xml/ns/j2ee" 
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
      xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
      http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> .....<web-app>
      

      spring-servlet.xml

      <?xml version="1.0" encoding="UTF-8"?>
      <beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:context="http://www.springframework.org/schema/context"
      xmlns:mvc="http://www.springframework.org/schema/mvc"
      xsi:schemaLocation="  
          http://www.springframework.org/schema/beans  
          http://www.springframework.org/schema/beans/spring-beans.xsd  
          http://www.springframework.org/schema/context  
          http://www.springframework.org/schema/context/spring-context.xsd  
          http://www.springframework.org/schema/mvc  
          http://www.springframework.org/schema/mvc/spring-mvc.xsd">...</beans>
      

      还添加了以下依赖项

          <dependency>
              <groupId>javax.servlet</groupId>
              <artifactId>jstl</artifactId>
              <version>1.2</version>
          </dependency> 
      

      试试这个改变可能对你有用

      【讨论】:

        【解决方案4】:

        你可以使用&lt;th:block th:text="${your_attribut_name}"/&gt;

        示例控制器

        @RequestMapping(method = RequestMethod.GET)
        public String printWelcome(ModelMap model) {
              model.addAttribute("message", "Spring 3 MVC Hello World");
              return "hello";
        }
        

        查看

        <h1>Message : <th:block th:text="${message}"/></h1>
        

        输出

        Message : Spring 3 MVC Hello World
        

        【讨论】:

          猜你喜欢
          • 2013-06-01
          • 1970-01-01
          • 1970-01-01
          • 2011-11-25
          • 2013-10-09
          • 1970-01-01
          • 2013-05-08
          • 2015-01-07
          • 1970-01-01
          相关资源
          最近更新 更多