弹簧启动 MVC 模板未加载(404 未找到)
2022-09-01 08:15:13
我在这里有一个非常简单的Spring-Boot MVC应用程序,它不起作用。一个控制器有一个页面未加载,但未找到404。
我在控制器方法中放置了一个System.out.println(“主页”)语句,并验证它是否已映射并正确触发,但模板未加载。
错误:
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Thu Nov 06 23:43:51 EST 2014
There was an unexpected error (type=Not Found, status=404).
文件夹结构:
src/main/java
+-Application.java
+-WebController.java
src/main/resources
+-templates
+-index.html
缩写的控制台输出:
Server initialized with port: 8080
Starting service Tomcat
Starting Servlet Engine: Apache Tomcat/7.0.55
Initializing Spring embedded WebApplicationContext
Root WebApplicationContext: initialization completed in 1947 ms
Mapping servlet: 'dispatcherServlet' to [/]
Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
Mapped "{[/],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.String app.WebController.index()
Tomcat started on port(s): 8080/http
Started Application in 5.552 seconds (JVM running for 6.366)
Home Page
Home Page
Home Page
配置:
@EnableAutoConfiguration
@Configuration
@ComponentScan
public class Application extends WebMvcConfigurerAdapter {
public static void main(String[] args) {
ConfigurableApplicationContext context = SpringApplication.run(Application.class);
}
}
控制器:
@Controller
public class WebController {
@RequestMapping(value="/")
public String index(){
System.out.println("Home Page");
return "index";
}
}
索引.html :
<!DOCTYPE html>
<html lang="en">
<body>
<h1>Hello</h1>
</body>
</html>
我在这里有一个非常简单的Spring-Boot MVC应用程序,它不起作用。一个控制器有一个页面未加载,但未找到404。
我在控制器方法中放置了一个System.out.println(“主页”)语句,并验证它是否已映射并正确触发,但模板未加载。
错误:
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Thu Nov 06 23:43:51 EST 2014
There was an unexpected error (type=Not Found, status=404).
文件夹结构:
src/main/java
+-Application.java
+-WebController.java
src/main/resources
+-templates
+-index.html
缩写的控制台输出:
Server initialized with port: 8080
Starting service Tomcat
Starting Servlet Engine: Apache Tomcat/7.0.55
Initializing Spring embedded WebApplicationContext
Root WebApplicationContext: initialization completed in 1947 ms
Mapping servlet: 'dispatcherServlet' to [/]
Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
Mapped "{[/],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.String app.WebController.index()
Tomcat started on port(s): 8080/http
Started Application in 5.552 seconds (JVM running for 6.366)
Home Page
Home Page
Home Page
配置:
@EnableAutoConfiguration
@Configuration
@ComponentScan
public class Application extends WebMvcConfigurerAdapter {
public static void main(String[] args) {
ConfigurableApplicationContext context = SpringApplication.run(Application.class);
}
}
控制器:
@Controller
public class WebController {
@RequestMapping(value="/")
public String index(){
System.out.println("Home Page");
return "index";
}
}
索引.html :
<!DOCTYPE html>
<html lang="en">
<body>
<h1>Hello</h1>
</body>
</html>
聚 甲醛:
<?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>com.App</groupId>
<artifactId>App</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>App</name>
<description>App</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.1.8.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.181</version>
</dependency>
<!--dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-redis</artifactId>
</dependency-->
<dependency>
<groupId>com.firebase</groupId>
<artifactId>firebase-client</artifactId>
<version>1.0.1</version>
</dependency>
<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala-library</artifactId>
<version>2.10.4</version>
</dependency>
</dependencies>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<start-class>App.Application</start-class>
<java.version>1.7</java.version>
</properties>
<repositories>
<repository>
<id>spring-releases</id>
<name>Spring Releases</name>
<url>http://repo.spring.io/libs-release</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-releases</id>
<name>Spring Releases</name>
<url>http://repo.spring.io/libs-release</url>
</pluginRepository>
</pluginRepositories>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>