spring mvc 集成freemarker模板
主要使用到的jar 文件:spring mvc +freemarker.jar
第一步:spring mvc 集成 freemarker
- <!-- 定义跳转的文件的前后缀 ,视图模式配置-->
- <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
- <!-- 这里的配置我的理解是自动给后面action的方法return的字符串加上前缀和后缀,变成一个 可用的url地址 -->
- <property name="prefix" value="/backstage/jsp/" />
- <property name="suffix" value=".jsp" />
- <property name="viewClass" value="org.springframework.web.servlet.view.InternalResourceView"/>
- <property name="order" value="1"/>
- </bean>
- <!-- 配置freeMarker视图解析器 -->
- <bean id="viewResolverFtl" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
- <property name="viewClass" value="org.springframework.web.servlet.view.freemarker.FreeMarkerView"/>
- <property name="contentType" value="text/html; charset=UTF-8"/>
- <property name="exposeRequestAttributes" value="true" />
- <property name="exposeSessionAttributes" value="true" />
- <property name="exposeSpringMacroHelpers" value="true" />
- <property name="cache" value="true" />
- <property name="suffix" value=".ftl" />
- <property name="order" value="0"/>
- </bean>
- <!-- 配置freeMarker的模板路径 -->
- <bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
- <property name="templateLoaderPath" value="/backstage/jsp/"/>
- <property name="freemarkerVariables">
- <map>
- <entry key="xml_escape" value-ref="fmXmlEscape" />
- </map>
- </property>
- <property name="defaultEncoding" value="UTF-8"/>
- <property name="freemarkerSettings">
- <props>
- <prop key="template_update_delay">3600</prop>
- <prop key="locale">zh_CN</prop>
- <prop key="datetime_format">yyyy-MM-dd HH:mm:ss</prop>
- <prop key="date_format">yyyy-MM-dd</prop>
- <prop key="number_format">#.##</prop>
- </props>
- </property>
- </bean>
- <bean id="fmXmlEscape" class="freemarker.template.utility.XmlEscape"/>
【重点:在JSP和Freemarker的配置项中都有一个order property,上面例子是把freemarker的order设置为0,jsp为1。意思是找view时,先找ftl文件,再找jsp文件做为视图。】
第二步:controller代码
- package com.wlsq.controller;
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RequestMethod;
- import org.springframework.web.servlet.ModelAndView;
- @Controller
- @RequestMapping(value="/ftl")
- public class FreemarkerTest {
- @RequestMapping(value="/index",method=RequestMethod.GET)
- public ModelAndView index() {
- ModelAndView view = new ModelAndView("TestFreemarker/index");
- view.addObject("message", "Say hi for Freemarker.");
- return view;
- }
- }
第三步:TestFreemarker/index.ftl
- <Html>
- <head>
- <title>${message}</title>
- </head>
- <body>
- <b>${message}</b>
- <body>
- </html>
spring mvc 集成freemarker模板的更多相关文章
- spring mvc集成freemarker使用
freemarker作为视图技术出现的比velocity早,想当年struts风靡一时,freemarker作为视图层也风光了一把.但现在velocity作为后起之秀的轻量级模板引擎,更容易得到青睐. ...
- spring mvc集成velocity使用
目前流行的三大页面视图神器是:老牌大哥jsp.后起之秀freemarker和velocity.这里不详细比较这三者的优劣,总体来说,jsp是标配,但后面两个更严格的执行了视图与业务的分离,页面里是不允 ...
- Spring Boot 集成 FreeMarker 详解案例(十五)
一.Springboot 那些事 SpringBoot 很方便的集成 FreeMarker ,DAO 数据库操作层依旧用的是 Mybatis,本文将会一步一步到来如何集成 FreeMarker 以及配 ...
- springboot学习笔记:5.spring mvc(含FreeMarker+layui整合)
Spring Web MVC框架(通常简称为"Spring MVC")是一个富"模型,视图,控制器"的web框架. Spring MVC允许你创建特定的@Con ...
- 【转载】Spring MVC 整合 Freemarker
前言 1.为什么要使用Spring MVC呢? 2.为什么要使用Freemarker呢? 3.为什么不使用Struts2呢? 此示例出现的原因就是发现了struts2的性能太差,所以学习Spring ...
- 【FreeMarker】Spring MVC与FreeMarker整合(二)
前一篇介绍了FreeMarker的基本使用,本例介绍Spring MVC与FreeMarker整合 不熟悉项目搭建,可参考 [FreeMarker]FreeMarker快速入门(一) 整合 1.新建S ...
- Spring MVC整合 freemarker
1.什么是Spring MVC? Spring MVC是一种基于Java的实现了Web MVC设计模式的请求驱动类型的轻量级Web框架,即使用了MVC架构模式的思想,将Web层进行职责解耦,基于请求驱 ...
- Spring MVC集成slf4j-logback
转自: Spring MVC集成slf4j-logback 1. Spring MVC集成slf4j-log4j 关于slf4j和log4j的相关介绍和用法,网上有很多文章可供参考,但是关于logb ...
- Spring mvc+hibernate+freemarker(实战)
Spring mvc+hibernate+freemarker(实战) 博客分类: Spring Spring mvchibernatefreemarkerwebjava 今天我为大家做了一个 sp ...
随机推荐
- 在Eclipse中通过JDBC连接MySQL步骤,非常详细!
通过JDBC连接MySQL基本步骤代码讲解步骤可能遇到的Bug基本步骤JDBC访问MySQL 1.加载JDBC驱动器—>哪个project需要,就添加到该project的jdbc文件夹下,我的j ...
- centos下升级python
1. wget http://www.python.org/ftp/python/3.3.0/Python-3.3.0.tgz 2.解压之后 tar -xzvf Python-3.3.0.tgz 3 ...
- DELETE - 删除一个表中的行
SYNOPSIS DELETE FROM [ ONLY ] table [ WHERE condition ] DESCRIPTION 描述 DELETE 从指明的表里删除满足 WHERE 子句的行. ...
- ionic小白的学习路之目录结构分析、创建组件、创建页面、页面跳转
一. 目录结构分析 hooks:编译cordova 时自定义的脚本命令,方便整合到我们的编译系统和版本控制系统中. node_modules :node 各类依赖包. resources :andro ...
- ASP.NetCore 错误 NU1605 检测到包降级: Microsoft.Data.Sqlite 从 2.2.1 降级到 2.1.0
找到使用的.csproj文件 将 <PackageReference Include="Microsoft.Data.Sqlite" Version="2.1.0& ...
- 编写一个微信小程序
1.创建项目 2.创建目录及文件,结构如下:
- 关于 vertical-align
默认情况下(行内基线位置 = 行内元素最大高度): 如果对这个正方形使用 vertival-align:middle.在最大高度的元素上使用负值(middle = - 50% * 元素高度),可以提升 ...
- c++_奖券数目
奖券数目 有些人很迷信数字,比如带“4”的数字,认为和“死”谐音,就觉得不吉利.虽然这些说法纯属无稽之谈,但有时还要迎合大众的需求.某抽奖活动的奖券号码是5位数(10000-99999),要求其中不要 ...
- LeetCode(80)Remove Duplicates from Sorted Array II
题目 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For ex ...
- 《算法导论》 — Chapter 7 快速排序
序 快速排序(QuickSort)也是一种排序算法,对包含n个数组的输入数组,最坏情况运行时间为O(n^2).虽然这个最坏情况运行时间比较差,但是快速排序通常是用于排序的最佳实用选择,这是因为其平均性 ...