【发布时间】:2012-06-24 14:07:03
【问题描述】:
我目前正在运行一个有效的 Spring + Apache Tiles webapp。 我需要展示一些示例代码来解释我的意图。
Apache Tiles 配置:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE tiles-definitions PUBLIC
"-//Apache Software Foundation//DTD Tiles Configuration 2.1//EN"
"http://tiles.apache.org/dtds/tiles-config_2_1.dtd">
<tiles-definitions>
<definition name="baseLayout" template="/WEB-INF/layouts/www_base.jsp" />
<definition name="home" extends="baseLayout">
<put-attribute name="body" value="/WEB-INF/views/home.jsp" />
</definition>
</tiles-definitions>
示例控制器:
@Controller
public class ExampleController {
@RequestMapping("/index.html")
public String index(Map<String, Object> map) {
map.put("hello", "world");
return "home";
}
}
这会将www_base.jsp 和home.jsp 显示为body。我可以在www_base.jsp 和home.jsp 中使用变量${hello}。
但我不想在每个 Controller 方法中设置 hello 以便能够在每个页面上的 www_base.jsp 中使用它。
有没有办法为www_base.jsp 设置全局变量,例如在ExampleController的构造函数中?
更新 使用地图的示例代码
@Controller
@RequestMapping("/")
public class BlogController {
@ModelAttribute
public void addGlobalAttr( Map<String, Object> map ) {
map.put("fooone", "foo1");
}
@RequestMapping("/index.html")
public String posts(Map<String, Object> map) {
map.put("foothree", "foo3");
return "posts";
}
}
【问题讨论】:
标签: spring spring-mvc tiles tiles2 apache-tiles