2013-03-15 55 views
1

給出一個簡單的模板JSP由SiteMesh的3渲染:如何使用自定義屬性與SiteMesh的3

<%@include file="../jsp_inc/taglibs.jsp" %> 
<!DOCTYPE html> 
<head> 
    <link rel="stylesheet" type="text/css" href='<c:url value="/css/global.css" />' > 
</head> 
<body> 
<h1>[HEADING]</h1> 
<div> 
    <sitemesh:write property='body'/> 
</div> 
</body> 

該模板按預期工作,插入JSP元素的內容到模板。

正如您從上面所期望的那樣,我希望能夠將JSP中的值(例如.h1元素)插入到我的模板中的相應元素中。

我想:

<sitemesh:getProperty property="page.heading"></sitemesh:getProperty> 

模板/裝飾和:

<content tag="heading"></content> 

在JSP中,每一個問題上的SO,但我認爲這可能是指的sitemesh 2.我我正在使用Sitemesh 3.

有什麼建議嗎?

+0

順便說一下這篇文章改變了我的生活http://stackoverflow.com/questions/1296235/jsp-tricks-to -make-templating-easy我看不出再次使用SiteMesh的理由,jsp標籤徹底更好 – Magnus 2016-12-22 15:55:24

回答

9

不知道你是否還在使用Sitemesh 3,但我正在檢查它,並且在通過源代碼拖動之後發現你已經配置了構建器。

我使用的是Java based configuration,並創建了自己的子類,增加了支持的sitemesh 2風格的內容塊標記處理規則包:

public class MySiteMeshFilter extends ConfigurableSiteMeshFilter { 
    @Override 
    protected void applyCustomConfiguration(SiteMeshFilterBuilder builder) { 
     builder.addTagRuleBundle(new Sm2TagRuleBundle()); 
    } 
} 

通過XML配置sitemesh3.xml:

<sitemesh> 
    <content-processor> 
     <tag-rule-bundle class="org.sitemesh.content.tagrules.html.Sm2TagRuleBundle" /> 
    </content-processor> 
    <!-- Your mappings go here --> 
    <mapping ... /> 
</sitemesh> 

這樣做可以讓您使用內容標籤,如:

<content tag="heading"></content> 

在裝飾/模板頁面使用sitemesh:write標籤,而不是sitemesh:getProperty不存在了:

<sitemesh:write property="page.heading"/> 
+0

謝謝@Phuong LeCong。這對我有效。我想知道爲什麼這個答案沒有通過表決/接受。 – hemu 2014-10-31 12:14:57