SpringMVC环境的搭建在这里就不多说了,我们这节主要是FreeMarker与SpringMVC整合
首先,在springmvc的配置文件普通视图之前,加入freemarker的视图
fre-servlet.xml
1 <!-- 一定要放在viewResolver的前面,这样就先去找freemarker的 -->
2
3 <bean id="freemarkerConfig"
4
5 class="org.springframework.web.servlet
6
7 .view.freemarker.FreeMarkerConfigurer">
8
9 <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
1 package org. fre.controller;
2
3
4
5 import org.springframework.stereotype.Controller;
6
7 import org.springframework.ui.Model;
8
9 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
1 <%@ page language="java" contentType="text/html; charset=UTF-8"
2
3 pageEncoding="UTF-8"%>
4
5 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd">
6
7 <html>
8
9 <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目录下有
1 <html>
2
3 <head>
4
5 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
6
7 <title>Insert title here</title>
8
9 </head>
10
11 <body>
12
13 <h1>${username}</h1>
14
15 </body>
16
17 </html>
启动服务,
访问http://localhost:8080/hello时,显示张三
访问http://localhost:8080/world时,显示李四
到此就整合成功了
SpringMVC环境的搭建在这里就不多说了,我们这节主要是FreeMarker与SpringMVC整合
首先,在springmvc的配置文件普通视图之前,加入freemarker的视图
fre-servlet.xml
03 |
< bean id = "freemarkerConfig" |
05 |
class="org.springframework.web.servlet |
07 |
.view.freemarker.FreeMarkerConfigurer"> |
09 |
< property name = "templateLoaderPath" value = "/WEB-INF/ftl/" /> |
13 |
< bean id = "viewResolver" |
15 |
class="org.springframework.web.servlet |
17 |
.view.freemarker.FreeMarkerViewResolver"> |
19 |
< property name = "cache" value = "true" /> |
21 |
< property name = "prefix" value = "" /> |
23 |
< property name = "suffix" value = ".ftl" /> |
25 |
< property name = "contentType" value = "text/html; charset=UTF-8" /> |
31 |
class="org.springframework.web.servlet |
33 |
.view.InternalResourceViewResolver"> |
35 |
< property name = "viewClass" |
37 |
value = "org.springframework.web.servlet.view.JstlView" /> |
39 |
< property name = "prefix" value = "/WEB-INF/jsp/" /> |
41 |
< property name = "suffix" value = ".jsp" /> |
控制器HelloController
01 |
package org. fre.controller; |
05 |
import org.springframework.stereotype.Controller; |
07 |
import org.springframework.ui.Model; |
09 |
import org.springframework.web.bind.annotation.RequestMapping; |
15 |
public class HelloController { |
17 |
@RequestMapping("/hello") |
19 |
public String hello(Model model) { |
21 |
model.addAttribute("username", "张三"); |
27 |
@RequestMapping("/world") |
29 |
public String helloworld(Model model) { |
31 |
model.addAttribute("username","李四"); |
在WEB-INF/jsp目录下有一个world.jsp
01 |
<%@ page language= "java" contentType= "text/html; charset=UTF-8" |
03 |
pageEncoding= "UTF-8" %> |
11 |
<meta http-equiv= "Content-Type" content= "text/html; charset=UTF-8" > |
13 |
<title>Insert title here</title> |
在WEB-INF/ftl目录下有
05 |
<meta http-equiv= "Content-Type" content= "text/html; charset=UTF-8" > |
07 |
<title>Insert title here</title> |
启动服务,
访问http://localhost:8080/hello时,显示张三
访问http://localhost:8080/world时,显示李四
到此就整合成功了