【JAVA基础】springMVC 整合freemarker(maven)

当今最流行的java模板引擎,莫过于freemarker和velocity。这里我弄个springMVC整合freemark的例子分享给大家。


基本的就不说了,直接入正题。

第一步:pom文件的依赖包

  <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>3.2.4.RELEASE</version>
    </dependency>
    
    <dependency>
	        <groupId>org.freemarker</groupId>
		<artifactId>freemarker</artifactId>
		<version>2.3.20</version>
	</dependency>
	
	<dependency>
	    <groupId>org.springframework</groupId>
	    <artifactId>spring-context-support</artifactId>
	    <version>3.2.4.RELEASE</version>
	</dependency>
第二步:web.xml配置

  <servlet>
	        <servlet-name>demo</servlet-name>
	        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
	        <load-on-startup>1</load-on-startup>
	</servlet>
	
	<servlet-mapping>
	        <servlet-name>demo</servlet-name>
	        <url-pattern>*.html</url-pattern>
	</servlet-mapping>
第三步:在WEN-INF下面 再建一个demo-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.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context.xsd">


    <context:component-scan base-package="com"/>


    <!-- ... -->

</beans>
第四步: 在java/main/resource目录下新增freemarker.properties配置文件

#\u8BBE\u7F6E\u6807\u7B7E\u7C7B\u578B\uFF1Asquare_bracket:[]     auto_detect:[]<>
tag_syntax=auto_detect
#\u6A21\u7248\u7F13\u5B58\u65F6\u95F4\uFF0C\u5355\u4F4D\uFF1A\u79D2
template_update_delay=0
default_encoding=UTF-8
output_encoding=UTF-8
locale=zh_CN
#\u8BBE\u7F6E\u6570\u5B57\u683C\u5F0F \uFF0C\u9632\u6B62\u51FA\u73B0 000.00
number_format=#
#\u53D8\u91CF\u4E3A\u7A7A\u65F6\uFF0C\u4E0D\u4F1A\u62A5\u9519
classic_compatible=true
#auto_import="/WEB-INF/templates/index.ftl" as do
第五步: 在demo-servlet.xml中增加freemarker视图配置

 <!-- 设置freeMarker的配置文件路径 -->
		<bean id="freemarkerConfiguration" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
		    <property name="location" value="classpath:freemarker.properties"/>
		</bean>
		
		<!-- 配置freeMarker的模板路径 -->
		<bean class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
			<property name="freemarkerSettings" ref="freemarkerConfiguration"/>
		    <property name="templateLoaderPath">
		        <value>/WEB-INF/ftl/</value>
		    </property>
		</bean>
		
		<!-- 配置freeMarker视图解析器 -->
		<bean id="viewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
		    <property name="viewClass" value="org.springframework.web.servlet.view.freemarker.FreeMarkerView"/>
		    <property name="contentType" value="text/html; charset=utf-8"/>
		    <property name="cache" value="true"/>
		</bean>
第六部:controller

package com.testFreeMarker.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.testFreeMarker.model.User;

@Controller
public class FreemarkerController {
	
	@RequestMapping("/hi")
	public String sayHello(ModelMap map){
        System.out.println("哈哈哈");
        map.put("name", "测试一下这个freeMarker");

        return "/hi.ftl";
	}
	
	@RequestMapping("/user")
    public String helloUser(ModelMap modelMap) {
		User user1=new User(1, "罗宾", "123456");
		User user2=new User(2, "老施", "123456");
		User user3=new User(3, "老大", "123456");
		User user4=new User(4, "老板", "123456");
		List<User> list=new ArrayList<User>();
		list.add(user1);
		list.add(user2);
		list.add(user3);
		list.add(user4);
        modelMap.addAttribute("userDo", list) ;
        return "/userList.ftl";
    }
}
User

package com.testFreeMarker.model;

public class User {
	private int id;
	private String username;
	private String password;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public User(int id, String username, String password) {
		super();
		this.id = id;
		this.username = username;
		this.password = password;
	}

}
hi.ftl

<html>
<body>
    <h1>say hello ${name}</h1><br/>
    ${(1 != 1)?string("yes", "no")}
</body>
</html>
userList.ftl

<#setting classic_compatible=true>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>User List</title>
<style type="text/css">
<!--
.STYLE1 {
    font-family: Arial, Helvetica, sans-serif;
    font-weight: bold;
    font-size: 36px;
    color: #FF0000;
}
.STYLE13 {font-size: 24}
.STYLE15 {font-family: Arial, Helvetica, sans-serif; font-size: 24px; }
-->
</style>
</head>

<body>
<table width="1500" height="600" border="0" cellpadding="0" cellspacing="0">
  <tr>
    <td width="500" height="200"> </td>
    <td width="500" height="200" align="center" valign="middle"><div align="center"><span class="STYLE1">User List </span></div></td>
    <td width="500" height="200"> </td>
  </tr>
  <tr>
    <td width="500" height="200"> </td>
    <td width="500" height="200"><table width="500" height="200" border="1" cellpadding="0" cellspacing="0">
      <tr>
        <td width="160" height="65" align="center" valign="middle"><span class="STYLE15">ID</span></td>
        <td width="160" height="65" align="center" valign="middle"><span class="STYLE15">Username</span></td>
        <td width="160" height="65" align="center" valign="middle"><span class="STYLE15">Password</span></td>
      </tr>
      <#list userDo as user>
      <tr>
        <td width="160" height="65" align="center" valign="middle"><span class="STYLE15">${user.id}</span></td>
        <td width="160" height="65" align="center" valign="middle"><span class="STYLE15">${user.username}</span></td>
        <td width="160" height="65" align="center" valign="middle"><span class="STYLE15">${user.password}</span></td>
      </tr>
      </#list>
    </table></td>
    <td width="500" height="200"> </td>
  </tr>
  <tr>
    <td width="500" height="200"> </td>
    <td width="500" height="200"> </td>
    <td width="500" height="200"> </td>
  </tr>
</table>
</body>
</html>

最后看一下目录结构:














评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

时间煮水

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值