SpringMVC环境的搭建在这里就不多说了,我们这节主要是FreeMarker与SpringMVC整合

首先,在springmvc的配置文件普通视图之前,加入freemarker的视图

fre-servlet.xml

 <!-- 一定要放在viewResolver的前面,这样就先去找freemarker的 -->

 <bean id="freemarkerConfig"

     class="org.springframework.web.servlet

           .view.freemarker.FreeMarkerConfigurer">

     <property name="templateLoaderPath" value="/WEB-INF/ftl/"/>

 </bean>

 <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"/>

     <property name="contentType" value="text/html; charset=UTF-8"/>

 </bean>

 <bean

     class="org.springframework.web.servlet

           .view.InternalResourceViewResolver">

     <property name="viewClass"

           value="org.springframework.web.servlet.view.JstlView"/>

     <property name="prefix" value="/WEB-INF/jsp/" />

     <property name="suffix" value=".jsp" />

 </bean>
控制器HelloController
 package org. fre.controller;

 import org.springframework.stereotype.Controller;

 import org.springframework.ui.Model;

 import org.springframework.web.bind.annotation.RequestMapping;

 @Controller

 public class HelloController {

     @RequestMapping("/hello")

     public String hello(Model model) {

         model.addAttribute("username", "张三");

         return "hello";

     }

     @RequestMapping("/world")

     public String helloworld(Model model) {

         model.addAttribute("username","李四");

         return "world";

     }

 }
在WEB-INF/jsp目录下有一个world.jsp
 <%@ page language="java" contentType="text/html; charset=UTF-8"

     pageEncoding="UTF-8"%>

 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd">

 <html>

 <head>

     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

     <title>Insert title here</title>

     </head>

     <body>

         ${username }

     </body>

 </html>

在WEB-INF/ftl目录下有

 <html>

 <head>

 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

 <title>Insert title here</title>

 </head>

 <body>

     <h1>${username}</h1>

 </body>

 </html>

启动服务,

访问http://localhost:8080/hello时,显示张三

访问http://localhost:8080/world时,显示李四

到此就整合成功了

 

SpringMVC环境的搭建在这里就不多说了,我们这节主要是FreeMarker与SpringMVC整合

首先,在springmvc的配置文件普通视图之前,加入freemarker的视图

fre-servlet.xml

01 <!-- 一定要放在viewResolver的前面,这样就先去找freemarker的 -->
02  
03 <bean id="freemarkerConfig"
04  
05     class="org.springframework.web.servlet
06  
07           .view.freemarker.FreeMarkerConfigurer">
08  
09     <property name="templateLoaderPath" value="/WEB-INF/ftl/"/>
10  
11 </bean>
12  
13 <bean id="viewResolver"
14  
15     class="org.springframework.web.servlet
16  
17           .view.freemarker.FreeMarkerViewResolver">
18  
19     <property name="cache" value="true"/>
20  
21     <property name="prefix" value=""/>
22  
23     <property name="suffix" value=".ftl"/>
24  
25     <property name="contentType" value="text/html; charset=UTF-8"/>
26  
27 </bean>
28  
29 <bean
30  
31     class="org.springframework.web.servlet
32  
33           .view.InternalResourceViewResolver">
34  
35     <property name="viewClass"
36  
37           value="org.springframework.web.servlet.view.JstlView"/>
38  
39     <property name="prefix" value="/WEB-INF/jsp/" />
40  
41     <property name="suffix" value=".jsp" />
42  
43 </bean>

控制器HelloController

01 package org. fre.controller;
02  
03    
04  
05 import org.springframework.stereotype.Controller;
06  
07 import org.springframework.ui.Model;
08  
09 import org.springframework.web.bind.annotation.RequestMapping;
10  
11    
12  
13 @Controller
14  
15 public class HelloController {
16  
17     @RequestMapping("/hello")
18  
19     public String hello(Model model) {
20  
21         model.addAttribute("username", "张三");
22  
23         return "hello";
24  
25     }
26  
27     @RequestMapping("/world")
28  
29     public String helloworld(Model model) {
30  
31         model.addAttribute("username","李四");
32  
33         return "world";
34  
35     }
36  
37 }

在WEB-INF/jsp目录下有一个world.jsp

01 <%@ page language="java" contentType="text/html; charset=UTF-8"
02  
03     pageEncoding="UTF-8"%>
04  
05 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd">
06  
07 <html>
08  
09 <head>
10  
11     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
12  
13     <title>Insert title here</title>
14  
15     </head>
16  
17     <body>
18  
19         ${username }
20  
21     </body>
22  
23 </html>

在WEB-INF/ftl目录下有

01 <html>
02  
03 <head>
04  
05 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
06  
07 <title>Insert title here</title>
08  
09 </head>
10  
11 <body>
12  
13     <h1>${username}</h1>
14  
15 </body>
16  
17 </html>

启动服务,

访问http://localhost:8080/hello时,显示张三

访问http://localhost:8080/world时,显示李四

到此就整合成功了

