还是先贴出该例子存于github上的位置

https://github.com/lemonbar/spring-mvc-freemarker

Sping-Framework 的官方文档简单列出了在spring-mvc中如何使用freemarker, 但是相对来说提供的信息和例子太少, 所以在这给出一个详细的例子.

注:我是在maven基础上进行的构建, 很多解释已经在代码中加了, 所以尽量贴代码.

FreeMarker Site: http://freemarker.org/

1. 整个文件夹结构

src
main
java
com.lemon.spring.controller
GreetingController
webapp
WEB-INF
ftl
footer.ftl
header.ftl
login.ftl
welcome.ftl
root-context.xml
web.xml
pom.xml

2. pom.xml内容

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.lemon.spring</groupId>
<artifactId>spring-mvc-freemarker</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>spring-mvc-freemarker Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
<spring.framework.version>4.0.6.RELEASE</spring.framework.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.framework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.framework.version}</version>
</dependency>
<!--context-support should be included for freemarker bean definition.-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.framework.version}</version>
</dependency> <dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.20</version>
</dependency>
</dependencies>
<build>
<finalName>spring-mvc-freemarker</finalName>
</build>
</project>

3. web.xml

<web-app 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_3_0.xsd"
version="3.0"> <context-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/root-context.xml</param-value>
</context-param>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value></param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>

4. root-context.xml内容

<?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: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.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <context:component-scan base-package="com.lemon.spring"/>
<!-- 添加注解驱动 -->
<mvc:annotation-driven enable-matrix-variables="true"/>
<!--<context:annotation-config/>-->
<!-- 允许对静态资源文件的访问 -->
<mvc:default-servlet-handler /> <!--freemarker config-->
<bean id="freemarkerConfig"
class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
<property name="templateLoaderPath" value="/WEB-INF/ftl/"/>
</bean> <!--
View resolvers can also be configured with ResourceBundles or XML files.
If you need different view resolving based on Locale, you have to use the resource bundle resolver.
-->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
<property name="cache" value="true"/>
<property name="prefix" value=""/>
<property name="suffix" value=".ftl"/>
</bean> </beans>

5. GreetingController.java内容

/*
* Copyright (c) 2014 General Electric Company. All rights reserved.
*
* The copyright to the computer software herein is the property of
* General Electric Company. The software may be used and/or copied only
* with the written permission of General Electric Company or in accordance
* with the terms and conditions stipulated in the agreement/contract
* under which the software has been supplied.
*/
package com.lemon.spring.controller; import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView; import java.util.Arrays;
import java.util.List; @Controller
public class GreetingController { @RequestMapping(value = "/greeting/{user}", method = RequestMethod.GET)
public String greeting(@PathVariable String user, Model model) {
List<String> userList = Arrays.asList(user.split("-"));
//userList is the variable name, used in ftl file.
model.addAttribute("userList", userList);
return "welcome";
} @RequestMapping(value = "/greeting", method = RequestMethod.POST)
public ModelAndView greeting(@RequestParam("user") String user) {
List<String> userList = Arrays.asList(user.split("-"));
ModelAndView result = new ModelAndView("welcome");
//userList is the variable name, used in ftl file.
result.addObject("userList", userList);
return result;
} @RequestMapping("/login")
public String login() {
return "login";
}
}

6. welcome.ftl

<html>
<head>
<title>Welcome!</title>
</head>
<body>
<#include "./header.ftl"/>
<table border="1">
<tr><th>Name</th><th>Price</th></tr>
<#list userList as user>
<tr><th>${user}</th><th>1.0</th></tr>
</#list>
</table>
<!--use include to include another ftl file content in this file.-->
<#include "./footer.ftl"/>
</body>
</html>

7. login.ftl

<#import "/spring.ftl" as spring/>
<html>
<head>
<title>Please input your names, seperator with '-' char.</title>
</head>
<body>
<#include "./header.ftl"/>
<form action="greeting" method="POST">
Names:
<input type="text" name="user"/><br>
<input type="submit" value="submit"/>
</form>
<!--use include to include another ftl file content in this file.-->
<#include "./footer.ftl"/>
</body>
</html>

8. footer.ftl

<hr>
<i>
Copyright (c) 2014 <a href="http://www.acmee.com">Acmee
Inc</a>,
<br>
All Rights Reserved.
</i>

9. header.ftl

<h1>
This is header!
</h1>
<hr>

