首页

通过springframework的mvc返回freemarker的ftl文件ModelAndView模板内容代码示例

标签:springframework,freemarker,ftl,模板,ModelAndView     发布时间:2017-11-05   

一、前言

这边基于springframeworkmvc框架,并通过freemarker的ftl视图渲染文件返回数据,在spring文件中配置对应org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver视图解析类(freemarker-2.3.21.jar.rar的jar、pom文件下载),完整Maven项目源码点击跳转spring4Mvc-freemarker的helloworld项目相关篇章

二、代码实例

1.web.xml配置如下

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"@b@	xmlns="http://java.sun.com/xml/ns/javaee"@b@	xsi:schemaLocation="http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"@b@	id="WebApp_ID" version="3.0">@b@	<display-name>HelloWorldSpring</display-name>@b@@b@	<servlet>@b@		<servlet-name>spring-mvc</servlet-name>@b@		<servlet-class>@b@			org.springframework.web.servlet.DispatcherServlet@b@		</servlet-class>@b@		<load-on-startup>1</load-on-startup>@b@	</servlet>@b@@b@	<servlet-mapping>@b@		<servlet-name>spring-mvc</servlet-name>@b@		<url-pattern>/</url-pattern>@b@	</servlet-mapping>@b@@b@	<!-- Other XML Configuration -->@b@	<!-- Load by Spring ContextLoaderListener -->@b@	<context-param>@b@		<param-name>contextConfigLocation</param-name>@b@		<param-value>/WEB-INF/root-context.xml</param-value>@b@	</context-param>@b@@b@	<!-- Spring ContextLoaderListener -->@b@	<listener>@b@		<listener-class>org.springframework.web.context.ContextLoaderListener@b@		</listener-class>@b@	</listener>@b@</web-app>

2.spring-mvc-servlet.xml配置如下

<?xml version="1.0" encoding="UTF-8"?>@b@<beans xmlns="http://www.springframework.org/schema/beans"@b@    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"@b@    xmlns:p="http://www.springframework.org/schema/p"@b@    xmlns:context="http://www.springframework.org/schema/context"@b@    xmlns:mvc="http://www.springframework.org/schema/mvc"@b@    xsi:schemaLocation="http://www.springframework.org/schema/beans@b@      http://www.springframework.org/schema/beans/spring-beans-4.1.xsd @b@      http://www.springframework.org/schema/context@b@      http://www.springframework.org/schema/context/spring-context-4.1.xsd @b@      http://www.springframework.org/schema/mvc@b@      http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">@b@ @b@   <context:component-scan base-package="com.xwood.springmvc"/>@b@    @b@   <context:annotation-config/> @b@   @b@   <bean id="FreeMarkerConfigurer" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">@b@          <property name="templateLoaderPath" value="/WEB-INF/pages/"/>@b@          <property name="defaultEncoding" value="UTF-8"/>@b@      </bean>@b@      <bean id="FreeMarkerViewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">@b@          <property name="suffix" value=".ftl"/>@b@          <property name="contentType" value="text/html;charset=UTF-8"/>  @b@      </bean>@b@   @b@    @b@</beans>

3.FreeMarkerController的代码

package com.xwood.springmvc;@b@import org.springframework.stereotype.Controller;@b@import org.springframework.web.bind.annotation.RequestMapping;@b@import org.springframework.web.servlet.ModelAndView;@b@  @b@  @b@@Controller  @b@@RequestMapping("/freemark")  @b@public class FreeMarkerController {  @b@  @b@    @RequestMapping("/index")  @b@    public ModelAndView  index(){@b@        return  new ModelAndView("hello3","content","欢迎访问小木人印象,网址:<a href=\"http://www.xwood.net/\">http://www.xwood.net/</a>");  @b@    }@b@}

4.hello3.ftl模板内容如下

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" @b@ "http://www.w3.org/TR/html4/loose.dtd">@b@<html> @b@    <body>@b@        ${content}@b@    </body>@b@</html>

5.访问http://localhost:8080/HelloSpringMVC/freemark/index效果如下所示

通过springframework的mvc返回freemarker的ftl文件ModelAndView模板内容代码示例

三、pom.xml文件

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"@b@	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">@b@	<modelVersion>4.0.0</modelVersion>@b@	<groupId>com.yiibai</groupId>@b@	<artifactId>HelloSpringMVC</artifactId>@b@	<packaging>war</packaging>@b@	<version>0.0.1-SNAPSHOT</version>@b@	<name>HelloSpringMVC Maven Webapp</name>@b@	<url>http://maven.apache.org</url>@b@	<dependencies>@b@@b@		<dependency>@b@			<groupId>junit</groupId>@b@			<artifactId>junit</artifactId>@b@			<version>3.8.1</version>@b@			<scope>test</scope>@b@		</dependency>@b@@b@		<!-- Servlet Library -->@b@		<!-- http://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->@b@		<dependency>@b@			<groupId>javax.servlet</groupId>@b@			<artifactId>javax.servlet-api</artifactId>@b@			<version>3.1.0</version>@b@			<scope>provided</scope>@b@		</dependency>@b@@b@		<!-- Spring dependencies -->@b@		<!-- http://mvnrepository.com/artifact/org.springframework/spring-core -->@b@		<dependency>@b@			<groupId>org.springframework</groupId>@b@			<artifactId>spring-core</artifactId>@b@			<version>4.1.4.RELEASE</version>@b@		</dependency>@b@@b@		<!-- http://mvnrepository.com/artifact/org.springframework/spring-web -->@b@		<dependency>@b@			<groupId>org.springframework</groupId>@b@			<artifactId>spring-web</artifactId>@b@			<version>4.1.4.RELEASE</version>@b@		</dependency>@b@@b@		<!-- http://mvnrepository.com/artifact/org.springframework/spring-webmvc -->@b@		<dependency>@b@			<groupId>org.springframework</groupId>@b@			<artifactId>spring-webmvc</artifactId>@b@			<version>4.1.4.RELEASE</version>@b@		</dependency>@b@		@b@		<dependency>@b@			<groupId>org.springframework</groupId>@b@			<artifactId>spring</artifactId>@b@			<version>2.5.6.SEC03</version>@b@		</dependency>@b@		@b@		<dependency>@b@			<groupId>freemarker</groupId>@b@			  <artifactId>freemarker</artifactId>@b@			  <version>2.3.21</version>@b@		</dependency>@b@@b@	</dependencies>@b@@b@	<build>@b@		<finalName>HelloSpringMVC</finalName>@b@		<plugins>@b@@b@			<!-- Config: Maven Tomcat Plugin -->@b@			<!-- http://mvnrepository.com/artifact/org.apache.tomcat.maven/tomcat7-maven-plugin -->@b@			<plugin>@b@				<groupId>org.apache.tomcat.maven</groupId>@b@				<artifactId>tomcat7-maven-plugin</artifactId>@b@				<version>2.2</version>@b@				<!-- Config: contextPath and Port (Default - /HelloSpringMVC : 8080) -->@b@				<!-- <configuration> <path>/</path> <port>8899</port> </configuration> -->@b@			</plugin>@b@		</plugins>@b@	</build>@b@</project>
<<热门下载>>