簡體   English   中英

無法檢索從控制器發送到spring mvc中的值

[英]Unable to retrieve value sent from controller to view in spring mvc

我是Spring MVC的新手。 我正在編寫一個簡單的maven web項目,以便在調用時在視圖上顯示hello world。 除此之外,我使用的是apache tomcat 7。
我跟着這個
控制器被調用,我可以設置和打印模型圖。

的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>

為HelloController

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";
    }
}

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. 把maven依賴放在jstl上

提前致謝。

編輯 - 添加output.jsp屏幕截圖

在此輸入圖像描述

如果要使用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">

如果這還沒有解決您的問題,請嘗試在您的視圖中包含以下指令:

<%@ page isELIgnored="false" %>

希望它有所幫助。

這里的問題是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">

您不需要將<%@ page isELIgnored="false" %>到每個頁面,而是更改web.xml標頭一次。

它將無法工作,因為您正在修改控制器中的modal ,然后完全移動到具有自己的modelview 因此,您所做的任何更改都將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;
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM