SpringMVC+FreeMarker的使用
来源:互联网 发布:天津二手房成交量数据 编辑:程序博客网 时间:2024/05/17 22:04
之前用Java写Web都是用的HTML静态页面,重复的代码很多,基本是都是复制的,让人看起来很乱而且不好维护,FreeMarker使得代码更加整洁、更易维护,而且简化了后端的处理逻辑,简单易上手,下面介绍一下基本使用方法。
一、POM 中增加依赖
<dependency> <groupId>org.freemarker</groupId> <artifactId>freemarker</artifactId> <version>2.3.23</version> </dependency>
二、在xml配置bean
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer"> <property name="templateLoaderPath" value="/WEB-INF/view/" /> <property name="freemarkerSettings"> <props> <prop key="template_update_delay">0</prop> <prop key="default_encoding">UTF-8</prop> <prop key="number_format">0.##########</prop> <prop key="datetime_format">yyyy-MM-dd HH:mm:ss</prop> <prop key="classic_compatible">true</prop> <prop key="template_exception_handler">ignore</prop> </props> </property> </bean> <bean id="viewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver"> <property name="suffix"> <value>.ftl</value> </property> <property name="contentType" value="text/html;charset=UTF-8"></property> </bean></beans>
三、模板文件示例,ftl文件,freemarker.ftl
<!DOCTYPE html><html><head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>${title}</title></head><body>${content}</body></html>
四、Controler示例,通过ModelAndView引用一个模板,使用的时候名字不带后缀
@Controller@RequestMapping("/index/")public class FreeMarkerController { private Logger LOGGER = LoggerFactory.getLogger(FreeMarkerController.class); //批量更新DQC数据用的 @RequestMapping(value = "test", produces = "text/plain;charset=UTF-8", method = RequestMethod.GET) public ModelAndView freeMarkerTest() { ModelAndView mv = new ModelAndView("freemarker"); mv.addObject("title", "Spring MVC And Freemarker"); mv.addObject("content", " Hello world , test my first spring mvc ! "); return mv; }}
Ref:
http://www.iteye.com/topic/1121801
http://blog.csdn.net/yakson/article/details/9813531
阅读全文
0 0
- SpringMVC+FreeMarker的使用
- SpringMVC + Freemarker 模版的使用
- springMVC整合freemarker的使用
- Freemarker使用教程-基于Springmvc的Freemarker实例
- SpringMvc使用Freemarker时的配置文件
- SpringMVC的架设,使用Freemarker模板引擎
- SpringMvc使用Freemarker时的配置文件
- SpringMVC + freemarker使用
- springmvc使用freemarker
- 【Freemarker】SpringMVC使用Freemarker作为视图解析
- SpringMVC+Maven+FreeMarker+MyBatis 使用
- Ibatis +Springmvc+Freemarker+Dwr 搭建之后的使用
- springmvc整合freemarker时,freemarker使用include的根路径的问题
- springmvc+freemarker的简单demo
- springmvc+freemarker的简单demo
- springmvc整合freemarker,使用ftl文件
- SpringMVC中使用FreeMarker生成Word文档
- SpringMVC中使用FreeMarker生成Word文档
- swiper-wrapper轮滑组件(多组轮滑界面)无效问题解决
- Maven快速使用教程(一)
- HQL数据查询基础
- Maven快速使用教程(二) spring boot 项目构建
- 多个css合在一起写
- SpringMVC+FreeMarker的使用
- HBase-HMaster源码分析
- 实现随机10个20至200之间的随机数
- KMP(2)-KMP算法原理与匹配部分.
- C#--添加对象前
- 【Cocos2d-x】开发实战-Cococs2d-x中的菜单
- 手机归属地查询----自学了这么久总算可以不copy他人代码写出一款APP了 虽然有点丑
- shiro_spring.xml
- 位运算符