您的位置:首页 > 编程语言 > Java开发

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

2018-03-29 10:17 489 查看
当今最流行的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;
4000

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>
最后看一下目录结构:
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息