2010-10-21 43 views
2

我有一個問題:假設在Spring MVC 3.0環境中,我使用Tiles管理視圖:我有一個帶有所有視圖定義的xml文件。每個視圖都擴展了特定的模板。我有兩個模板:一個用於渲染完整的DOM()和一個用於partialDOM(.....)。問題是,有一些視圖可以在fullDOM中檢索,也可以在partialDOM中檢索,但我不想寫兩個相似的定義。Apache Tiles:運行時更改模板頁面

我在想一個動態的方法:在運行時注入一個視圖的模板,指定一個http參數,它應該包含模板的名稱。如果請求包含參數,則Tiles應覆蓋視圖擴展的模板,並使用由http參數值檢測到的模板。

有些建議?

回答

2

在春天的TilesConfigurer需要設置可變容器:

<property name="useMutableTilesContainer" value="true"/> 
<property name="checkRefresh" value="true"/> 

而且在Spring控制器:

ModelAndView model = new ModelAndView(); 
MutableTilesContainer container = (MutableTilesContainer)ServletUtil.getContainer(request.getSession().getServletContext()); 
Attribute attribute = new Attribute("your template jsp"); 
HashMap<String, Attribute> attributes = new HashMap<String, Attribute>(); 
attributes.put("body", attribute); 
Definition definition = new Definition("your definition name", "your jsp", attributes); 
definition.setExtends("your definition template name"); 
definition = PatternUtil.replacePlaceholders(definition, "your definition name", new Object()); 
container.register(definition, request, response); 
model.setViewName("your definition name"); 
3

我知道這是一個老問題,但我需要做的這事,所以我想我會分享我的解決方案。

瓷磚允許他們稱之爲「runtime composition」,它允許您修改定義。所以,你可以重用現有的定義,只是換模板:

<tiles:insertDefinition name="existingDefinition" template="alternateTemplate.jsp" />