Freemarker具有丰富前端界面表示能力,在SSM项目中使用Freemarker作为前台页面展现层。
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.test</groupId>
<artifactId>freemarkerweb</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>freemarkerweb Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.23</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.7.RELEASE</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>4.3.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>4.3.7.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.29</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.0</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>4.3.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.10</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.2</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.3.10.RELEASE</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.3</version>
</dependency>
</dependencies>
<build>
<finalName>freemarkerweb</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<port>8080</port>
<path>/</path>
</configuration>
</plugin>
</plugins>
</build>
</project>
SpringMVC.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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
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.test.freemarker"></context:component-scan>
<!-- 视图解析器 -->
<!-- jsp视图解析器 -->
<!-- 配置InternalResourceViewResolver -->
<!--
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
id="internalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
-->
<!-- 页面: templateLoaderPath + prefix + 视图名 + suffix -->
<!-- 模板位置 -->
<bean
class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
<property name="templateLoaderPath" value="/WEB-INF"></property>
<property name="defaultEncoding" value="UTF-8"></property>
<property name="freemarkerSettings">
<props>
<prop key="number_format">#.##</prop>
</props>
</property>
</bean>
<bean
class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
<property name="prefix" value="/ftl/"></property>
<property name="suffix" value=".ftl"></property>
<property name="contentType" value="text/html;charSet=UTF-8"></property>
<property name="requestContextAttribute" value="request"></property>
</bean>
<!-- 文件上传处理器,id是固定的 -->
<!-- Ctrl + Shift + T -->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="utf-8"></property>
</bean>
<!-- 注解驱动,静态资源 -->
<mvc:annotation-driven />
<mvc:default-servlet-handler />
</beans>
jdbc.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8
jdbc.uname=root
jdbc.pwd=root
``
SqlMapConfig.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<settings>
<!-- 打印查询语句-->
<setting name="logImpl" value="STDOUT_LOGGING" />
</settings>
<mappers>
<!--
<mapper resource="com/test/mapper/StudentMapper.xml"/>
<package name="com.test.hr"/>
<package name="com.test.o2m"/>
<mapper resource="user.xml"/>
<mapper resource="com/test/hr/UserDao.xml"/>
<mapper class="com.test.hr.UserDao"/>
-->
</mappers>
</configuration>
Spring.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:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:rabbit="http://www.springframework.org/schema/rabbit"
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/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/rabbit
http://www.springframework.org/schema/rabbit/spring-rabbit.xsd">
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- Spring为什么要整合Mybatis ? -->
<!-- 1.Spring帮助Mybatis管理事务 -->
<!-- 2.SqlSession是由SqlSessionFactory来创建的,SqlSessionFactory是一个重量级
管理SqlSessionFactory
3.整合映射文件xml,自动映射接口,生成代理
-->
<!-- 数据源 -->
<bean id="ds" class="com.alibaba.druid.pool.DruidDataSource">
<property name="url" value="${jdbc.url}"></property>
<property name="driverClassName" value="${jdbc.driver}"></property>
<property name="username" value="${jdbc.uname}"></property>
<property name="password" value="${jdbc.pwd}"></property>
</bean>
<!-- Session工厂 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="configLocation" value="classpath:SqlMapConfig.xml"/>
<property name="dataSource" ref="ds"></property>
<!-- 给实体类取别名 -->
<property name="typeAliasesPackage" value="com.test.bean"></property>
</bean>
<!-- 接口代理 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
<property name="basePackage" value="com.test.mapper"></property>
</bean>
<!-- 事务管理器 -->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" >
<property name="dataSource" ref="ds"></property>
</bean>
<!-- 哪些方法需要事务处理 -->
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="insert*"/>
<tx:method name="add*"/>
<tx:method name="save*"/>
<tx:method name="update*"/>
<tx:method name="upd*"/>
<tx:method name="modify*"/>
<tx:method name="delete*"/>
<tx:method name="del*"/>
<tx:method name="get*" read-only="true"/>
<tx:method name="find*" read-only="true"/>
<tx:method name="select*" read-only="true"/>
</tx:attributes>
</tx:advice>
<!-- 事务切面 -->
<aop:config>
<aop:pointcut expression="execution(* com.test.service.impl.*.*(..))" id="pc"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="pc"/>
</aop:config>
<!-- 扫描service -->
<context:component-scan base-package="com.test.service.impl"></context:component-scan>
<import resource="classpath*:/springmvc.xml"/>
</beans>
UserInfo.java
package com.test.bean;
import java.sql.Date;
public class UserInfo {
private Integer id = null;
private String name = null;
private Date dt = new Date(System.currentTimeMillis());
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getDt() {
return dt;
}
public void setDt(Date dt) {
this.dt = dt;
}
}
FreemarkerCtrl.java
package com.test.freemarker;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.test.bean.UserInfo;
@Controller
@RequestMapping("/free")
public class FreemarkerCtrl {
@RequestMapping("test")
public String test(HttpServletRequest req)
{
UserInfo ui = new UserInfo();
ui.setId(100);
ui.setName("Oracle");
Map m = new HashMap();
m.put("name","java");
m.put("user", ui);
req.setAttribute("data",m);
return "test";
}
}
test.ftl Freemarker模板
<html>
<head>First freemarker demo</head>
<body>
<#function sum i j>
<#return i+j>
</#function>
${data.name}
<br>
${sum(19,20)}
<br>
${data.user.dt}
<br>
<#function sumTotal para...>
<#local total=0>
<#list para as p>
<#local total=total+p>
</#list>
<#return total>
</#function>
${sumTotal(1,2,19,20)}
</body>
</html>