Freemarker与Springmvc

1.导入springmvc和freemarker的jar包

2.web.xml中配置Spring和Springmvc

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

<!-- Spring -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:SpringIOC.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

<!-- Spring MVC -->
    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:SpringmvcIOC.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>


</web-app>

3.建立ftl模板(WEB-INF/view/hello.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=UTF-8">
        <title>${title}</title>
    </head>
    <body>
        ${content}
        <br><hr>
        <#list citys as c>
            索引:${c_index}; 内容:${c}<br>
        </#list>
        
        <br><hr>
        <#if age gt 21>
            age is ${age}
        <#else>
            age${age} 小于21
        </#if>
        
        <#if txt??>${txt[5..20]}</#if>
        <br><hr>
        ${date?string("yyyy-mm-dd")!"null"}
        
        <#macro m1>   <#--定义指令m1 -->
            <b>aaabbbccc</b>
            <b>dddeeefff</b>
        </#macro>
        <@m1 /><@m1 />  <#--调用上面的宏指令 -->
        
        <#macro m2 a b c >
        ${a}--${b}--${c}
        </#macro>
        <@m2 a="老高" b="老张" c="老马" />
        <#--
        测试include指令:
        <#include "included.txt" />
        -->
    </body>
</html>

4.在SpringmvcIOC.xml中配置freemarker(也可以在SpringIOC.xml中配置,SpringIOC.xml文件中无任何配置)

《SpringIOC容器的范围已经大于SpringmvcIOC容器的作用范围;Springmvc的bean可以使用Spring中的,反之不可》

<?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:jee="http://www.springframework.org/schema/jee"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    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-3.2.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
    <!-- 
    <context:component-scan base-package="com.wzy.controller"></context:component-scan>
     -->
    
    <bean id="m" class="com.wzy.controller.Main">
    </bean>


     <!-- Freemarker配置 -->
    <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>
    <!--视图解释器 -->
    <bean id="viewResolver"
          class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
        <property name="suffix">
            <value>.ftl</value>
        </property>
        <property name="contentType" value="text/html;charset=UTF-8"></property>
    </bean>
    
    <!-- 地址转发器 
    <bean name="HelloAction" class="HelloWordController.HelloWordController" />
    <bean id="urlMapping"
          class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
    <property name="mappings">
        <props>
                 映射URL地址 
            <prop key="/hello.do">HelloAction</prop>
        </props>
    </property>
    
    </bean>
    -->
</beans>

5.控制器

package com.wzy.controller;

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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class Main {
    
    @RequestMapping("hello")
    public String hello(Map mv) {
          mv.put("title", "s_f");
          mv.put("content", "當前時間為: "+new Date());
          List l = new  ArrayList();
          l.add("北京");
          l.add("上海");
          l.add("广州");
          l.add("南京");
          mv.put("citys", l);
          mv.put("age", 20);
          mv.put("txt", "14:17:37.724 "
                  + "[http-nio-8080-exec-8] DEBUG o.s.web.servlet.DispatcherServlet -"
                  + " Successfully completed request");
          mv.put("date", new Date());
        return "hello";//返回view/hello.ftl
    }
}

原文地址:https://www.cnblogs.com/wwzyy/p/5498054.html