Java spring boot 2.0.6资源加载程序路径属性的velocity视图问题
当vm文件位于Java spring boot 2.0.6资源加载程序路径属性的velocity视图问题,java,spring,spring-boot,velocity,Java,Spring,Spring Boot,Velocity,当vm文件位于类路径:/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 vi
类路径:/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 lang="ko">
<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>
我的控制器:
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 lang="ko">
<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>
我的模板:
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 lang="ko">
<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>
你好,米尔基
你好!${name}
速度版
自v1.4版以来,Spring boot已弃用velocity模板支持
org.springframework.boot.autoconfigure.velocity.VelocityAutoconfiguration已弃用。从1.4开始,在速度支持被弃用之后
在Spring框架4.3中
下面的类不再是SpringBootAutoConfigure jar版本2.x(即SpringBootV2.x)的一部分。所以,它在SpringBootVersion2上不起作用
org.springframework.boot.autoconfigure.velocity.VelocityAutoConfiguration
另外,根据我的理解,如果您正确地使用了依赖项,那么它不应该在1.5.x版本上工作。spring-boot-2.0不再像上面的答案那样支持velocity视图
而不是使用Freemaker的速度
application.yml中不需要任何设置
[pom.xml] - add freemaker dependency
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
[Controller]
@CrossOrigin
@SkipSessionCheck
@GetMapping(baseUri+"/buy/pg/test")
public ModelAndView impViewTest() throws ResultCodeException {
try {
System.out.println("/buy/pg/test") ;
logger.debug("/buy/pg/test") ;
ModelAndView model = new ModelAndView();
model.addObject("errorTitle", "Error") ;
model.addObject("errorMessage", "success : No Error !!!") ;
model.setViewName("paygate/error");
return model ;
}
catch(Exception e){
logger.error(AppUtil.excetionToString(e)) ;
ModelAndView model = new ModelAndView();
model.addObject("errorTitle", "Error") ;
model.addObject("errorMessage", e.getMessage()) ;
model.setViewName("paygate/error");
return model ;
}
}
[error.ftl] - view file extension is ftl
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<title>error</title>
</head>
<body>
<h1>${errorTitle}</h1>
<p>${errorMessage}</p>
</body>
</html>
[pom.xml]-添加freemaker依赖项
org.springframework.boot
弹簧启动机自由标记器
[控制器]
@交叉起源
@跳过会话检查
@GetMapping(baseUri+“/buy/pg/test”)
public ModelAndView impViewTest()引发ResultCodeException{
试一试{
System.out.println(“/buy/pg/test”);
logger.debug(“/buy/pg/test”);
ModelAndView模型=新的ModelAndView();
addObject(“errorTitle”、“Error”);
model.addObject(“errorMessage”,“成功:无错误!!!”);
model.setViewName(“付款门/错误”);
收益模型;
}
捕获(例外e){
logger.error(AppUtil.exceptionToString(e));
ModelAndView模型=新的ModelAndView();
addObject(“errorTitle”、“Error”);
addObject(“errorMessage”,例如getMessage());
model.setViewName(“付款门/错误”);
收益模型;
}
}
[错误.ftl]-视图文件扩展名为ftl
错误
${errorTitle}
${errorMessage}
它在spring-boot-1.5.x上工作,但在spring-boot-2.0-6-RELEASE上不工作。我明白了,那么你的意思是我不能在spring-boot-2.0中使用velocity view任何模式吗?是的,自动配置不起作用。如果需要使用velocity,则需要手动配置。Spring不赞成velocity,因为在他们看来,与其他可用选项相比,模板很难使用。请接受这个答案,以便它对其他人有用。但是,我还有一个问题。我的Freemaker视图像ErrorError一样打印MVC模型值两次。