码迷,mamicode.com
首页 > 其他好文 > 详细

ftl简单使用

时间:2017-06-29 15:25:49      阅读:483      评论:0      收藏:0      [点我收藏+]

标签:.config   async   图解   过滤   -name   resource   tag   java   filter   

使用springmvc整合!使用maven!不会maven的手动导入jar包!

目录:

技术分享

依赖:(省略springmvc的一些包)

<dependency>
      <groupId>org.freemarker</groupId>
      <artifactId>freemarker</artifactId>
      <version>2.3.22</version>
    </dependency>

web.xml(这个没什么好说的)

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>ftl</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>

  <!-- 编码过滤器 -->
  <filter>
    <filter-name>encodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <async-supported>true</async-supported>
    <init-param>
      <param-name>encoding</param-name>
      <param-value>UTF-8</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>encodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

  <!-- 添加对springmvc的支持 -->
  <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:spring-mvc.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
    <async-supported>true</async-supported>
  </servlet>
  <servlet-mapping>
    <servlet-name>springMVC</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

</web-app>

freemarker.properties

tag_syntax=auto_detect

template_update_delay=2

default_encoding=UTF-8

output_encoding=UTF-8

locale=zh_CN

date_format=yyyy-MM-dd

time_format=HH:mm:ss

datetime_format=yyyy-MM-dd HH:mm:ss

spring-mvc.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"
       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.loger.controller"></context:component-scan>

    <bean id="freemarkerConfiguration" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
        <property name="location" value="classpath:freemarker.properties"/>
    </bean>

    <bean id="fmXmlEscape" class="freemarker.template.utility.XmlEscape"/>
    <!-- 配置freeMarker的模板路径 -->
    <bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
        <property name="templateLoaderPath">
            <value>/WEB-INF/ftl/</value>
        </property>
        <property name="freemarkerVariables">
            <map>
                <entry key="xml_escape" value-ref="fmXmlEscape" />
            </map>
        </property>
        <property name="freemarkerSettings">
            <props>
                <prop key="defaultEncoding">UTF-8</prop>    <!-- 解决中文乱码 -->
            </props>
        </property>
    </bean>

    <!-- jsp视图解析器 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"></property>
        <property name="suffix" value=".jsp"></property>
        <property name="order" value="2"></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" />
        <property name="prefix" value="" />
        <property name="suffix" value=".ftl" />
        <property name="order" value="1"/>
    </bean>


</beans>

hello2.ftl

${name},你好!这是FTL页面<br>
<#list list as a>
    ${a}<br>
</#list>

hello2.jsp

<%--
  Created by IntelliJ IDEA.
  User: qianjy
  Date: 2017/6/29
  Time: 13:43
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    ${name},你好!这是JSP页面
</body>
</html>

TestController.java

package com.loger.controller;

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

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by zhengrf
 * Date: 2017-06-29
 * TIme: 13:39
 * Description :
 */
@Controller
public class TestController {

    @RequestMapping("/ftl")
    public String testFTL(HttpServletRequest request){
        request.setAttribute("name","陈乐");
        List<String> list = new ArrayList<String>();
        list.add("aaa");
        list.add("bbb");
        list.add("ccc");
        request.setAttribute("list",list);
        return "hello2";
    }

}

暂且贴一下代码,具体是什么我也还没有屡清楚!

ftl简单使用

标签:.config   async   图解   过滤   -name   resource   tag   java   filter   

原文地址:http://www.cnblogs.com/loger1995/p/7094381.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!