[color=red]使用freemarker.properties配置方式[/color]: [url]http://www.bug315.com/article/176.htm[/url]
freemarker.properties
tag_syntax=auto_detect
template_update_delay=2
default_encoding=UTF-8
output_encoding=UTF-8
locale=zh_CN
date_format=yyyy-MM-dd
time_format=HH:mm:ss
datetime_format=yyyy-MM-dd HH:mm:ss
/WEB-INF/ftl/
[b][color=red]赋值和使用方式: [/color][/b]
[color=blue]1. 使用Model model或者ModelMap map[/color]
public String list(Model model, ModelMap map) {
Collection sessions = sessionDAO.getActiveSessions();
model.addAttribute("sessions", sessions);
model.addAttribute("sesessionCount", sessions.size());
map.put("sessions1", sessions);
map.put("sesessionCount1", sessions.size());
map.addAttribute("sessions2", sessions);
map.addAttribute("sesessionCount2", sessions.size());
return "views/online.ftl";
}
在线用户后台管理, 在线个数: ${sesessionCount},${sesessionCount1},${sesessionCount2},
sesessionCount is 1;
#if>
sesessionCount1 is 1;
#if>
sesessionCount2 is 1;
#if>
[color=blue]2. 使用Map方式[/color]
public String list(Model model, Map map) {
Collection sessions = sessionDAO.getActiveSessions();
model.addAttribute("sessions", sessions);
model.addAttribute("sesessionCount", sessions.size());
map.put("sessions1", sessions);
map.put("sesessionCount1", sessions.size());
return "views/online.ftl";
}
在线用户后台管理, 在线个数: ${sesessionCount},${sesessionCount1}
sesessionCount is 1;
#if>
sesessionCount1 is 1;
#if>
spring mvc版本 4.1.5
sitemesh版本 3.0.0
freemarker-2.3.22
spring ioc 4.1.5
唯一需要更改iade地方 以前返回时不加后缀,现在返回时请加入后缀,必须你想使用jsp文件就返回 模板名称.jsp,
使用freemarker模板就返回 模板.ftl ,注意在springmvc里面2个试图解析器都不能定义后缀,否则只会调用jsp解析器
标签: Spring SiteMesh FreeMarker
代码片段(5) [全屏查看所有代码]
1. [文件] web.xml
springmvc
springmvc
contextConfigLocation
classpath:beans.xml
org.springframework.web.context.ContextLoaderListener
hiddenHttpMethodfilter
org.springframework.web.filter.HiddenHttpMethodFilter
hiddenHttpMethodfilter
/*
sitemesh
com.sniper.springmvc.sitemesh3.MySiteMeshFilter
sitemesh
/*
springDispatcherServlet
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:springmvc.xml
1
springDispatcherServlet
/
2. [文件] beans.xml
3. [文件] springmvc.xml
auto_detect
5
UTF-8
UTF-8
zh_CN
true,false
yyyy-MM-dd HH:mm:ss
yyyy-MM-dd
HH:mm:ss
0.######
true
500
404
500
4. [文件] MySiteMeshFilter.java
package com.sniper.springmvc.sitemesh3;
import org.sitemesh.builder.SiteMeshFilterBuilder;
import org.sitemesh.config.ConfigurableSiteMeshFilter;
public class MySiteMeshFilter extends ConfigurableSiteMeshFilter {
@Override
protected void applyCustomConfiguration(SiteMeshFilterBuilder builder) {
builder.addDecoratorPath("/springmvc**", "/WEB-INF/mvc/main.jsp")
.addExcludedPath("/springmvc/login**")
.addExcludedPath("/springmvc/admin-print**")
.addExcludedPath("/springmvc/file-upload**")
.addDecoratorPath("/**", "/WEB-INF/mvc/main.jsp")
.addExcludedPath("/myfiles/**");
}
}
5. [文件] Index.java
package com.sniper.springmvc;
import java.util.Map;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.sniper.springmvc.util.SearchUtil;
@RequestMapping("/")
@Controller
public class Index {
private final static String SUCCESS = "success";
private final static String INDEX = "index";
@RequestMapping("/")
public String index(Map map) {
map.put("freemarker", "freemarker");
map.put("integers", 123456);
return "index.jsp";
}
@RequestMapping("search")
public String search(SearchUtil searchUtil, Map map) {
map.put("searchUtil", searchUtil);
System.out.println(searchUtil);
return "search";
}
@RequestMapping("ftl")
public String ftl(Map map) {
// 获取模板路径跳转
map.put("freemarker", "freemarker");
return "ftl.ftl";
}
}