Spring Boot无法解析JSP中的模型数据[重复]
Posted
技术标签:
【中文标题】Spring Boot无法解析JSP中的模型数据[重复]【英文标题】:Spring Boot not able to parse model data in JSP [duplicate] 【发布时间】:2020-10-22 08:44:21 【问题描述】:我有一个简单的JSP
webapp
,它应该在页面上显示Hello World
,但由于某种原因,model
数据未被解析。
浏览器上的实际输出: Hello $name!
浏览器上的预期输出: Hello World!
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>com.howtodoinjava</groupId>
<artifactId>spring-boot-demo</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>spring-boot-demo Maven Webapp</name>
<url>http://maven.apache.org</url>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.1.RELEASE</version>
</parent>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!-- Web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Tomcat Embed -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<!-- JSTL -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<!-- To compile JSP files -->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
IndexController.java
@Controller
public class IndexController
@GetMapping("/", "/hello")
public String hello(Model model, @RequestParam(value = "name", required = false, defaultValue = "World") String name)
model.addAttribute("name", name);
return "hello";
MvcConfiguration.java
@Configuration
@EnableWebMvc
@ComponentScan
public class MvcConfiguration extends WebMvcConfigurerAdapter
@Override
public void configureViewResolvers(ViewResolverRegistry registry)
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/view/");
resolver.setSuffix(".jsp");
resolver.setViewClass(JstlView.class);
registry.viewResolver(resolver);
SpringBootWebApplication.java
@SpringBootApplication
public class SpringBootWebApplication extends SpringBootServletInitializer
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application)
return application.sources(SpringBootWebApplication.class);
public static void main(String[] args) throws Exception
SpringApplication.run(SpringBootWebApplication.class, args);
application.properties
spring.mvc.view.prefix=/WEB-INF/view/
spring.mvc.view.suffix=.jsp
WEB-INF/view/hello.jsp
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Hello $name!</title>
</head>
<body>
<h2 class="hello-title">Hello $name!</h2>
</body>
</html>
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>
<display-name>Archetype Created Web Application</display-name>
</web-app>
【问题讨论】:
我没有检查你的配置,但据我记得你使用了错误的语法Hello
在页面顶部添加<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
并更改为<c:out value="$name"></c:out>
但仍然没有运气
【参考方案1】:
通过将包类型从 war
更改为 jar
并更改一些代码来使其工作。见下文。
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>com.howtodoinjava</groupId>
<artifactId>spring-boot-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-boot-demo Maven Webapp</name>
<url>http://maven.apache.org</url>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.1.RELEASE</version>
</parent>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
</dependencies>
</project>
SpringBootWebApplication.java
@SpringBootApplication
public class SpringBootWebApplication
public static void main(String[] args) throws Exception
SpringApplication.run(SpringBootWebApplication.class, args);
hello.jsp
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Hello $name!</title>
</head>
<body>
<h2 class="hello-title">Hello $name!</h2>
</body>
</html>
MvcConfiguration.java(已删除)
现在浏览器中的实际输出: Hello World!
【讨论】:
以上是关于Spring Boot无法解析JSP中的模型数据[重复]的主要内容,如果未能解决你的问题,请参考以下文章
使用 IntelliJ IDEA 进行调试时如何解析 JSP 中的 Spring 模型变量?
IntelliJ中的Spring boot + thymeleaf:无法解析变量