参考:http://viralpatel.net/blogs/spring-mvc-freemarker-ftl-example/

上图:

目录层级:

启动后的访问地址:http://localhost:8080/Freemarker_SpringMVC_example/index.html

源码:链接:http://pan.baidu.com/s/1bTtvEQ 密码:k00e

User.java:

 package net.viralpatel;

 public class User {
private String firstname;
private String lastname; public User() {
} public String getFirstname() {
return firstname;
} public void setFirstname(String firstname) {
this.firstname = firstname;
} public String getLastname() {
return lastname;
} public void setLastname(String lastname) {
this.lastname = lastname;
} public User(String firstname, String lastname) {
this.firstname = firstname;
this.lastname = lastname; } // Add Getter and Setter methods }

UserController.java:

 package net.viralpatel;

 import java.util.ArrayList;
import java.util.List; import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
/**
* 参考自:http://viralpatel.net/blogs/spring-mvc-freemarker-ftl-example/
* 本地服务启动后访问地址:http://localhost:8080/Freemarker_SpringMVC_example/index.html
*
* @author Wei
* @time 2016年12月14日 下午4:15:43
*/
@Controller
public class UserController {
/**
* Static list of users to simulate Database
*/
private static List<User> userList = new ArrayList<User>(); //Initialize the list with some data for index screen
static {
userList.add(new User("Bill", "Gates"));
userList.add(new User("Steve", "Jobs"));
userList.add(new User("Larry", "Page"));
userList.add(new User("Sergey", "Brin"));
userList.add(new User("Larry", "Ellison"));
} /**
* Saves the static list of users in model and renders it
* via freemarker template.
*
* @param model
* @return The index view (FTL)
*/
@RequestMapping(value = "/index", method = RequestMethod.GET)
public String index(@ModelAttribute("model") ModelMap model) { model.addAttribute("userList", userList); return "index";
} /**
* Add a new user into static user lists and display the
* same into FTL via redirect
*
* @param user
* @return Redirect to /index page to display user list
*/
@RequestMapping(value = "/add", method = RequestMethod.POST)
public String add(@ModelAttribute("user") User user) { if (null != user && null != user.getFirstname()
&& null != user.getLastname() && !user.getFirstname().isEmpty()
&& !user.getLastname().isEmpty()) { synchronized (userList) {
userList.add(user);
} } return "redirect:index.html";
} }

ftl:

 <html>
<head><title>ViralPatel.net - FreeMarker Spring MVC Hello World</title>
<body>
<div id="header">
<H2>
<a href="http://viralpatel.net"><img height="37" width="236" border="0px" src="http://viralpatel.net/blogs/wp-content/themes/vp/images/logo.png" align="left"/></a>
FreeMarker Spring MVC Hello World
</H2>
</div> <div id="content"> <fieldset>
<legend>Add User</legend>
<form name="user" action="add.html" method="post">
Firstname: <input type="text" name="firstname" /> <br/>
Lastname: <input type="text" name="lastname" /> <br/>
<input type="submit" value=" Save " />
</form>
</fieldset>
<br/>
<table class="datatable">
<tr>
<th>Firstname</th> <th>Lastname</th>
</tr>
<#list model["userList"] as user>
<tr>
<strong><td>${user.firstname}</td></strong> <td>${user.lastname}</td>
</tr>
</#list>
</table> </div>
</body>
</html>

spring-servlet.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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <!-- 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> <context:component-scan base-package="net.viralpatel" /> </beans>

web.xml:

 <?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0"> <display-name>Freemarker_SpringMVC_example</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list> <servlet>
<servlet-name>spring</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<!-- <url-pattern>*.html</url-pattern> -->
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>

springmvc结合freemarker,非自定义标签的更多相关文章

  1. freemarker自己定义标签报错(三)

    freemarker自己定义标签 1.错误描写叙述 freemarker.core.ParseException: Encountered " " at line 14, colu ...

  2. freemarker自己定义标签(二)

    freemarker自己定义标签 1.自己定义标签 通过自己定义标签,写一个反复指定字符串 2.实现源代码 <html> <head> <meta http-equiv= ...

  3. freemarker自己定义标签报错(二)

    freemarker自己定义标签 1.错误描写叙述 freemarker.core.ParseException: Unexpected end of file reached. at freemar ...

  4. freemarker自己定义标签报错(七)

    1.错误描写叙述 六月 09, 2014 11:11:09 下午 freemarker.log.JDK14LoggerFactory$JDK14Logger error 严重: Template pr ...

  5. freemarker自己定义标签(一)

    freemarker自己定义标签 1.自己定义标签说明 宏变量存储模板片段能够被用作自己定义指令macro 2.演示样例说明 <html> <head> <meta ht ...

  6. SpringMVC和Freemarker整合,带自定义标签的使用方法

    SpringMVC和Freemarker整合,带自定义标签的使用方法. [参考来源:http://www.360doc.com/content/14/1225/14/1007797_435663342 ...

  7. Spring MVC 环境搭建(maven+SpringMVC+mybatis+Freemarker)

    Spring MVC 环境搭建(maven+SpringMVC+mybatis+Freemarker) 一.准备工作 1.Eclipse Java EE IDE(4.4.1) 2.JDK 3.Tomc ...

  8. spring boot+freemarker+spring security标签权限判断

    spring boot+freemarker+spring security标签权限判断 SpringBoot+SpringSecurity+Freemarker项目中在页面上使用security标签 ...

  9. SpringMVC 集成 Freemarker 模板引擎

    本文通过 maven 项目中集成 1.引入 SpringMVC 与 Freemarker 需要的依赖 <!-- SpringMVC --> <dependency> <g ...

随机推荐

  1. 解决ubuntu 14.04删ibus导致系统设置项目的损失后,,退出关机问题是不正常的

    已安装sogou第一篇文章.嫌ibus碍眼,习惯性删.我没想到会引起一系列问题.退出登录用户崩溃.系统设置非常多的项目. 它似乎ubuntu 14.04于ibus它不能被卸载(卸载iBus本身没有问题 ...

  2. 折叠Collapse插件

    实例 折叠(Collapse)插件可以很容易地让页面区域折叠起来.无论您用它来创建折叠导航还是内容面板,它都允许很多内容选项. .collapse 隐藏内容. .collapse.in 显示内容. . ...

  3. SQL Server 2008如何导出带数据的脚本文件

    第一步,选中需要导出脚本的数据库,右键选中 第二步,选取弹出菜单中的任务-生成脚本选项(会弹出一SQL生成脚本的向导) 第三步,在向导中点击下一步,弹出选择数据库界面(默认是自己之前选中的数据库),把 ...

  4. [C++基础]在构造函数内部调用构造函数

    看下面的面试题: #include <iostream> using namespace std; struct CLS { int m_i; CLS( int i ) : m_i(i){ ...

  5. PHP基础示例:用正则表达式修改配置信息

    各php工作原理图: 以下是扯代码时间: <?php //配置文件信息 define("HOST","localhost"); //主机名 define( ...

  6. Android 三星手机不能调起应用市场

    Uri uri; if (hasAnyMarketInstalled(getContext())) { uri = Uri.parse("market://details?id=" ...

  7. php实现两分法查找

    两分法查找的前提:顺序方式存储,而且必须是排好序 直接上代码: function search($array, $target, $low = 0, $high = 0){ $len = count( ...

  8. 什么是Web前端,Web前端是做什么的?

    什么是Web前端 Web前端,顾名思义是来做Web的前端的.而Web前端开发应该就是来开发基于Web前端的相关应用的或者说是来开发前端的.那么,前端又是什么呢?我们这里所说的前端泛指Web前端,也就是 ...

  9. javascript 中字符串之比较

    <script type="text/javascript"> var string1="apple"; var string2="Ban ...

  10. sql server 2012 镜像和出现的问题

    镜像安装的环境: 主机:win server 2012 , sql server 2012 ,ip:192.168.1.189  PC账户:administrator 备机:win server 20 ...