springmvc结合freemarker,非自定义标签
参考: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,非自定义标签的更多相关文章
- freemarker自己定义标签报错(三)
freemarker自己定义标签 1.错误描写叙述 freemarker.core.ParseException: Encountered " " at line 14, colu ...
- freemarker自己定义标签(二)
freemarker自己定义标签 1.自己定义标签 通过自己定义标签,写一个反复指定字符串 2.实现源代码 <html> <head> <meta http-equiv= ...
- freemarker自己定义标签报错(二)
freemarker自己定义标签 1.错误描写叙述 freemarker.core.ParseException: Unexpected end of file reached. at freemar ...
- freemarker自己定义标签报错(七)
1.错误描写叙述 六月 09, 2014 11:11:09 下午 freemarker.log.JDK14LoggerFactory$JDK14Logger error 严重: Template pr ...
- freemarker自己定义标签(一)
freemarker自己定义标签 1.自己定义标签说明 宏变量存储模板片段能够被用作自己定义指令macro 2.演示样例说明 <html> <head> <meta ht ...
- SpringMVC和Freemarker整合,带自定义标签的使用方法
SpringMVC和Freemarker整合,带自定义标签的使用方法. [参考来源:http://www.360doc.com/content/14/1225/14/1007797_435663342 ...
- Spring MVC 环境搭建(maven+SpringMVC+mybatis+Freemarker)
Spring MVC 环境搭建(maven+SpringMVC+mybatis+Freemarker) 一.准备工作 1.Eclipse Java EE IDE(4.4.1) 2.JDK 3.Tomc ...
- spring boot+freemarker+spring security标签权限判断
spring boot+freemarker+spring security标签权限判断 SpringBoot+SpringSecurity+Freemarker项目中在页面上使用security标签 ...
- SpringMVC 集成 Freemarker 模板引擎
本文通过 maven 项目中集成 1.引入 SpringMVC 与 Freemarker 需要的依赖 <!-- SpringMVC --> <dependency> <g ...
随机推荐
- Android dump .so 文件crash log
众所周知,在android系统上,有时候我们遇到so文件的crash仅仅能打log,可是非常多时候并不知道crash在什么地方,幸运的是crash后,一般能够产生一个.dmp文件. 我们能够依据这个文 ...
- 【转】 LESS CSS 框架简介
简介 CSS(层叠样式表)是一门历史悠久的标记性语言,同 HTML 一道,被广泛应用于万维网(World Wide Web)中.HTML 主要负责文档结构的定义,CSS 负责文档表现形式或样式的定义. ...
- Linux 常用命令学习
sed 大法: cat file | sed 's/string/replace_str/' file 按大小分割文件 split -b 100m filename 设置vi不自动转换tab: set ...
- eclipse中输入的中文为繁体的问题
今天在eclipse中编写注释的时候发现,输入的中文都为繁体,且只在eclipse编辑器中为繁体,切换到网页中则为正常. 最后发现,竟然是输入法的shift+ctrl+F快捷键和eclipse的冲突. ...
- Ubuntu安装Mysql及使用
(1)在线安装:sudo apt-get install mysql-server(2)管理mysql: sudo /etc/init.d/mysql start===========>开启my ...
- 一个简单的AMD模块加载器
一个简单的AMD模块加载器 参考 https://github.com/JsAaron/NodeJs-Demo/tree/master/require PS Aaron大大的比我的完整 PS 这不是一 ...
- MySQL----cluster安装
第一步.下载MySQL cluster: http://cdn.mysql.com/Downloads/MySQL-Cluster-7.4/mysql-cluster-gpl-7.4.7-linux- ...
- 提醒录入BOM更改原因
应用 Oracle Bill Of Materiel 层 Level Function 函数名 Funcgtion Name BOM_BOMFDBOM 表单名 Form Name BOMFDBOM ...
- 九度OJ 题目1534:数组中第K小的数字(二分解)
题目链接:点击打开链接 题目描述: 给定两个整型数组A和B.我们将A和B中的元素两两相加可以得到数组C. 譬如A为[1,2],B为[3,4].那么由A和B中的元素两两相加得到的数组C为[4,5,5,6 ...
- Largest Submatrix(动态规划)
Largest Submatrix Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...