猿问

spring-boot 2.0.6 速度视图问题与资源加载器路径属性

当 vm 文件位于 中时classpath:/templates/hello.vm,在 spring-boot-1.5.x 上一切正常。


当 vm 文件被定位classpath:/templates/hello.vm或移动到classpath:/template/WEB-INF/view/hellow.vm并更新application.yml如下时,它不再在 spring-boot-2.0.6-RELEASE 上工作。


我的应用程序.yml:


spring:

    velocity:

    enabled: true

    view-names: 

    resource-loader-path: classpath:/templates/WEB-INF/view/         

    expose-request-attributes: true

我的控制器:


@Controller

public class HelloController {


    @RequestMapping("/hello")

    public String index(Model model) {

        model.addAttribute("name", "SpringBlog from Millky");

        return "hello";

    }


}

我的模板:


<!DOCTYPE html>

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>Hello Millky</title>

</head>

<body>

    <h2>Hello! ${name}</h2>

    <div>Velocity version</div>

</body>

</html>


largeQ
浏览 191回答 2
2回答

慕村9548890

Spring Boot 自v1.4 version.org.springframework.boot.autoconfigure.velocity.VelocityAutoConfigurationDeprecated。从 1.4 开始,在 Spring Framework 4.3 中弃用 Velocity 支持之后以下类不再是 Spring boot 自动配置 jar 版本 2.x(即 Spring boot v 2.x)的一部分。因此,它不适用于 Spring Boot 版本 2。org.springframework.boot.autoconfigure.velocity.VelocityAutoConfiguration另外,根据我的理解,如果您正确使用依赖项,它也不应该在 1.5.x 版本上工作。

慕姐8265434

spring-boot-2.0 不再像上面的答案那样支持速度视图。使用 Freemaker 代替速度application.yml 中不需要设置&nbsp; &nbsp; [pom.xml] - add freemaker dependency&nbsp; &nbsp; <dependency>&nbsp; &nbsp; &nbsp; &nbsp; <groupId>org.springframework.boot</groupId>&nbsp; &nbsp; &nbsp; &nbsp; <artifactId>spring-boot-starter-freemarker</artifactId>&nbsp; &nbsp; </dependency>&nbsp; &nbsp; [Controller]&nbsp; &nbsp; @CrossOrigin&nbsp; &nbsp; @SkipSessionCheck&nbsp; &nbsp; @GetMapping(baseUri+"/buy/pg/test")&nbsp; &nbsp; public ModelAndView impViewTest() throws ResultCodeException {&nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("/buy/pg/test") ;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; logger.debug("/buy/pg/test") ;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ModelAndView model = new ModelAndView();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; model.addObject("errorTitle", "Error") ;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; model.addObject("errorMessage", "success : No Error !!!") ;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; model.setViewName("paygate/error");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return model ;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; catch(Exception e){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; logger.error(AppUtil.excetionToString(e)) ;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ModelAndView model = new ModelAndView();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; model.addObject("errorTitle", "Error") ;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; model.addObject("errorMessage", e.getMessage()) ;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; model.setViewName("paygate/error");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return model ;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; [error.ftl] - view file extension is ftl&nbsp; &nbsp; <!DOCTYPE html>&nbsp; &nbsp; <html>&nbsp; &nbsp; <head>&nbsp; &nbsp; &nbsp; &nbsp; <meta charset="UTF-8">&nbsp; &nbsp; &nbsp; &nbsp; <title>error</title>&nbsp; &nbsp; &nbsp;</head>&nbsp; &nbsp; <body>&nbsp; &nbsp; <h1>${errorTitle}</h1>&nbsp; &nbsp; <p>${errorMessage}</p>&nbsp; &nbsp; </body>&nbsp; &nbsp; </html>
随时随地看视频慕课网APP

相关分类

Java
我要回答