springboot 集成freemarker

来源:互联网 发布:linux用户加组 编辑:程序博客网 时间:2024/06/06 12:26

接着上一篇(springboot应用搭建及简单介绍)继续搞,之前已经搭起了springboot的架子,接下来是集成freemarker,这样一个最简单的web项目就有了。

首先需要在pom里面加入freemarker的依赖

<dependency>  <groupId>org.springframework.boot</groupId>  <artifactId>spring-boot-starter-freemarker</artifactId></dependency>
然后在src/main/resources目录下建一个template的目录,之后可以把页面放到这里面,新建application.properties,添加如下内容

spring.freemarker.suffix=.ftlspring.freemarker.templateEncoding=UTF-8spring.freemarker.templateLoaderPath=classpath:/templates/
定义一个controller,写一个index的方法,如下

@Controllerpublic class IndexController {@RequestMapping(value = { "index", "/" })public String index() {return "index";// template目录下页面文件名}}
启动访问http://localhost:8080/   就可以看到对应的页面了

源码

git clone https://github.com/cm-jornada/demo.gitgit checkout freemarker


详细的freemarker配置

spring.freemarker.allow-request-override=false # Set whether HttpServletRequest attributes are allowed to override (hide) controller generated model attributes of the same name.spring.freemarker.allow-session-override=false # Set whether HttpSession attributes are allowed to override (hide) controller generated model attributes of the same name.spring.freemarker.cache=false # Enable template caching.spring.freemarker.charset=UTF-8 # Template encoding.spring.freemarker.check-template-location=true # Check that the templates location exists.spring.freemarker.content-type=text/html # Content-Type value.spring.freemarker.enabled=true # Enable MVC view resolution for this technology.spring.freemarker.expose-request-attributes=false # Set whether all request attributes should be added to the model prior to merging with the template.spring.freemarker.expose-session-attributes=false # Set whether all HttpSession attributes should be added to the model prior to merging with the template.spring.freemarker.expose-spring-macro-helpers=true # Set whether to expose a RequestContext for use by Spring's macro library, under the name "springMacroRequestContext".spring.freemarker.prefer-file-system-access=true # Prefer file system access for template loading. File system access enables hot detection of template changes.spring.freemarker.prefix= # Prefix that gets prepended to view names when building a URL.spring.freemarker.request-context-attribute= # Name of the RequestContext attribute for all views.spring.freemarker.settings.*= # Well-known FreeMarker keys which will be passed to FreeMarker's Configuration.spring.freemarker.suffix= # Suffix that gets appended to view names when building a URL.spring.freemarker.template-loader-path=classpath:/templates/ # Comma-separated list of template paths.spring.freemarker.view-names= # White list of view names that can be resolved.






原创粉丝点击