[英]Unable to retrieve value sent from controller to view in spring mvc
I am new to Spring MVC. 我是Spring MVC的新手。 I am writing a simple maven web project to display a hello world on view when it is invoked.
我正在编写一个简单的maven web项目,以便在调用时在视图上显示hello world。 In addition to this I am using apache tomcat 7.
除此之外,我使用的是apache tomcat 7。
I am following this 我跟着这个
The Controller is getting invoked and I am able to set and print the modelmap. 控制器被调用,我可以设置和打印模型图。
pom.xml 的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 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 的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 为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 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>
But on resulting view(output.jsp) I am unable to fetch the value. 但是在结果视图(output.jsp)上,我无法获取该值。 I tried
我试过了
Thanks in advance. 提前致谢。
Edit - adding output.jsp screen shot 编辑 - 添加output.jsp屏幕截图
If you want to use EL you need to declare your deployment descriptor (web.xml) as Servlet 2.4 or higher. 如果要使用EL,则需要将部署描述符(web.xml)声明为Servlet 2.4或更高版本。 As you use Tomcat 7 which supports Servlet 3.0 I'd recommend you to use that version.
当您使用支持Servlet 3.0的Tomcat 7时,我建议您使用该版本。
You can achieve it by changing your opening bracket in web.xml to this: 您可以通过将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">
If that hasn't resolved your problem try to include below directive in your view: 如果这还没有解决您的问题,请尝试在您的视图中包含以下指令:
<%@ page isELIgnored="false" %>
Hope it helped. 希望它有所帮助。
Here the problem is with the web.xml
header 这里的问题是
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" >
Change it to the following. 将其更改为以下内容。
<?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">
You don't need to add <%@ page isELIgnored="false" %>
to every page instead change the web.xml
header once. 您不需要将
<%@ page isELIgnored="false" %>
到每个页面,而是更改web.xml
标头一次。
It will not work, as you are modifying the modal
in controller and then moving to new view
altogether which will have its own model
. 它将无法工作,因为您正在修改控制器中的
modal
,然后完全移动到具有自己的model
新view
。 Hence any change you do will be nullified
. 因此,您所做的任何更改都将
nullified
。
I would recommend you to use flash attribute
facility of Spring MVC by adding RedirectAttributes
in your controller method as follows, 我建议您通过在控制器方法中添加
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";
}
Or Create a new ModelAndView
and return it, 或者创建一个新的
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.