Spring Boot - 放置jsp文件的位置

Ree*_*ema 7 java jsp spring-mvc maven spring-boot

我正在尝试使用MVC开发一个新的Spring启动应用程序,作为将现有的Spring MVC应用程序移动到Spring启动的第一步.

但是,我遇到了jsp文件映射的问题.

无法解析名为'dispatcherServlet'的servlet中名为'hello'的视图

我已经准备好了很多答案,但似乎没有解决我的问题 - 我不打算使用任何模板引擎,因为我会考虑很多jsps - 一旦设置了spring boot,可能是一个计划.

我有一个项目结构如下:

MyFirstApp
  --src/main/java
    --uk.co.company
      --MainApplication.java
      --ServletInitializer.java
    --uk.co.company.web
      --HelloController.java
  --src/main/resources
    --static
    --templates
    --application.properties
 --src
   --main
     --webapp
       --WEB-INF
         --jsp
           --hello.jsp
  --pom.xml
Run Code Online (Sandbox Code Playgroud)

放置以下代码:

MyFirstAppApplication.java

 @SpringBootApplication(exclude = { DataSourceAutoConfiguration.class, 
 HibernateJpaAutoConfiguration.class })
 @EnableWebMvc
 public class MyFirstAppApplication extends SpringBootServletInitializer {
     public static void main(String[] args) {
    SpringApplication.run(MyFirstAppApplication.class, args);
  }
}
Run Code Online (Sandbox Code Playgroud)

ServletInitializer.java

  public class ServletInitializer extends SpringBootServletInitializer {

@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder 
application) {
    return application.sources(MyFirstAppApplication.class);
}
}
Run Code Online (Sandbox Code Playgroud)

HelloController.java

@Controller
public class HelloController {  
@RequestMapping("/hello")
public String sayHello() {      
    return "hello";
}   
}
Run Code Online (Sandbox Code Playgroud)

为hello.jsp

<%@ 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>Hello</title>
</head>
<body>
hellooo
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

的pom.xml

<?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>uk.co.company</groupId>
<artifactId>MyFirstApp</artifactId>
<version>1.0.0</version>
<packaging>war</packaging>

<name>MyFirstApp</name>
<description>Demo project for Spring Boot</description>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.3.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
</properties>
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>com.microsoft.sqlserver</groupId>
        <artifactId>mssql-jdbc</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.apache.tomcat.embed</groupId>
        <artifactId>tomcat-embed-jasper</artifactId>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>
Run Code Online (Sandbox Code Playgroud)

application.properties

spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix:.jsp
Run Code Online (Sandbox Code Playgroud)

gla*_*tor 5

我创建了一个渲染jsp的演示项目

Git 网址: https: //github.com/rksharma1401/spring-boot-war

结帐然后 mvn package java -jar target\simple-web-app-tomcat-0.0.1-SNAPSHOT.war URL : http://localhost:8081/w


ksw*_*ghs 0

为什么有两个独立的java类(MyFirstAppApplication,ServletInitializer)扩展SpringBootServletInitializer类?

删除 ServletInitializer.java 并将配置方法从 ServletInitializer 移至 MyFirstAppApplication。