springmvc 整合freemarker

1.引入依赖jar包

        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-contextartifactId>
            <version>4.2.4.RELEASEversion>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-beansartifactId>
            <version>4.2.4.RELEASEversion>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-webmvcartifactId>
            <version>4.2.4.RELEASEversion>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-jdbcartifactId>
            <version>4.2.4.RELEASEversion>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-aspectsartifactId>
            <version>4.2.4.RELEASEversion>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-jmsartifactId>
            <version>4.2.4.RELEASEversion>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-context-supportartifactId>
            <version>4.2.4.RELEASEversion>
        dependency>
         <dependency>
            <groupId>org.freemarkergroupId>
            <artifactId>freemarkerartifactId>
             <version>2.3.23version>
        dependency>

2. 创建整合spring的配置文件


<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"
    xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
    xmlns:aop="http://www.springframework.org/schema/aop"
     xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
        http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd 
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd">

     
    <context:component-scan base-package="com.ty.web.controller" />
    
    <mvc:annotation-driven >mvc:annotation-driven>
    
  <bean id="freeMarkerConfigurer"
        class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
        <property name="templateLoaderPath" value="/WEB-INF/ftl/">property>
        <property name="defaultEncoding" value="utf-8" />
               
         <property name="freemarkerSettings">
            <props>
                <prop key="tag_syntax">auto_detectprop>
                <prop key="template_update_delay">5prop>
                <prop key="defaultEncoding">UTF-8prop>
                <prop key="url_escaping_charset">UTF-8prop>
                <prop key="locale">zh_CNprop>
                <prop key="boolean_format">true,falseprop>
                <prop key="datetime_format">yyyy-MM-dd HH:mm:ssprop>
                <prop key="date_format">yyyy-MM-ddprop>
                <prop key="time_format">HH:mm:ssprop>
                <prop key="number_format">0.######prop>
                <prop key="whitespace_stripping">trueprop>
            props>
        property>
        
        <property name="freemarkerVariables">
            <map>
            
                <entry key="getCurrentUser">
                    <bean class="com.ty.web.freemark.fn.CurrentUserGetter" />
                entry>
            map>
        property>
    bean>
    
    <bean id="viewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
    <property name="viewClass"
            value="org.springframework.web.servlet.view.freemarker.FreeMarkerView" />
        <property name="prefix" value=""/>
        <property name="contentType" value="text/html; charset=utf-8" />
        <property name="requestContextAttribute" value="request" />
        <property name="suffix" value=".ftl" />
    bean>

     
    <mvc:resources location="/js/" mapping="/js/**"/>
    <mvc:resources location="/images/" mapping="/images/**"/>
    <mvc:resources location="/styles/" mapping="/styles/**"/>
    <mvc:default-servlet-handler/>

beans>      

下面介绍一下前面的自定义函数:

//获取当前登录的用户信息
public class CurrentUserGetter implements TemplateMethodModelEx {
    @Override
    public Object exec(List arguments) throws TemplateModelException {
        System.err.println("arguments==" + arguments);
        return "ty:" + arguments;
    }
}

在test.ftl的引用方式为:

    getCurrentUser:
<#assign getCurrentUser= getCurrentUser("1")/> ${getCurrentUser}

3. Controller代码

@Controller
@RequestMapping("/freemark")
public class FreeMarkerAction {
    @RequestMapping(value = "/testFreemarker")
    @ResponseBody
    public ModelAndView testFreemarker() throws Exception {
        ModelAndView mv = new ModelAndView();

        List list = new ArrayList();
        list.add("Jack1");
        list.add("Jack2");
        list.add("Jack3");

        mv.addObject("list", list);
        mv.addObject("param", new String("wyy"));
        mv.setViewName("may");
        return mv;
    }

}

4. 前台代码测试

在WEB-INF 下创建文件夹ftl,在ftl下创建test.ftl:

List:<br/>
<#list list as item >
<font color="red">${item}br>font>
#list>

getCurrentUser:<br>
    <#assign getCurrentUser= getCurrentUser("1")/>  
    ${getCurrentUser}  

springmvc 整合freemarker_第1张图片

你可能感兴趣的:(SpringMVC,FreeMarker)