[Spring MVC] learning notes -- Use of FreeMarker, mvc -- freemarker
Or paste the example on github.
Https://github.com/lemonbar/spring-mvc-freemarker
The official document of Sping-Framework briefly lists how to use freemarker in spring-mvc, but provides too little information and examples, so a detailed example is provided here.
Note: I built it on the basis of maven. Many explanations have been added to the Code, so try to paste the code as much as possible.
FreeMarker Site: http://freemarker.org/
1. The entire folder structure
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 content
<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 content
<? 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 "/> <! -- Add annotation driver --> <mvc: annotation-driven enable-matrix-variables = "true"/> <! -- <Context: annotation-config/> --> <! -- Allow access to static resource files --> <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 content
/* * 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
7. login. ftl
<#import "/spring.ftl" as spring/>
8. footer. ftl
9. header. ftl
How does spring mvc controller jump to html that has been generated by freemarker?
Directly write the path you need to jump to in the returnNewAndView in the controller (generally: Folder/html page)
How does Spring mvc differentiate whether the returned view is freemarker or jsp?
This configuration is ordered. As shown in your figure, the freemarker view is returned first. If the view cannot be found, the jsp view is returned.