Freemarker与Springmvc
1.导入springmvc和freemarker的jar包
2.web.xml中配置Spring和Springmvc
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <!-- Spring -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:SpringIOC.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> <!-- Spring MVC -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:SpringmvcIOC.xml</param-value>
</init-param>
<load-on-startup></load-on-startup>
</servlet> <servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping> </web-app>
3.建立ftl模板(WEB-INF/view/hello.ftl)
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>${title}</title>
</head>
<body>
${content}
<br><hr>
<#list citys as c>
索引:${c_index}; 内容:${c}<br>
</#list> <br><hr>
<#if age gt 21>
age is ${age}
<#else>
age${age} 小于21
</#if> <#if txt??>${txt[5..20]}</#if>
<br><hr>
${date?string("yyyy-mm-dd")!"null"} <#macro m1> <#--定义指令m1 -->
<b>aaabbbccc</b>
<b>dddeeefff</b>
</#macro>
<@m1 /><@m1 /> <#--调用上面的宏指令 --> <#macro m2 a b c >
${a}--${b}--${c}
</#macro>
<@m2 a="老高" b="老张" c="老马" />
<#--
测试include指令:
<#include "included.txt" />
-->
</body>
</html>
4.在SpringmvcIOC.xml中配置freemarker(也可以在SpringIOC.xml中配置,SpringIOC.xml文件中无任何配置)
《SpringIOC容器的范围已经大于SpringmvcIOC容器的作用范围;Springmvc的bean可以使用Spring中的,反之不可》
<?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:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
<!--
<context:component-scan base-package="com.wzy.controller"></context:component-scan>
--> <bean id="m" class="com.wzy.controller.Main">
</bean> <!-- Freemarker配置 -->
<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"></prop>
<prop key="default_encoding">UTF-</prop>
<prop key="number_format">.##########</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> <!-- 地址转发器
<bean name="HelloAction" class="HelloWordController.HelloWordController" />
<bean id="urlMapping"
class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
映射URL地址
<prop key="/hello.do">HelloAction</prop>
</props>
</property> </bean>
-->
</beans>
5.控制器
package com.wzy.controller; import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping; @Controller
public class Main { @RequestMapping("hello")
public String hello(Map mv) {
mv.put("title", "s_f");
mv.put("content", "當前時間為: "+new Date());
List l = new ArrayList();
l.add("北京");
l.add("上海");
l.add("广州");
l.add("南京");
mv.put("citys", l);
mv.put("age", );
mv.put("txt", "14:17:37.724 "
+ "[http-nio-8080-exec-8] DEBUG o.s.web.servlet.DispatcherServlet -"
+ " Successfully completed request");
mv.put("date", new Date());
return "hello";//返回view/hello.ftl
}
}

