spring boot学习系列之整合Freemarker模板引擎4


现在要模板引擎Freemarker整合进springboot。使用freemarker视图解析。

pom文件添加依赖

  1. <!– https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-freemarker –>
  2. <dependency>
  3.     <groupId>org.springframework.boot</groupId>
  4.     <artifactId>spring-boot-starter-freemarker</artifactId>
  5. </dependency>

配置freemarker参数,将参数加到application.properties配置文件。

  1. #配置freemarker参数
  2. #设定ftl文件路径
  3. #spring.freemarker.template-loader-path=classpath:/templates/
  4. ##设定静态文件路径,js,css等
  5. spring.mvc.static-path-pattern=/static/**
  6. spring.freemarker.cache=false
  7. spring.freemarker.charset=UTF-8
  8. spring.freemarker.check-template-location=true
  9. spring.freemarker.content-type=text/html
  10. spring.freemarker.expose-request-attributes=true
  11. spring.freemarker.expose-session-attributes=true
  12. spring.freemarker.request-context-attribute=request
  13. spring.freemarker.suffix=.ftl
  14. spring.freemarker.template-loader-path=classpath:/templates/
  15. #comma-separated list
  16. #spring.freemarker.view-names= # whitelist of view names that can be resolved

写入控制层,路由映射到模板文件

  1. @Controller
  2. public class LoginController {
  3.     @RequestMapping(“/login”)
  4.     public String login() {
  5.         return “sysLogin”;
  6.     }
  7. }

定义freemarker模板,以登录页为样例。

  1. <#assign webRoot=request.contextPath />
  2. <!DOCTYPE html>
  3. <html lang=“en”>
  4.   <head>
  5.     <meta charset=“utf-8”>
  6.     <meta http-equiv=“X-UA-Compatible” content=“IE=edge”>
  7.     <meta name=“viewport” content=“width=device-width, initial-scale=1”>
  8.     <!– The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags –>
  9.     <meta name=“description” content=“”>
  10.     <meta name=“author” content=“”>
  11.     <link rel=“icon” href=“${webRoot}/static/favicon.ico”>
  12.     <title>Signin Template for Bootstrap</title>
  13.     <!– Bootstrap core CSS –>
  14.     <link href=“${webRoot}/static/css/bootstrap/bootstrap.min.css” rel=“stylesheet”>
  15.     <!– IE10 viewport hack for Surface/desktop Windows 8 bug –>
  16.     <link href=“${webRoot}/static/css/bootstrap/ie10-viewport-bug-workaround.css” rel=“stylesheet”>
  17.     <!– Custom styles for this template –>
  18.     <link href=“${webRoot}/static/css/custom/signin.css” rel=“stylesheet”>
  19.     <!– Just for debugging purposes. Don’t actually copy these 2 lines! –>
  20.     <!–[if lt IE 9]><script src=”../../assets/js/ie8-responsive-file-warning.js”></script><![endif]–>
  21.     <script src=“${webRoot}/static/js/bootstrap/ie-emulation-modes-warning.js”></script>
  22.     <!– HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries –>
  23.     <!–[if lt IE 9]>
  24.       <script src=“${webRoot}/static/js/bootstrap/html5shiv.min.js”></script>
  25.       <script src=“${webRoot}/static/js/bootstrap/respond.min.js”></script>
  26.     <![endif]–>
  27.   </head>
  28.   <body>
  29.     <div class=“container”>
  30.       <form class=“form-signin”>
  31.         <h2 class=“form-signin-heading”>小温之家</h2>
  32.         <label for=“inputEmail” class=“sr-only”>Email address</label>
  33.         <input type=“email” id=“inputEmail” class=“form-control” placeholder=“Email address” required autofocus>
  34.         <label for=“inputPassword” class=“sr-only”>Password</label>
  35.         <input type=“password” id=“inputPassword” class=“form-control” placeholder=“Password” required>
  36.         <div class=“checkbox”>
  37.           <label>
  38.             <input type=“checkbox” value=“remember-me”> 记住密码
  39.           </label>
  40.         </div>
  41.         <button class=“btn btn-lg btn-primary btn-block” type=“submit”>登 录</button>
  42.       </form>
  43.     </div> <!– /container –>
  44.     <!– IE10 viewport hack for Surface/desktop Windows 8 bug –>
  45.     <script src=“${webRoot}/static/js/bootstrap/ie10-viewport-bug-workaround.js”></script>
  46.   </body>
  47. </html>

启动成功,访问却报错:ServletException: Circular view path。。。Check your ViewResolver setup。。。

我依赖添加了啊,为什么没有加载freemarker,找不到视图渲染?配置有错?视图解析器有问题?mvn clean install下,还是一样,jar包里有FreeMarkerAutoConfiguration类啊,不是说会自动加载freemarker类吗?包冲突了,启动也没看到freemarker 配置日志?不至于啊,springboot的依赖配置不是模块化了吗?不至于吧,然后我把spring-boot-starter-parent父组件版本 ,有1.5.4改成1.5.3,他就好了。。。Springboot能简单快速开发是好事,可以一出问题就坑了。不懂得原理就会成为硬伤。暂且这样吧。。。

效果图

springboot_freemarker

参考:

http://freemarker.apache.org/

发表评论

电子邮件地址不会被公开。 必填项已用*标注

20 + 18 = ?