我是靠谱客的博主 健壮飞鸟,这篇文章主要介绍SpringMVC整合FreeMarker产生模板视图,现在分享给大家,希望可以做个参考。

引入包:

复制代码
1
2
3
4
5
<dependency> <groupId>org.freemarker</groupId> <artifactId>freemarker</artifactId> <version>2.3.20</version> </dependency>
第一次引入的包groupId为freemarker,版本2.3.8,报错,改为org.freemarker正常。


配置:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"> <!-- 扫描的包 --> <context:component-scan base-package="com.smart.web"/> <!-- HandlerMapping和HandlerAdapter及数据转换器配置 --> <mvc:annotation-driven conversion-service="conversionService"> <mvc:message-converters register-defaults="true"> <bean class="org.springframework.http.converter.BufferedImageHttpMessageConverter"/> <bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter"/> <bean class="org.springframework.http.converter.StringHttpMessageConverter" /> <bean class="org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter"/> <bean class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter" p:marshaller-ref="xmlMarshaller" p:unmarshaller-ref="xmlMarshaller"> </bean> <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/> </mvc:message-converters> </mvc:annotation-driven> <!-- 数据转换器 --> <bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean"> <property name="converters"> <list> <bean class="com.smart.domain.StringToUserConvert" /> </list> </property> </bean> <!-- xml消息转换器 --> <bean id="xmlMarshaller" class="org.springframework.oxm.xstream.XStreamMarshaller"> <property name="streamDriver"> <bean class="com.thoughtworks.xstream.io.xml.StaxDriver"/> </property> <property name="annotatedClasses"> <list> <value>com.smart.domain.User</value> </list> </property> </bean> <!-- 在使用Excel/PDF/XML的视图时,请先把这个视图解析器注释掉,否则产生视图解析问题--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:order="100" p:viewClass="org.springframework.web.servlet.view.JstlView" p:prefix="/WEB-INF/views/" p:suffix=".jsp"/> <!-- FreeMarker基础设施及视图解析器配置 --> <bean class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer" p:templateLoaderPath="/WEB-INF/ftl" p:defaultEncoding="UTF-8"> <property name="freemarkerSettings"> <props> <prop key="classic_compatible">true</prop> </props> </property> </bean> <bean class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver" p:order="5" p:suffix=".ftl" p:contentType="text/html; charset=utf-8"/> </beans>

order指定的优先级中,freeMarker视图高于InternalResource视图,所以系统使用freeMarker解析视图。


接口:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package com.smart.web; import java.io.UnsupportedEncodingException; import java.util.ArrayList; import java.util.List; import org.springframework.http.HttpEntity; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; import com.smart.domain.User; @Controller @RequestMapping("/sp4") public class Sp4Controller { @RequestMapping(value="m1",method=RequestMethod.GET) @ResponseBody public String m1(){ return "inm1"; } @RequestMapping(value = "/m2",method=RequestMethod.POST) public ResponseEntity<User> m2(HttpEntity<User> requestEntity) throws UnsupportedEncodingException { User user = requestEntity.getBody(); System.out.println("######:"+user+"---"+user.getRealName()); user.setUserId("1000"); return new ResponseEntity<User>(user, HttpStatus.OK); } @RequestMapping(value="/m3",method=RequestMethod.GET) public String m3(ModelMap mm){ List<User> list = new ArrayList<User>(); User user = new User(); user.setUserId("1"); user.setUserName("zhangsan"); list.add(user); User user1 = new User(); user1.setUserId("2"); user1.setUserName("lisi"); list.add(user1); mm.addAttribute("list", list); return "suc"; } @RequestMapping(value="/m4" ,method=RequestMethod.GET) public String m4(ModelMap mm){ List<User> list = new ArrayList<User>(); User user = new User(); user.setUserId("1"); user.setUserName("zhangsan"); list.add(user); User user1 = new User(); user1.setUserId("2"); user1.setUserName("lisi"); list.add(user1); mm.addAttribute("userList", list); return "userFtl"; } }

m4方法;


模板,和配置对应,在/WEB-INF/ftl  下创建 userFtl.ftl文件

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<#import "spring.ftl" as spring /> <html> <head> <title>smart</title> </head> <body> 用户列表 <table> <#list userList as user> <tr> <td> <a href="<@spring.url '/user/showUser/${user.userName}.html'/>"> ${user.userName} </a> </td> <td>${user.userId}</td> <td>${user.userId}</td> </tr> </#list> <table> </body> </html>

这里使用spring定义的宏,<@spring.url '/user/showUser/${user.userName}.html'/>产生的路径为相对于项目项目部署路径。

最后

以上就是健壮飞鸟最近收集整理的关于SpringMVC整合FreeMarker产生模板视图的全部内容,更多相关SpringMVC整合FreeMarker产生模板视图内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(63)

评论列表共有 0 条评论

立即
投稿
返回
顶部