Freemarker与Springmvc的更多相关文章
- FreeMarker 整合 springmvc
一.添加 jar 包 <dependency> <groupId>org.freemarker</groupId> <artifactId>freema ...
- springmvc使用freemarker
首先需要添加freemarker.jar到项目,如果项目中有spring或者spirngmvc,需要整合,首先配置freemarkerConfig,代码结构如下 <!-- 设置freeMarke ...
- FreeMarker与Spring MVC的结合应用
Freemarker是一种基于java的模板引擎.SpringMVC对FreeMarker进行一些配置的支持,能够利用Freemarker只关注表现层以及Spring MVC的三层分离的特点,向前端输 ...
- Spring4 SpringMVC Hibernate4 Freemaker 集成示例
变更更正(2014-05-30 13:47:22):一些IDE在web.xml我们会报告这个错误: cvc-complex-type.2.4.a: Invalid content was found ...
- FreeMarker 快速入门
FreeMarker 快速入门 FreeMarker是一个很值得去学习的模版引擎.它是基于模板文件生成其他文本的通用工具.本章内容通过如何使用FreeMarker生成Html web 页面 和 代码自 ...
- FreeMarker生成Word文档
FreeMarker简介: FreeMarker是一款模板引擎:即一种基于模板和要改变的数据,并用来生成输出文本(HTML网页.电子邮件.配置文件.源代码等)的通用工具,它不是面向最终用户的,而是一个 ...
- Spring4 SpringMVC Hibernate4 Freemaker 整合样例
更正改动(2014-05-30 13:47:22):有的IDE中web.xml会报这个错: cvc-complex-type.2.4.a: Invalid content was found star ...
- FreeMarker配置详解
首先需要添加freemarker.jar到项目,如果项目中有spring或者spirngmvc,需要整合,首先配置freemarkerConfig,代码结构如下: <!-- 设置 ...
- 如何玩转最新的项目的搭配springmvc+mybatis+Redis+Nginx+tomcat+mysql
上一次完成nginx+tomcat组合搭配,今天我们就说说,这几个软件在项目中充当的角色: 要想完成这几个软件的组合,我们必须知道和熟悉应用这个框架, 一: Nginx:在项目中大多数作为反向代理服务 ...
随机推荐
- 创业公司招php商城开发者
众筹 电商 已经融资100W美元 职位要求1.对PHP编程熟悉程度以上,有电商相关开发经验优先:2.熟悉lnmp相关配套搭建运维,开发;熟悉linux 使用3.对数据结构和算法设计有较深刻的理解:4 ...
- 背水一战 Windows 10 (17) - 动画: ThemeTransition(过渡效果)
[源码下载] 背水一战 Windows 10 (17) - 动画: ThemeTransition(过渡效果) 作者:webabcd 介绍背水一战 Windows 10 之 动画 ThemeTrans ...
- 修改MySQL自动递增值
alter table tablename auto_increment=num 其中tablename为表的名称,num为要设置的新的自动递增值,此时再Insert一条数据,自动递增值即为num,不 ...
- JAVA 链表操作:单链表和双链表
主要讲述几点: 一.链表的简介 二.链表实现原理和必要性 三.单链表示例 四.双链表示例 一.链表的简介 链表是一种比较常用的数据结构,链表虽然保存比较复杂,但是在查询时候比较便捷,在多种计算机语言都 ...
- 2个很有趣、耐思考的C语言算法
1. 输入10个整数,任意相邻的两个数不同,输出所有的递增,递减序列 比如: 输入:1 5 9 8 12 21 3 0 -1 9 输出: 1 5 9 9 8 8 12 21 21 3 0 -1 -1 ...
- 初学C++ 之 auto关键字(IDE:VS2013)
/*使用auto关键字,需要先赋初值,auto关键字是会根据初值来判断类型*/ auto i = ; auto j = ; cout << "auto i = 5" & ...
- 从零开始学 Java - CentOS 下 Nginx + Tomcat 配置负载均衡
为什么现在有非常多的聪明人都在致力于互联网? 最近在读埃隆·马斯克传记,他说「我认为现在有非常多的聪明人都在致力于互联网」. 仔细一想,好像真的是这样的. 我问了自己一个问题:如果你不敲代码了,你能做 ...
- “.”(十六进制值 0x00)是无效的字符解决方案
自从我们的项目数据层从读取数据库改为读取接口服务后,经常会出现一些类似于的错误.我们的数据结构如下所示 <type><![CDATA[gp]]></type> &l ...
- 今天再给大家带点html5前端开发的干货模板“text/tpl”怎么用 script template怎么用
text/tpl 顾名思义就是模板,其实和C++模板函数类似的作用,就是利用他生成一个HMTL内容,然后append或者替换html里面 有什么好处,假如后端返回来的数据都是一样的,但是需要生成不同的 ...
- iOS之获取屏幕尺寸
//app尺寸,去掉状态栏 CGRect appRect = [UIScreenmainScreen].applicationFrame; NSLog(@"%f, %f, %f,%f&quo ...