freemarker+springMvc简单实例

Spring Tool Suite(sts)http://spring.io/tools/sts/all 

一篇很全面的freemarker教程 http://blog.csdn.net/fhx007/article/details/7902040/

mvc-config.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">
	
    <!-- Uncomment and your base-package here:-->
    <context:component-scan base-package="com.freemarker.controller"/>  

    <mvc:annotation-driven />

	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
	        <property name="prefix" value="/WEB-INF/view/"/>
	        <property name="suffix" value=".jsp"/>
	        <property name="order" value="1"/>
	</bean> 
	
	 <!-- 针对freemarker的视图配置 -->  
    <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" />  
        <property name="contentType" value="text/html;charset=UTF-8"></property>  
        <property name="requestContextAttribute" value="request" />  
        <property name="exposeSpringMacroHelpers" value="true" />  
        <property name="exposeRequestAttributes" value="true" />  
        <property name="exposeSessionAttributes" value="true" />  
        <property name="order" value="0"/>
    </bean>
	
	 <!-- freemaker配置 -->
    <bean id="freemarkerConfig"  
        class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">  
        <property name="templateLoaderPath" value="/WEB-INF/view/" />  
        <property name="freemarkerSettings">  
            <props>  
                <prop key="template_update_delay">0</prop>  
                <prop key="default_encoding">UTF-8</prop>  
                <prop key="number_format">0.##########</prop>  
                <prop key="datetime_format">yyyy-MM-dd HH:mm:ss</prop>  
                <prop key="classic_compatible">true</prop>  
                <prop key="template_exception_handler">ignore</prop>  
            </props>  
        </property>  
    </bean> 
	
</beans>

welcome.ftl

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
<html>  
<head>  
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">  
<title>Insert title here</title>  
</head>  
<body>  
Hello <br/>   
		${name}  <br/>
        ${money?string.currency} <br/>
		${str1??} <br/>
		${str2} <br/>
		${name1} <br/>
		${name2} <br/>
		
		${now?string("yyyy-MM-dd HH:mm:ss zzzz")}	<br/>
		
		<#assign foo=true/> 
		${foo?string(true, false)}  <br/>
		
		<ul>
		<#list list as x> 
			<li>${x} </li> 
		</#list>
		</ul> 
		
		${map.mapName} <br>
		
		<#if num==''> this no	${num}
			<#elseif num==1> this 1
			<#elseif num==2> this 2
			<#else> this other 
		</#if> <br>
		 
</body>  
</html>

FreemarkerController.java

package com.freemarker.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping("/freemarker")
public class FreemarkerController {

	@RequestMapping(value = "/welcome", method = RequestMethod.GET)
	public String getFirstPage(HttpServletRequest request) {
		// welcom就是视图的名称(welcome.ftl)
//		ModelAndView mv = new ModelAndView();
//		mv.setViewName("welcome");
//		mv.addObject("name", "this is freemaker test!!!");
		
		List<String> list=new ArrayList<String>();
		list.add("元素1");
		list.add("元素2");
		list.add("元素3");
		list.add("元素4");
		list.add("元素5");
		
		Map<String,Object> map=new HashMap<String, Object>();
		map.put("mapName", "this is map");
		
		request.setAttribute("name", "這是一個freemarker的页面");
		request.setAttribute("now", new Date());
		request.setAttribute("money", 10.22D);
		request.setAttribute("str1", "");
		request.setAttribute("str2", "字符串");
		request.setAttribute("name1", "這是一個freemarker的页面");
		request.setAttribute("map", map);
		request.setAttribute("list", list);
		request.setAttribute("num", 2);
		
		
		return "welcome";
	}
}

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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>org.springframework.samples.service.service</groupId>
  <artifactId>test</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  
    <properties>

		<!-- Generic properties -->
		<java.version>1.6</java.version>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		
		<!-- Web -->
		<jsp.version>2.2</jsp.version>
		<jstl.version>1.2</jstl.version>
		<servlet.version>2.5</servlet.version>
		

		<!-- Spring -->
		<spring-framework.version>3.2.3.RELEASE</spring-framework.version>

		<!-- Hibernate / JPA -->
		<hibernate.version>4.2.1.Final</hibernate.version>

		<!-- Logging -->
		<logback.version>1.0.13</logback.version>
		<slf4j.version>1.7.5</slf4j.version>

		<!-- Test -->
		<junit.version>4.11</junit.version>

	</properties>
	
	<dependencies>
	
		<!-- Spring MVC -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>${spring-framework.version}</version>
		</dependency>
		
		<!-- Other Web dependencies -->
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>jstl</artifactId>
			<version>${jstl.version}</version>
		</dependency>
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>servlet-api</artifactId>
			<version>${servlet.version}</version>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>javax.servlet.jsp</groupId>
			<artifactId>jsp-api</artifactId>
			<version>${jsp.version}</version>
			<scope>provided</scope>
		</dependency>
	
		<!-- Spring and Transactions -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-tx</artifactId>
			<version>${spring-framework.version}</version>
		</dependency>

		<!-- Logging with SLF4J & LogBack -->
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-api</artifactId>
			<version>${slf4j.version}</version>
			<scope>compile</scope>
		</dependency>
		<dependency>
			<groupId>ch.qos.logback</groupId>
			<artifactId>logback-classic</artifactId>
			<version>${logback.version}</version>
			<scope>runtime</scope>
		</dependency>

		<!-- Hibernate -->
		<dependency>
			<groupId>org.hibernate</groupId>
			<artifactId>hibernate-entitymanager</artifactId>
			<version>${hibernate.version}</version>
		</dependency>

		<!-- Test Artifacts -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-test</artifactId>
			<version>${spring-framework.version}</version>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>${junit.version}</version>
			<scope>test</scope>
		</dependency>
		
		<dependency>
		  <groupId>org.freemarker</groupId>
		  <artifactId>freemarker</artifactId>
		  <version>2.3.19</version>
		</dependency>
		
		<!-- https://mvnrepository.com/artifact/org.springframework/spring-context-support -->
		<dependency>
		    <groupId>org.springframework</groupId>
		    <artifactId>spring-context-support</artifactId>
		    <version>3.2.3.RELEASE</version>
		</dependency>
		<dependency>
		    <groupId>javax.servlet</groupId>
		    <artifactId>servlet-api</artifactId>
		    <version>2.5</version>
		</dependency>

	</dependencies>	
</project>

测试访问:http://localhost:8080/test/freemarker/welcome.do

转载于:https://my.oschina.net/ludd79806329/blog/779922

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值