麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 學院 > 開發設計 > 正文

[Spring MVC]學習筆記--FreeMarker的使用

2019-11-14 20:52:53
字體:
來源:轉載
供稿:網友
[SPRing MVC]學習筆記--FreeMarker的使用

還是先貼出該例子存于github上的位置

https://github.com/lemonbar/spring-mvc-freemarker

Sping-Framework 的官方文檔簡單列出了在spring-mvc中如何使用freemarker, 但是相對來說提供的信息和例子太少, 所以在這給出一個詳細的例子.

注:我是在maven基礎上進行的構建, 很多解釋已經在代碼中加了, 所以盡量貼代碼.

FreeMarker Site:http://freemarker.org/

1. 整個文件夾結構

src    main        java            com.lemon.spring.controller                GreetingController        webapp            WEB-INF                ftl                    footer.ftl                    header.ftl                    login.ftl                    welcome.ftl                root-context.xml                web.xmlpom.xml

2. 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/maven-v4_0_0.xsd">    <modelVersion>4.0.0</modelVersion>    <groupId>com.lemon.spring</groupId>    <artifactId>spring-mvc-freemarker</artifactId>    <packaging>war</packaging>    <version>1.0-SNAPSHOT</version>    <name>spring-mvc-freemarker Maven Webapp</name>    <url>http://maven.apache.org</url>    <properties>        <spring.framework.version>4.0.6.RELEASE</spring.framework.version>    </properties>    <dependencies>        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-context</artifactId>            <version>${spring.framework.version}</version>        </dependency>        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-webmvc</artifactId>            <version>${spring.framework.version}</version>        </dependency>        <!--context-support should be included for freemarker bean definition.-->        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-context-support</artifactId>            <version>${spring.framework.version}</version>        </dependency>        <dependency>            <groupId>org.freemarker</groupId>            <artifactId>freemarker</artifactId>            <version>2.3.20</version>        </dependency>    </dependencies>    <build>        <finalName>spring-mvc-freemarker</finalName>    </build></project>

3. web.xml

<web-app 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_3_0.xsd"         version="3.0">    <context-param>        <param-name>contextConfigLocation</param-name>        <param-value>WEB-INF/root-context.xml</param-value>    </context-param>    <servlet>        <servlet-name>dispatcher</servlet-name>        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>        <init-param>            <param-name>contextConfigLocation</param-name>            <param-value></param-value>        </init-param>        <load-on-startup>1</load-on-startup>    </servlet>    <servlet-mapping>        <servlet-name>dispatcher</servlet-name>        <url-pattern>/</url-pattern>    </servlet-mapping>    <listener>        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>    </listener></web-app>

4. root-context.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"       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.xsd        http://www.springframework.org/schema/mvc        http://www.springframework.org/schema/mvc/spring-mvc.xsd">    <context:component-scan base-package="com.lemon.spring"/>    <!-- 添加注解驅動 -->    <mvc:annotation-driven enable-matrix-variables="true"/>    <!--<context:annotation-config/>-->    <!-- 允許對靜態資源文件的訪問 -->    <mvc:default-servlet-handler />    <!--freemarker config-->    <bean id="freemarkerConfig"          class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">        <property name="templateLoaderPath" value="/WEB-INF/ftl/"/>    </bean>    <!--    View resolvers can also be configured with ResourceBundles or XML files.    If you need different view resolving based on Locale, you have to use the resource bundle resolver.    -->    <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"/>    </bean></beans>

5. GreetingController.java內容

/*  * Copyright (c) 2014 General Electric Company. All rights reserved.  *  * The copyright to the computer software herein is the property of  * General Electric Company. The software may be used and/or copied only  * with the written permission of General Electric Company or in accordance  * with the terms and conditions stipulated in the agreement/contract  * under which the software has been supplied.  */package com.lemon.spring.controller;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.servlet.ModelAndView;import java.util.Arrays;import java.util.List;@Controllerpublic class GreetingController {    @RequestMapping(value = "/greeting/{user}", method = RequestMethod.GET)    public String greeting(@PathVariable String user, Model model) {        List<String> userList = Arrays.asList(user.split("-"));        //userList is the variable name, used in ftl file.        model.addAttribute("userList", userList);        return "welcome";    }    @RequestMapping(value = "/greeting", method = RequestMethod.POST)    public ModelAndView greeting(@RequestParam("user") String user) {        List<String> userList = Arrays.asList(user.split("-"));        ModelAndView result = new ModelAndView("welcome");        //userList is the variable name, used in ftl file.        result.addObject("userList", userList);        return result;    }    @RequestMapping("/login")    public String login() {        return "login";    }}

6. welcome.ftl

<html><head>    <title>Welcome!</title></head><body><#include "./header.ftl"/><table border="1">    <tr><th>Name</th><th>Price</th></tr>    <#list userList as user>        <tr><th>${user}</th><th>1.0</th></tr>    </#list></table><!--use include to include another ftl file content in this file.--><#include "./footer.ftl"/></body></html>

7. login.ftl

<#import "/spring.ftl" as spring/><html><head>    <title>Please input your names, seperator with '-' char.</title></head><body><#include "./header.ftl"/><form action="greeting" method="POST">    Names:    <input type="text" name="user"/><br>    <input type="submit" value="submit"/></form><!--use include to include another ftl file content in this file.--><#include "./footer.ftl"/></body></html>

8. footer.ftl

<hr><i>    Copyright (c) 2014 <a >Acmee    Inc</a>,    <br>    All Rights Reserved.</i>

9. header.ftl

<h1>    This is header!</h1><hr>


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 欧美日韩在线看片 | 亚洲精品午夜国产va久久成人 | 亚洲aⅴ免费在线观看 | 全黄毛片 | 国产精品美女久久久免费 | 久久精品站 | 欧美视频一二区 | 成人毛片免费看 | 一级做人爱c黑人影片 | 久久综合艹 | 性 毛片| 国产女做a爱免费视频 | 精品国产乱码一区二区三区四区 | 国产噜噜噜噜噜久久久久久久久 | 中文字幕在线免费观看电影 | 激情视频在线播放 | 国产精品午夜在线观看 | 国产精品爱久久久久久久 | 精品在线免费播放 | 色网站免费观看 | 羞羞视频免费网站含羞草 | 91短视频在线播放 | 中文字幕在线观看亚洲 | 亚洲一级电影在线观看 | 午夜视频在线免费播放 | 国产电影精品久久 | 日韩av电影免费看 | 国产欧美精品综合一区 | 久久网日本 | chinese hd xxxx tube | 性欧美极品xxxx欧美一区二区 | 免费成人 | 成人免费网视频 | 久久99精品久久久久久秒播蜜臀 | 天堂在线资源av | 特级西西444www大精品视频免费看 | 姑娘第四集免费看视频 | 欧美人与禽性xxxxx杂性 | 午夜a狂野欧美一区二区 | 久久蜜桃精品一区二区三区综合网 | 在线播放视频一区二区 |