首页 > 代码库 > ftl简单使用

ftl简单使用

使用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_detecttemplate_update_delay=2default_encoding=UTF-8output_encoding=UTF-8locale=zh_CNdate_format=yyyy-MM-ddtime_format=HH:mm:ssdatetime_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=http://www.mamicode.com/"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=http://www.mamicode.com/"/WEB-INF/jsp/"></property>        <property name="suffix" value=http://www.mamicode.com/".jsp"></property>        <property name="order" value=http://www.mamicode.com/"2"></property>    </bean>    <!-- 配置freeMarker视图解析器 -->    <bean id="viewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">        <property name="viewClass" value=http://www.mamicode.com/"org.springframework.web.servlet.view.freemarker.FreeMarkerView"/>        <property name="contentType" value=http://www.mamicode.com/"text/html; charset=utf-8"/>        <property name="cache" value=http://www.mamicode.com/"true" />        <property name="prefix" value=http://www.mamicode.com/"" />        <property name="suffix" value=http://www.mamicode.com/".ftl" />        <property name="order" value=http://www.mamicode.com/"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 : */@Controllerpublic 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简单使用