Freemark与spring整合的更多相关文章

  1. 使用Spring整合Quartz轻松完成定时任务

    一.背景 上次我们介绍了如何使用Spring Task进行完成定时任务的编写,这次我们使用Spring整合Quartz的方式来再一次实现定时任务的开发,以下奉上开发步骤及注意事项等. 二.开发环境及必 ...

  2. 【Java EE 学习 53】【Spring学习第五天】【Spring整合Hibernate】【Spring整合Hibernate、Struts2】【问题:整合hibernate之后事务不能回滚】

    一.Spring整合Hibernate 1.如果一个DAO 类继承了HibernateDaoSupport,只需要在spring配置文件中注入SessionFactory就可以了:如果一个DAO类没有 ...

  3. spring整合hibernate的详细步骤

    Spring整合hibernate需要整合些什么? 由IOC容器来生成hibernate的sessionFactory. 让hibernate使用spring的声明式事务 整合步骤: 加入hibern ...

  4. Spring整合Ehcache管理缓存

    前言 Ehcache 是一个成熟的缓存框架,你可以直接使用它来管理你的缓存. Spring 提供了对缓存功能的抽象:即允许绑定不同的缓存解决方案(如Ehcache),但本身不直接提供缓存功能的实现.它 ...

  5. spring整合hibernate

    spring整合hibernate包括三部分:hibernate的配置.hibernate核心对象交给spring管理.事务由AOP控制 好处: 由java代码进行配置,摆脱硬编码,连接数据库等信息更 ...

  6. MyBatis学习(四)MyBatis和Spring整合

    MyBatis和Spring整合 思路 1.让spring管理SqlSessionFactory 2.让spring管理mapper对象和dao. 使用spring和mybatis整合开发mapper ...

  7. Mybatis与Spring整合,使用了maven管理项目,作为初学者觉得不错,转载下来

    转载自:http://www.cnblogs.com/xdp-gacl/p/4271627.html 一.搭建开发环境 1.1.使用Maven创建Web项目 执行如下命令: mvn archetype ...

  8. Spring整合HBase

    Spring整合HBase Spring HBase SHDP § 系统环境 § 配置HBase运行环境 § 配置Hadoop § 配置HBase § 启动Hadoop和HBase § 创建Maven ...

  9. Spring整合Ehcache管理缓存(转)

    目录 前言 概述 安装 Ehcache的使用 HelloWorld范例 Ehcache基本操作 创建CacheManager 添加缓存 删除缓存 实现基本缓存操作 缓存配置 xml方式 API方式 S ...

随机推荐

  1. 第一章 : Android Studio 介绍 [Learn Android Studio 汉化教程]

    摘自:http://ask.android-studio.org/?/question/789,为便于学习重新整理.. 本章将引导您完成安装和设置开发环境,然后你就可以跟随本书的例子和课程学习. 首先 ...

  2. Flask之模板web表单

    3.3 Web表单: web表单是web应用程序的基本功能. 它是HTML页面中负责数据采集的部件.表单有三个部分组成:表单标签.表单域.表单按钮.表单允许用户输入数据,负责HTML页面数据采集,通过 ...

  3. 四、jenkins+postman+newman环境搭建

    前提: 搭建环境之前需要先理清楚各个环境的依赖关系,jenkins只支持windows命令行跟linux shell环境执行构建命令,而postman的测试脚本不能直接在命令行或shell环境执行,p ...

  4. RocketMQ初探(二)之RocketMQ3.26版本搭建(含简单Demo测试案例)

    作为一名程序猿,要敢于直面各种现实,脾气要好,心态要棒,纵使Bug虐我千百遍,我待它如初恋,方法也有千万种,一条路不行,换条路走走,方向对了,只要前行,总会上了罗马的道. Apache4.x最新版本既 ...

  5. 基于 DirectX11 的 MMDViewer 04-渲染目标视图和多视口

    上篇文章给出了一个简单并且可以运行的渲染框架,接下来将介绍框架中的渲染管线构成. 1.创建渲染管线 在你创建完一个窗口后,接着便要创建渲染管线,使用的函数是 D3D11CreateDeviceAndS ...

  6. MySQL 执行 'use databases;' 时很慢

    问题描述: 就是这么个情况,登录数据库切换库时感觉很卡,需要等待几秒钟. 案例: shell > mysql -uroot -ppassword mysql> use databases; ...

  7. JQuery easyUi datagrid 中 editor 动态设置最大值最小值

    前言 近来项目中使用到 easyui 来进行页面设计,感觉挺方便的,但是网上除了api外,其他有价值的资料比较少,故在此分享一点经验,供大家参考.   问题 JQuery easyUi datagri ...

  8. MySQL数据库篇之数据类型

    主要内容: 一.数值类型 二.日期类型 三.字符串类型 四.枚举类型与集合类型 1️⃣ 数值类型 1.整数类型:tinyint  smallint  mediumint  int  bigint 作用 ...

  9. Mono在Full AOT模式下的限制

    [Mono在Full AOT模式下的限制] 调试时遇到一个Mono运行时异常: ExecutionEngineException: Attempting to JIT compile method ' ...

  10. iOS无网络提示或无数据提示空白页

    在我们平常我们用的app当中,当你在信号不好网络错误的时候,一般都会有个提示:“网络错误请点击重试~” 的话术,或者说当你浏览某一页的时候,没有数据,也会提示:“暂无数据,请搞点动静” 之类的话术. ...