SpringBoot学习笔记之JSP与freemarker支持

来源:互联网 发布:平板 win10 知乎 2017 编辑:程序博客网 时间:2024/06/16 13:18
1、SpringBoot 支持JSP
先在pom.xml加入依赖
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>

然后在application.properties文件中增加如下配置
# 页面默认前缀目录
spring.mvc.view.prefix=/WEB-INF/jsp/
# 响应页面默认后缀
spring.mvc.view.suffix=.jsp

测试类请见 com.vk.liyj.controller.LiyjController 类
注意:LiyjController类中使用@Value读取了application.properties里的文件
@Value("${application.value:Hello liyj}")
private String value="";
application.value表示在application.properties中的key,冒号后面的值是默认值,如果在资源文件中无法读取到值则返回默认值 。

2、Springboot支持freemarker
1)、先在pom.xml中加入依赖
<!-- 引入freeMarker的依赖包. -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
2)、配置application.properties
spring.freemarker.allow-request-override=false
spring.freemarker.cache=true
spring.freemarker.check-template-location=true
spring.freemarker.charset=UTF-8
spring.freemarker.content-type=text/html
spring.freemarker.expose-request-attributes=false
spring.freemarker.expose-session-attributes=false
spring.freemarker.expose-spring-macro-helpers=false
测试类请见 com.vk.liyj.controller.LiyjController 类freemarker方法。
执行SpringBootSampleApplication中的main方法,访问 http://localhost:8080/web/freemarker,看到下图信息

相关源码下载:http://download.csdn.net/download/liyuejin/9986140



原创粉丝点击