FreeMarker与SpringMVC整合

FreeMarker与SpringMVC整合


摘要: SpringMVC环境的搭建在这里就不多说了,我们这节主要是FreeMarker与SpringMVC整合 首先,在springmvc的配置文件普通视图之前,加入freemarker的视图...

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

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

fre-servlet.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<!-- 一定要放在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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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

1
2
3
4
5
6
7
8
9
10
11
12
<%@ 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目录下有

1
2
3
4
5
6
7
8
9
< 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时,显示李四

到此就整合成功了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值