【发布时间】:2018-03-20 10:20:04
【问题描述】:
我花了一整天的时间试图获得一个简单的 Spring Boot 应用程序来显示我的 .jsp 文件。我无法切换到另一种技术(要求),也无法让它发挥作用。这是一个非常简单的应用程序。这是文件结构:
pom.xml
src/
main/
java/
hello/
HelloController.java
MVCApplication.java
resources/
application.properties
static/
index.html
templates/
pages/
helloworld.jsp
这是我的两个 java 文件:
HelloController.java:
// @RestController // From when I first posted this question.
@Controller
public class HelloController {
// Should I add a "produces" attribute to this? I don't know what to use for jsp.
@RequestMapping("/hello")
public String hello(Model model) {
model.addAttribute("greeting", "Hello Spring MVC");
// I see this message in the output, so I know this method is getting called.
System.err.printf("Setting \"greeting\" to \"Hello Spring MVC\"%n");
return "helloworld";
}
}
MVCApplication.java:
@SpringBootApplication
public class MVCApplication extends SpringApplication {
public static void main(String[] args) {
SpringApplication.run(MVCApplication.class, args);
}
// Since I have the properties spring.mvc.view.prefix and spring.mvc.view.suffix
// defined in applications.properties, I don't think I need this method.
// I have tried it both with and without this method. It doesn't work either way.
@Bean
public ViewResolver getViewResolver() {
// I see this line in the output, which confirms that this method gets called.
System.err.printf("Creating custom JSP View Resolver%n"); // NON-NLS
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/templates/pages/");
resolver.setSuffix(".jsp");
resolver.setViewClass(JstlView.class);
return resolver;
}
}
我的 helloworld.jsp 文件很简单,但是我在浏览器中从来没有看到过:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Spring MVC -HelloWorld</title>
</head>
<body>
<h1>Greeting: ${greeting}</h1>
</body>
</html>
我在 applications.properties 中定义了三个属性。
spring.mvc.view.prefix=/templates/pages/
spring.mvc.view.suffix=.jsp
server.port=7070
我知道正在读取属性,因为我需要在浏览器中指定端口 7070 才能看到任何内容。所以所有这些属性都应该起作用。
我的 pom 文件似乎具有所有正确的依赖项。
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.springframework</groupId>
<artifactId>gs-spring-boot</artifactId>
<version>0.1.0</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--JSP Enabled: -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
</dependencies>
<properties>
<java.version>1.8</java.version>
</properties>
<packaging>jar</packaging>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
templates 目录曾经被称为 WEB-INF,但我在某处读到 WEB-INF 不适用于 Spring Boot。但这两种方法都不起作用。
我转到 localhost:7070/hello,我得到的只是一条错误消息:
出现意外错误(类型=未找到,状态=404)。 /templates/pages/helloworld.jsp
这告诉我我的控制器方法正在被调用,但它要么没有找到我的 .jsp 文件,要么甚至没有在寻找它。
我在这个论坛上看过很多类似的问题,到目前为止,没有任何帮助。人们提出了一些建议,比如改用百里香叶,这对其他人来说可能很好,但对我来说不是一个选择。谁能弄清楚我做错了什么?
附录:
在 Vipul Singh 的建议下,我尝试将控制器方法更改为:
@RequestMapping("/hello")
public ModelAndView hello(Model theModel) {
// theModel.addAttribute("greeting", "Hello Spring MVC"); // this didn't work.
ModelAndView modelView= new ModelAndView("helloworld", "helloworld", theModel);
modelView.addObject("greeting", "Hello Spring MVC"); // This didn't work either.
return modelView;
}
没有用。我得到了和以前一样的错误信息。
修订版。最初我使用@RestController,这是错误的。我还将 spring.boot.starter.parent 版本从 1.5.7 减少到 1.5.2
【问题讨论】:
标签: jsp spring-mvc spring-boot