现在要模板引擎Freemarker整合进springboot。使用freemarker视图解析。
pom文件添加依赖
- <!– https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-freemarker –>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-freemarker</artifactId>
- </dependency>
配置freemarker参数,将参数加到application.properties配置文件。
- #配置freemarker参数
- #设定ftl文件路径
- #spring.freemarker.template-loader-path=classpath:/templates/
- ##设定静态文件路径,js,css等
- spring.mvc.static-path-pattern=/static/**
- spring.freemarker.cache=false
- spring.freemarker.charset=UTF-8
- spring.freemarker.check-template-location=true
- spring.freemarker.content-type=text/html
- spring.freemarker.expose-request-attributes=true
- spring.freemarker.expose-session-attributes=true
- spring.freemarker.request-context-attribute=request
- spring.freemarker.suffix=.ftl
- spring.freemarker.template-loader-path=classpath:/templates/
- #comma-separated list
- #spring.freemarker.view-names= # whitelist of view names that can be resolved
写入控制层,路由映射到模板文件
- @Controller
- public class LoginController {
- @RequestMapping(“/login”)
- public String login() {
- return “sysLogin”;
- }
- }
定义freemarker模板,以登录页为样例。
- <#assign webRoot=request.contextPath />
- <!DOCTYPE html>
- <html lang=“en”>
- <head>
- <meta charset=“utf-8”>
- <meta http-equiv=“X-UA-Compatible” content=“IE=edge”>
- <meta name=“viewport” content=“width=device-width, initial-scale=1”>
- <!– The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags –>
- <meta name=“description” content=“”>
- <meta name=“author” content=“”>
- <link rel=“icon” href=“${webRoot}/static/favicon.ico”>
- <title>Signin Template for Bootstrap</title>
- <!– Bootstrap core CSS –>
- <link href=“${webRoot}/static/css/bootstrap/bootstrap.min.css” rel=“stylesheet”>
- <!– IE10 viewport hack for Surface/desktop Windows 8 bug –>
- <link href=“${webRoot}/static/css/bootstrap/ie10-viewport-bug-workaround.css” rel=“stylesheet”>
- <!– Custom styles for this template –>
- <link href=“${webRoot}/static/css/custom/signin.css” rel=“stylesheet”>
- <!– Just for debugging purposes. Don’t actually copy these 2 lines! –>
- <!–[if lt IE 9]><script src=”../../assets/js/ie8-responsive-file-warning.js”></script><![endif]–>
- <script src=“${webRoot}/static/js/bootstrap/ie-emulation-modes-warning.js”></script>
- <!– HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries –>
- <!–[if lt IE 9]>
- <script src=“${webRoot}/static/js/bootstrap/html5shiv.min.js”></script>
- <script src=“${webRoot}/static/js/bootstrap/respond.min.js”></script>
- <![endif]–>
- </head>
- <body>
- <div class=“container”>
- <form class=“form-signin”>
- <h2 class=“form-signin-heading”>小温之家</h2>
- <label for=“inputEmail” class=“sr-only”>Email address</label>
- <input type=“email” id=“inputEmail” class=“form-control” placeholder=“Email address” required autofocus>
- <label for=“inputPassword” class=“sr-only”>Password</label>
- <input type=“password” id=“inputPassword” class=“form-control” placeholder=“Password” required>
- <div class=“checkbox”>
- <label>
- <input type=“checkbox” value=“remember-me”> 记住密码
- </label>
- </div>
- <button class=“btn btn-lg btn-primary btn-block” type=“submit”>登 录</button>
- </form>
- </div> <!– /container –>
- <!– IE10 viewport hack for Surface/desktop Windows 8 bug –>
- <script src=“${webRoot}/static/js/bootstrap/ie10-viewport-bug-workaround.js”></script>
- </body>
- </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能简单快速开发是好事,可以一出问题就坑了。不懂得原理就会成为硬伤。暂且这样吧。。。
效果图
参考:
http://freemarker.apache.org/
发表评论