spring.mvc.view.prefix 和 spring.mvc.view.suffix 必须是什么?
What do spring.mvc.view.prefix and spring.mvc.view.suffix have to be?
我使用 Spring Initializr 创建了一个 Spring 使用 Maven 的启动演示应用程序(这几乎是我第一次使用 Spring)。它有效,但出于某种原因不显示 index.html
以外的任何页面。如果我是对的,那是因为 application.properties
中的配置,但我只是不知道,我在那里添加了什么。
我的项目的源代码结构:
src
main
java
irimi
springbootdemo
SpringBootDemoApplication.java
SpringBootDemoApplicationController.java
resources
static
index.html
templates
test-form.html
test-page.jsp
application.properties
我尝试在 application.properties
中添加不同的前缀和后缀,但没有任何效果。举个例子:
spring.mvc.view.prefix=/templates/
spring.mvc.view.suffix=.html
再次,index.html
完美打开。
也许,在这里也展示一下我的依赖就好了:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
这是一个错误页面:
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Sat Jan 15 11:49:12 MSK 2022
There was an unexpected error (type=Not Found, status=404).
No message available
如果我的错误确实在 application.properties
中,我在那里放了什么?
使用模板默认渲染
如果您使用默认的“/resources/templates”进行渲染view.Spring Boot 仅包含对以下模板引擎的自动配置支持:
- FreeMarker
- Groovy
- 百里香叶
- 速度
示例:
第一步:
要使用 thymeleaf,您应该使用 gradle 和 maven 添加依赖项
Gradle:
implementation group: 'org.springframework.boot', name: 'spring-boot-starter-thymeleaf', version: '2.5.4'
或
专家:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
<version>2.5.4</version>
</dependency>
第二步:
使用属性文件添加以下代码
(Optional)spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
支持 MVC
默认情况下,此处理程序提供类路径上的 /static、/public、/resources 和 /META-INF/resources 目录中的静态内容。由于 src/main/resources 默认情况下通常位于类路径中,因此我们可以将这些目录中的任何一个放在那里。
只有静态文件夹可用于呈现 view.You 可以使用下面的代码自定义属性 file.By 默认值 Spring 启动将从名为 /static(或 /public 或 /resources 或 /META-INF/resources) 在类路径中或从 ServletContext 的根。它使用来自 Spring MVC 的 ResourceHttpRequestHandler,因此您可以通过添加自己的 WebMvcConfigurerAdapter 并覆盖 addResourceHandlers 方法来修改该行为。
spring.mvc.static-path-pattern=/content/**
spring.web.resources.static-locations=classpath:/files/,classpath:/static-files
更多信息请访问
我使用 Spring Initializr 创建了一个 Spring 使用 Maven 的启动演示应用程序(这几乎是我第一次使用 Spring)。它有效,但出于某种原因不显示 index.html
以外的任何页面。如果我是对的,那是因为 application.properties
中的配置,但我只是不知道,我在那里添加了什么。
我的项目的源代码结构:
src
main
java
irimi
springbootdemo
SpringBootDemoApplication.java
SpringBootDemoApplicationController.java
resources
static
index.html
templates
test-form.html
test-page.jsp
application.properties
我尝试在 application.properties
中添加不同的前缀和后缀,但没有任何效果。举个例子:
spring.mvc.view.prefix=/templates/
spring.mvc.view.suffix=.html
再次,index.html
完美打开。
也许,在这里也展示一下我的依赖就好了:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
这是一个错误页面:
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Sat Jan 15 11:49:12 MSK 2022
There was an unexpected error (type=Not Found, status=404).
No message available
如果我的错误确实在 application.properties
中,我在那里放了什么?
使用模板默认渲染
如果您使用默认的“/resources/templates”进行渲染view.Spring Boot 仅包含对以下模板引擎的自动配置支持:
- FreeMarker
- Groovy
- 百里香叶
- 速度
示例:
第一步:
要使用 thymeleaf,您应该使用 gradle 和 maven 添加依赖项 Gradle:
implementation group: 'org.springframework.boot', name: 'spring-boot-starter-thymeleaf', version: '2.5.4'
或 专家:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
<version>2.5.4</version>
</dependency>
第二步: 使用属性文件添加以下代码
(Optional)spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
支持 MVC
默认情况下,此处理程序提供类路径上的 /static、/public、/resources 和 /META-INF/resources 目录中的静态内容。由于 src/main/resources 默认情况下通常位于类路径中,因此我们可以将这些目录中的任何一个放在那里。
只有静态文件夹可用于呈现 view.You 可以使用下面的代码自定义属性 file.By 默认值 Spring 启动将从名为 /static(或 /public 或 /resources 或 /META-INF/resources) 在类路径中或从 ServletContext 的根。它使用来自 Spring MVC 的 ResourceHttpRequestHandler,因此您可以通过添加自己的 WebMvcConfigurerAdapter 并覆盖 addResourceHandlers 方法来修改该行为。
spring.mvc.static-path-pattern=/content/**
spring.web.resources.static-locations=classpath:/files/,classpath:/static-files
更多信息请访问