[Spring MVC]学习笔记--FreeMarker的使用的更多相关文章

  1. Spring MVC 学习笔记一 HelloWorld

    Spring MVC 学习笔记一 HelloWorld Spring MVC 的使用可以按照以下步骤进行(使用Eclipse): 加入JAR包 在web.xml中配置DispatcherServlet ...

  2. Spring MVC 学习笔记12 —— SpringMVC+Hibernate开发(1)依赖包搭建

    Spring MVC 学习笔记12 -- SpringMVC+Hibernate开发(1)依赖包搭建 用Hibernate帮助建立SpringMVC与数据库之间的联系,通过配置DAO层,Service ...

  3. Spring MVC 学习笔记11 —— 后端返回json格式数据

    Spring MVC 学习笔记11 -- 后端返回json格式数据 我们常常听说json数据,首先,什么是json数据,总结起来,有以下几点: 1. JSON的全称是"JavaScript ...

  4. Spring MVC 学习笔记10 —— 实现简单的用户管理(4.3)用户登录显示全局异常信息

    </pre>Spring MVC 学习笔记10 -- 实现简单的用户管理(4.3)用户登录--显示全局异常信息<p></p><p></p>& ...

  5. Spring MVC 学习笔记9 —— 实现简单的用户管理(4)用户登录显示局部异常信息

    Spring MVC 学习笔记9 -- 实现简单的用户管理(4.2)用户登录--显示局部异常信息 第二部分:显示局部异常信息,而不是500错误页 1. 写一个方法,把UserException传进来. ...

  6. Spring MVC 学习笔记8 —— 实现简单的用户管理(4)用户登录

    Spring MVC 学习笔记8 -- 实现简单的用户管理(4)用户登录 增删改查,login 1. login.jsp,写在外面,及跟WEB-INF同一级目录,如:ls Webcontent; &g ...

  7. Spring MVC 学习笔记2 - 利用Spring Tool Suite创建一个web 项目

    Spring MVC 学习笔记2 - 利用Spring Tool Suite创建一个web 项目 Spring Tool Suite 是一个带有全套的Spring相关支持功能的Eclipse插件包. ...

  8. Spring MVC 学习笔记1 - First Helloworld by Eclipse【& - java web 开发Tips集锦】

    Spring MVC 学习笔记1 - First Helloworld by Eclipse reference:http://www.gontu.org 1. 下载 Spring freamwork ...

  9. spring MVC学习笔记

    为开发团队选择一款优秀的MVC框架是件难事儿,在众多可行的方案中决择需要很高的经验和水平.你的一个决定会影响团队未来的几年.要考虑方面太多: 1.简单易用,以提高开发效率.使小部分的精力在框架上,大部 ...

  10. Spring MVC 学习笔记(整理)

    SpringMVC学习 1.概述 Spring MVC是一种基于Java实现MVC设计模式的请求驱动类型的轻量级Web框架,即使用了MVC架构模式的思想,将web层进行解耦,基于请求-响应模型帮助我们 ...

随机推荐

  1. [Functional Programming] Randomly Pull an Item from an Array with the State ADT (Pair)

    Functor composition is a powerful concept that arises when we have one Functor nested in another Fun ...

  2. JAVA利用HttpClient进行POST请求(HTTPS)

    目前,要为另一个项目提供接口,接口是用HTTP URL实现的,最初的想法是另一个项目用jQuery post进行请求. 但是,很可能另一个项目是部署在别的机器上,那么就存在跨域问题,而JQuery的p ...

  3. 倍福TwinCAT(贝福Beckhoff)常见问题(FAQ)-如何配置虚拟轴 TC2

    右击NC- Configuration,然后Append Task,然后右击Axis,Append Axis   轴的类型可以分为:Continuous Axis,默认的类型,NC可以连续闭环控制该轴 ...

  4. $HTTP_RAW_POST_DATA 与$_POST

    出处:http://blog.163.com/gwo-cce@126/blog/static/325736492008101142422345/ 这是手册里写的 总是产生变量包含有原始的 POST 数 ...

  5. ssh2学习-applicationContext.xml文件配置-----<context:annotation-config/>详解

    当我们需要使用BeanPostProcessor时,直接在Spring配置文件中定义这些Bean显得比较笨拙,例如: 使用@Autowired注解,必须事先在Spring容器中声明AutowiredA ...

  6. 记录一个奇妙的Bug, -1 &gt;= 2 ?

    直接上代码: #include <iostream> #include <vector> using namespace std; int main() { vector< ...

  7. 重新 java 对象的 equals 和 hashCode 方法的建议和示例代码

    equals 方法 equals 方法需要满足的规范: 自反性: 对于任意非空引用 x, x.equals(x) 应该返回 true; 对称性: 对于任意引用, 当且仅当 x.equals(y) == ...

  8. 使用history.pushState()和popstate事件实现AJAX的前进、后退功能

    上一篇文章中.我们使用location.hash来模拟ajax的前进后退功能.使用location.hash存在以下几个问题: 1.使用location.hash会导致地址栏的url发生变化.用户体验 ...

  9. (一)Linux——Linux基本概念

    Linux是一种自由和开放源码的类UNIX操作系统,使用Linux内核.目前存在着许多不同的Linux发行版,可安装在各种各样的电脑硬件设备,从手机.平板电脑.路由器和影音游戏控制台,到桌上型电脑,大 ...

  10. (二)Maven 基本概念——依赖、生命周期、仓库管理、聚合&继承

    1. 依赖配置 1.1 依赖配置主要包含如下元素: <!--添加依赖配置--> <dependencies> <!--项目要使用到junit的jar包,所以在这里添加ju ...