springmvc与freemarker的整合

官方简介:FreeMarker 是一款 模板引擎: 即一种基于模板和要改变的数据, 并用来生成输出文本(HTML网页,电子邮件,配置文件,源代码等)的通用工具。 它不是面向最终用户的,而是一个Java类库,是一款程序员可以嵌入他们所开发产品的组件。

springmvc与freemarker的整合_第1张图片
  • 最终项目的目录如下:
    springmvc与freemarker的整合_第2张图片

01.首先利用maven构建一个以war方式打包的web工程

springmvc与freemarker的整合_第3张图片
  • finish后,看到如下报错
    springmvc与freemarker的整合_第4张图片
  • 然后进行解决
    springmvc与freemarker的整合_第5张图片

02.添加依赖

  • springmvc的依赖

<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webmvcartifactId>
<version>4.3.7.RELEASEversion>
dependency>

  • freemarker的依赖

<dependency>
<groupId>org.freemarkergroupId>
<artifactId>freemarkerartifactId>
<version>2.3.23version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-context-supportartifactId>
<version>4.3.7.RELEASEversion>
dependency>

03.web.xml的配置


<servlet>
<servlet-name>springmvcservlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>

<init-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath*:/applicationContext-web.xmlparam-value>
init-param>
<load-on-startup>1load-on-startup>
servlet>

<servlet-mapping>
<servlet-name>springmvcservlet-name>

<url-pattern>/url-pattern>
servlet-mapping>

<filter>
<filter-name>encodingFilterfilter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
<init-param>
<param-name>encodingparam-name>
<param-value>UTF-8param-value>
init-param>
<init-param>
<param-name>forceEncodingparam-name>
<param-value>trueparam-value>
init-param>
filter>
<filter-mapping>
<filter-name>encodingFilterfilter-name>
<url-pattern>/*url-pattern>
filter-mapping>

04.创建springmvc的配置文件

  • applicationContext-web.xml

<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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"
>


<context:component-scan base-package="freemarker" />

<bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
<property name="templateLoaderPath" value="/WEB-INF/templates"/>
bean>

<bean id="viewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
<property name="prefix" value=""/>
<property name="suffix" value=".ftl"/>
<property name="contentType" value="text/html; charset=UTF-8"/>
bean>

<mvc:annotation-driven />

<mvc:view-controller path="/index" view-name="index"/>
beans>

05.在WEB-INF目录下创建templates目录,设置直接跳转的页面index.ftl


<html>
<head>
<meta charset="UTF-8">

<title>Insert title heretitle>
head>
<body>
<a href="helloFtl">helloa>
body>
html>

06.在controller层创建一个类

package freemarker.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class Hello {
@RequestMapping(value = "helloFtl")
public String helloFtl(Model model) {
model.addAttribute("hello", "hello world!");
return "helloFtl";
}
}

07.在templates目录下创建一个helloFtl.ftl文件


<html>
<head>
<meta charset="UTF-8">
<title>Insert title heretitle>
head>
<body>
${hello}
body>
html>
  • 启动tomcat后,输入以下网址查看效果:
    springmvc与freemarker的整合_第6张图片
  • 点击hello的链接就可以从controller层获取数据和跳转页面
    springmvc与freemarker的整合_第7张图片
    springmvc与freemarker的整合_第8张图片 java后端生活

你可能感兴趣的:(java,freemarker,spring,springmvc)