[Spring MVC]学习笔记--FreeMarker的使用
还是先贴出该例子存于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的使用的更多相关文章
- Spring MVC 学习笔记一 HelloWorld
Spring MVC 学习笔记一 HelloWorld Spring MVC 的使用可以按照以下步骤进行(使用Eclipse): 加入JAR包 在web.xml中配置DispatcherServlet ...
- Spring MVC 学习笔记12 —— SpringMVC+Hibernate开发(1)依赖包搭建
Spring MVC 学习笔记12 -- SpringMVC+Hibernate开发(1)依赖包搭建 用Hibernate帮助建立SpringMVC与数据库之间的联系,通过配置DAO层,Service ...
- Spring MVC 学习笔记11 —— 后端返回json格式数据
Spring MVC 学习笔记11 -- 后端返回json格式数据 我们常常听说json数据,首先,什么是json数据,总结起来,有以下几点: 1. JSON的全称是"JavaScript ...
- Spring MVC 学习笔记10 —— 实现简单的用户管理(4.3)用户登录显示全局异常信息
</pre>Spring MVC 学习笔记10 -- 实现简单的用户管理(4.3)用户登录--显示全局异常信息<p></p><p></p>& ...
- Spring MVC 学习笔记9 —— 实现简单的用户管理(4)用户登录显示局部异常信息
Spring MVC 学习笔记9 -- 实现简单的用户管理(4.2)用户登录--显示局部异常信息 第二部分:显示局部异常信息,而不是500错误页 1. 写一个方法,把UserException传进来. ...
- Spring MVC 学习笔记8 —— 实现简单的用户管理(4)用户登录
Spring MVC 学习笔记8 -- 实现简单的用户管理(4)用户登录 增删改查,login 1. login.jsp,写在外面,及跟WEB-INF同一级目录,如:ls Webcontent; &g ...
- Spring MVC 学习笔记2 - 利用Spring Tool Suite创建一个web 项目
Spring MVC 学习笔记2 - 利用Spring Tool Suite创建一个web 项目 Spring Tool Suite 是一个带有全套的Spring相关支持功能的Eclipse插件包. ...
- 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 ...
- spring MVC学习笔记
为开发团队选择一款优秀的MVC框架是件难事儿,在众多可行的方案中决择需要很高的经验和水平.你的一个决定会影响团队未来的几年.要考虑方面太多: 1.简单易用,以提高开发效率.使小部分的精力在框架上,大部 ...
- Spring MVC 学习笔记(整理)
SpringMVC学习 1.概述 Spring MVC是一种基于Java实现MVC设计模式的请求驱动类型的轻量级Web框架,即使用了MVC架构模式的思想,将web层进行解耦,基于请求-响应模型帮助我们 ...
随机推荐
- hexo 使用教程
hexo 使用教程 这个早就用起来了,写给需要的小伙伴 mayufo.github.io 先扔出自己的地址 安装 想玩hexo,需要安装以下应用 git node 安装完成在终端输入 $ npm in ...
- WRTNode(MT7620n)USB启动总结
一.改动mt7620.dtsi,去掉默认的bootargs,kernel_menuconfig取消buildin的command line 二.kernel_menuconfig增加scsi驱动.US ...
- Unity3D在WebPlayer模式下的异常上报探索
原地址:http://www.cnblogs.com/hisiqi/archive/2013/07/21/3203527.html 我们知道,Unity3D在WebPlayer的发布模式下是沙箱环境中 ...
- 【高德地图API】从零開始学高德JS API(二)地图控件与插件——測距、圆形编辑器、鼠标工具、地图类型切换、鹰眼鱼骨
不管是控件还是插件,都是在一级API接口的基础上,进行二次开发,封装的一系列更加便于开发人员使用.降低开发人员工作量的二级API接口.除了官方通用的鱼骨.鹰眼控件,还有大量官方开发的地图插件,相似谷歌 ...
- Sonatype Nexus
Maven 常用的仓库管理http://zh.wikipedia.org/wiki/Apache_Maven
- Android 四大组件学习之BroadcastReceiver二
上节学习了怎样创建一个广播.也尝试接受系统打电话的广播. 本节课学习怎样自己定义广播.自己定义广播实质上也就是创建一个发送广播者,创建一个接受该广播者. 那我们就開始行动吧. 先创建一个发送广播的应用 ...
- C# Enum,Int,String,之间及bool与int之间的转换
枚举类型的基类型是除 Char 外的任何整型,所以枚举类型的值是整型值. Enum 提供一些实用的静态方法: (1)比较枚举类的实例的方法 (2)将实例的值转换为其字符串表示形式的方法 (3)将数字的 ...
- js - 类模拟
JavaScript 中并没有真正的类,但JavaScript 中有 构造函数 和 new 运算符. - 任何JavaScript 函数都可以用做构造函数, - 构造函数必须使用 new 运算符来创建 ...
- ubuntu14.04 flash driver 安装
直接将14.04镜像直接放到flash driver 中 然后在bios设置flash driver 优先启动 然后格出一块盘给ubuntu 安装使用 按操作要求一路点下去就可以,记得选windows ...
- DMA摘记
1.特点 PIO模式下硬盘和内存之间的数据传输是由CPU来控制的:而在DMA模式下,CPU只须向DMA控制器下达指令,让DMA控制器来处理数据的传送,数据传送完毕再把信息反馈给CPU,这样就很大程度上 ...