【问题标题】:Unable to retrieve value sent from controller to view in spring mvc无法检索从控制器发送的值以在 spring mvc 中查看
【发布时间】:2016-12-20 08:43:01
【问题描述】:

我是 Spring MVC 的新手。我正在编写一个简单的 Maven Web 项目,以在调用时显示一个 hello world。除此之外,我正在使用 apache tomcat 7。
我关注this
控制器正在被调用,我可以设置和打印模型图。

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>springmvc</groupId>
<artifactId>demo</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>demo Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>3.8.1</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>4.3.4.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
    </dependency>


</dependencies>
<build>
    <finalName>demo</finalName>
</build>

web.xml

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

<web-app>
 <servlet>
        <servlet-name>HelloWeb</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>HelloWeb</servlet-name>
        <url-pattern>*.action</url-pattern>
    </servlet-mapping>

</web-app>

HelloWeb-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:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    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">

    <mvc:annotation-driven />
    <context:component-scan base-package="com.tutorialpoint" />

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

</beans>

你好控制器

package com.tutorialpoint;

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

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

    public HelloController(){
        System.out.println("hiii");
    }

    @RequestMapping(method  = RequestMethod.GET)
    public String printHello(Model model){

        System.out.println("this is controller");
        model.addAttribute("testvalue", "Hello World!!");
        System.out.println(model.toString());

        return "output";
    }
}

输出.jsp

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>

<%@ 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>Output</title>
</head>
<body>
    <p>This is output jsp page  message is </p> ${testvalue}
</body>
</html>

但是在结果视图(output.jsp)上我无法获取值。 我试过了

  1. 在jsp中使用spring core标签
  2. 使用 c:out 标签
  3. 为jstl添加maven依赖

提前致谢。

编辑 - 添加 output.jsp 屏幕截图

【问题讨论】:

  • 你能看到 output.jsp 吗?
  • 您无法解析给定的模型值,但视图的其余部分是否会在浏览器中呈现?
  • 嗨,是的,output.jsp 正在加载这个生成 -->这是输出 jsp 页面消息是 ${testvalue}
  • 我不知道它的错字,但您返回的是“输出”,并且您的文件名为 Output.jsp(与大写“O”相关的问题)?
  • 我已经添加了 output.jsp 屏幕截图。

标签: java spring jsp spring-mvc


【解决方案1】:

如果您想使用 EL,您需要将您的部署描述符 (web.xml) 声明为 Servlet 2.4 或更高版本。当您使用支持 Servlet 3.0 的 Tomcat 7 时,我建议您使用该版本。

您可以通过将 web.xml 中的左括号更改为:

<web-app version="3.0"
  xmlns="http://java.sun.com/xml/ns/javaee"
  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_3_0.xsd">

如果这还没有解决您的问题,请尝试在您的视图中包含以下指令:

&lt;%@ page isELIgnored="false" %&gt;

希望对您有所帮助。

【讨论】:

    【解决方案2】:

    这里的问题在于web.xml 标头

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

    将其更改为以下内容。

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="2.5" 
    xmlns="http://java.sun.com/xml/ns/javaee"
    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">
    

    您无需将&lt;%@ page isELIgnored="false" %&gt; 添加到每个页面,只需更改一次web.xml 标头。

    【讨论】:

      【解决方案3】:

      它不起作用,因为您正在修改控制器中的modal,然后完全移动到新的view,它将拥有自己的model。因此,您所做的任何更改都将是nullified

      我建议您通过在控制器方法中添加RedirectAttributes 来使用Spring MVC 的flash attribute 工具,如下所示,

       @RequestMapping(method  = RequestMethod.GET)
          public String printHello(Model model, RedirectAttributes redir){
      
              System.out.println("this is controller");
             // model.addAttribute("testvalue", "Hello World!!");
              redir.addFlashAttribute("testvalue", "Hello World!!");
              System.out.println(model.toString());
      
              return "redirect:/output";
          }
      

      或者创建一个新的ModelAndView 并返回它,

       @RequestMapping(method  = RequestMethod.GET)
          public ModelAndView printHello(Model model){
      
              ModelAndView model= new ModelAndView("output");
              System.out.println("this is controller");
              model.addAttribute("testvalue", "Hello World!!");
              System.out.println(model.toString());
      
              return model;
          }
      

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多