配置资源路径及属性为: spring.velocity.resource-loader-path=classpath:velocity/ spring.mvc.view.suffix=.vm new ModelAndView("/project/detail",map) 资源存放在classpath:velocity/project/detail.vm modelAndView跳转时无法命中静态资源
时间: 2024-04-03 12:34:28 浏览: 153
你可以尝试修改 resource-loader-path 的值,将其设为 classpath:/velocity/,这样可以直接在 classpath 下查找资源。同时,你还需要确认资源文件的存放位置和命名是否正确,应该为 velocity/project/detail.vm。如果还是无法命中静态资源,可以尝试使用相对路径或绝对路径来指定资源文件的位置。另外,如果你使用的是 Spring Boot,可以考虑使用 Thymeleaf 模板引擎替代 Velocity,它的资源路径配置相对简单,且支持更多的功能和特性。
相关问题
3、(4分)Spring Boot集成Velocity模板引擎时,以下哪些配置是正确的 A、开启缓存的配置为:spring.velocity.cache B、设置字符编码的配置为:spring.velocity.charse C、设置模板文件的后缀名的配置为:spring.velocity.suffix D、设置模板文件存放路径的配置为:spring.velocity.resource-loader-path
D、设置模板文件存放路径的配置为:`spring.velocity.resource-loader-path`。在Spring Boot中集成Velocity模板引擎时,需要进行相关配置以便正确地加载和渲染模板文件。常见的配置方式有两种:
1. 在`application.properties`或`application.yml`配置文件中定义Velocity的相关属性,如模板文件的位置、缓存的开关等。示例:
```
spring.velocity.resource-loader-path=classpath:templates/
spring.velocity.cache=false
spring.velocity.charset=UTF-8
spring.velocity.suffix=.vm
```
2. 使用Java代码配置Velocity模板引擎的相关属性。示例:
```java
@Configuration
public class VelocityConfig {
@Bean
public VelocityConfigurer velocityConfigurer() {
VelocityConfigurer configurer = new VelocityConfigurer();
configurer.setResourceLoaderPath("classpath:templates/");
configurer.setVelocityProperties(velocityProperties());
return configurer;
}
@Bean
public Properties velocityProperties() {
Properties properties = new Properties();
properties.setProperty("input.encoding", "UTF-8");
properties.setProperty("output.encoding", "UTF-8");
properties.setProperty("directive.set.null.allowed", "true");
properties.setProperty("resource.manager.defaultcache.size", "100");
return properties;
}
@Bean
public ViewResolver velocityViewResolver() {
VelocityViewResolver resolver = new VelocityViewResolver();
resolver.setSuffix(".vm");
resolver.setCache(false);
resolver.setContentType("text/html;charset=UTF-8");
resolver.setExposeSpringMacroHelpers(true);
resolver.setExposeRequestAttributes(true);
resolver.setExposeSessionAttributes(true);
return resolver;
}
}
```
其中,`spring.velocity.resource-loader-path`表示模板文件的路径,`spring.velocity.cache`表示是否开启缓存,`spring.velocity.charset`表示字符编码,`spring.velocity.suffix`表示模板文件的后缀名。因此,选项D是正确的。选项A和B的属性名称不正确;选项C的属性名称正确,但是它是用于设置模板文件的后缀名,而不是用于开启缓存。
Spring Boot整合Velocity
### Spring Boot 整合 Velocity 模板引擎
#### 一、引入依赖项
为了使Spring Boot项目能够支持Velocity模板,在`pom.xml`文件中需加入相应的Maven依赖:
```xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 添加velocity工具包 -->
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-engine-core</artifactId>
<version>2.3</version>
</dependency>
<!-- spring boot velocity starter -->
<dependency>
<groupId>com.github.ulisesbocchio</groupId>
<artifactId>jasypt-spring-boot-starter</artifactId>
<version>3.0.4</version>
</dependency>
```
上述代码展示了如何通过POM文件添加必要的库以启用Velocity功能[^1]。
#### 二、配置application.properties 或 application.yml 文件
接下来,修改项目的配置文件以便正确设置Velocity参数。对于`.properties`格式而言:
```properties
spring.velocity.resource.loader.path=classpath:/templates/
spring.velocity.check-template-location=true
spring.velocity.expose-request-attributes=true
spring.velocity.suffix=.vm
```
而对于YAML格式,则应如下所示编写:
```yaml
spring:
velocity:
resource-loader-path: classpath:/templates/
check-template-location: true
expose-request-attributes: true
suffix: .vm
```
这些配置指定了模板的位置和其他一些选项,确保它们按照预期工作。
#### 三、创建控制器类并返回视图名称
定义一个简单的Controller用于处理HTTP请求并将数据传递给前端展示层:
```java
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class HelloController {
@GetMapping("/hello")
public String hello(Model model){
model.addAttribute("message", "Hello, World!");
return "index"; // 返回名为 index 的 vm 文件作为响应体内容
}
}
```
这段Java代码实现了基本的功能——当访问特定URL路径时会渲染指定的Velocity模板,并向其中注入变量供其使用。
#### 四、建立Velocity模板文件
最后一步是在资源目录下的`/src/main/resources/templates/`位置新建一个叫做`index.vm`的文本文件,里面可以放置HTML结构加上嵌入式的表达式语法来呈现动态信息:
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Hello Page</title>
</head>
<body>
<h1>$!{message}</h1>
</body>
</html>
```
此部分描述了具体的页面布局以及怎样利用特殊标记`${}`获取来自服务器端的数据。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231045021.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)