Freemarker的配置与使用
1.在pro.xml配置文件中引入架包
<!--freemarker-->
<dependency>
<groupId>freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>${freemarker-version}</version>
</dependency>
2.Freemarker的配置
- <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
- <!-- 配置Freemarker --> <bean id="staticPageService" class="cn.itcast.core.service.staticpage.StaticPageServiceImpl"> <property name="freeMarkerConfigurer"> <bean class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer"> <property name="templateLoaderPath" value="/WEB-INF/ftl/"/> <property name="defaultEncoding" value="UTF-8"/> </bean> </property> </bean> </beans>
3.Freemarker静态化页面
- package cn.itcast.core.service.staticpage;
- import java.io.File;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.OutputStreamWriter;
- import java.io.Writer;
- import java.util.Map;
- import javax.servlet.ServletContext;
- import org.springframework.web.context.ServletContextAware;
- import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;
- import freemarker.template.Configuration;
- import freemarker.template.Template;
- /**
- * 生成静态页实现类
- * @author lx
- *
- */
- public class StaticPageServiceImpl implements StaticPageService,ServletContextAware{
- private Configuration conf;
- public void setFreeMarkerConfigurer(FreeMarkerConfigurer freeMarkerConfigurer) {
- this.conf = freeMarkerConfigurer.getConfiguration();
- }
- //静态化方法
- public void productIndex(Map<String,Object> root,Integer id){
- //String dir = "C:\Users\lx\workspace\babasport12\";
- //设置模板的目录
- //conf.setDirectoryForTemplateLoading(dir);
- //输出流 从内存写出去 磁盘上
- Writer out = null;
- try {
- //读进来 UTF-8 内存中
- Template template = conf.getTemplate("productDetail.html");
- //获取Html的路径
- String path = getPath("/html/product/" + id + ".html");//278.html
- File f = new File(path);
- File parentFile = f.getParentFile();
- if(!parentFile.exists()){
- parentFile.mkdirs();
- }
- //输出流
- out = new OutputStreamWriter(new FileOutputStream(f), "UTF-8");
- //处理模板
- template.process(root, out);
- } catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }finally{
- if(out != null){
- try {
- out.close();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- }
- //获取路径
- public String getPath(String name){
- return servletContext.getRealPath(name);
- }
- private ServletContext servletContext;
- @Override
- public void setServletContext(ServletContext servletContext) {
- this.servletContext = servletContext;
- }
- }
Freemarker的配置与使用的更多相关文章
- freemarker属性配置
freemarker属性配置: spring.freemarker.allow-request-override=false # Set whether HttpServletRequest attr ...
- springboot集成freemarker属性配置(不知道是针对于某个版本,2.0后有变动)
freemarker属性配置 freemarker属性配置: spring.freemarker.allow-request-override=false # 设置是否允许HttpServletReq ...
- Java: FreeMarker的配置和使用
初学什么都不可以忽略的地方就是这个东西的官方网站:http://freemarker.org/.下载或者API都可以参考这里. FreeMarker是什么 非常的简单明了.FreeMarker是一个j ...
- freemarker springmvc配置异常
异常信息 java.lang.IllegalAccessError: tried to access method freemarker.ext.servlet.AllHttpScopesHashMo ...
- freemarker配置信息
<!-- <!– freemarker的配置 –> <bean id="freemarkerConfigurer" class="org.spr ...
- springMVC配置freemarker
这里呢,我首先来说明一下写该篇的目的. 我最近要用到freemarker因此研究了一下这个东西. 先来说说如何配置吧. 1.jar包.地址见下链接. http://pan.baidu.com/s/1j ...
- SpringMvc多视图配置(jsp、velocity、freemarker) velocity在springmvc.xml配置VelocityViewResolver,VelocityConfigurer,FreeMarkerConfigurer,FreeMarkerViewResolver
?xml version="1.0"encoding="UTF-8"?> <beans xmlns="http://www.springf ...
- springboot配置server相关配置&整合模板引擎Freemarker、thymeleaf&thymeleaf基本用法&thymeleaf 获取项目路径 contextPath 与取session中信息
1.Springboot配置server相关配置(包括默认tomcat的相关配置) 下面的配置也都是模板,需要的时候在application.properties配置即可 ############## ...
- spring配置freemarker
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.spr ...
随机推荐
- File Searching
Description Have you ever used file searching tools provided by an operating system? For example, in ...
- oracle数据库之PL/SQL 块结构和组成元素
一.PL/SQL 块 (一)PL/SQL 程序由三个块组成,即声明部分.执行部分.异常处理部分 PL/SQL 块的结构如下: 1.DECLARE /* 声明部分: 在此声明 PL/SQL 用到的变量, ...
- 总结get和post区别
参考博文: 浅谈HTTP中Get与Post的区别 1. 数据传递方向: Get是向服务器发索取数据的一种请求,Post是向服务器提交数据的一种请求 (都是请求,并不是一个取一个发) Get:①用于获取 ...
- 关于GenericJDBCException的问题
在spring和hibernate整合的初步阶段,还没有编辑hibernate.cfg.xml这个文件,只有一个beans.xml文件.此时遇到了一个bug. Exception in thread ...
- II 3.1 连接到服务器
II 3.1 连接到服务器 package socket; import java.io.IOException; import java.io.InputStream; import java.ne ...
- 一个demo让你彻底理解Android中触摸事件的分发
注:本文涉及的demo的地址:https://github.com/absfree/TouchDispatch 1. 触摸动作及事件序列 (1)触摸事件的动作 触摸动作一共有三种:ACTION_DOW ...
- <Effective C++>读书摘要--Designs and Declarations<一>
<Item 18> Make interfaces easy to use correctly and hard to use incorrectly 1.That being the c ...
- 最近面试前端面试题整理(css部分)
对最近面试的面试题坐下总结: 一,css部分 1,html元素的垂直居中 答案: <div id="box"> <div> 测试 </div> ...
- cacti 安装perl 和XML::Simple
一.安装perl #tar zxvf perl-5.20.1.tar.gz #cd perl-5.20.1 #./Configure -de #make #make test #make in ...
- tcp发送缓冲区中的数据都是由产生数据的进程给推送到ip层还是有定时任务触发?
和几个变量有非常大的关系 发送缓冲区的大小,如何单独设置一个socket的发送缓冲区 socketopt 发送缓冲区中的数据,如果被拥塞窗口限制住了,那么这些数据可能就放在tcpbuffer里的,此时 ...