2011-08-23 62 views
1

在試圖清理一些漂亮這些混沌的代碼我改寫的東西是這樣的:JSP:元素創建<img></img>元素

<jsp:element name="img"> 
    <jsp:attribute name="src"> 
     <c:url value="${akamai}/images/prdLargerImage.gif"/> 
    </jsp:attribute> 
    <jsp:attribute name="alt">Zoom Image</jsp:attribute> 
</jsp:element> 

...卻驚訝地發現,JSP生成的驗證碼

<img src="/images/prdLargerImage.gif" alt="Zoom Image"> 


      </img> 

有沒有辦法強制它生成<img src="/images/prdLargerImage.gif" alt="Zoom Image"/>

+0

我應該指出,我也嘗試了上面的代碼,手動刪除所有匿名空白。結果是'Zoom Image' – kojiro

回答

1

可以配置容器修剪是空白路程以下條目web.xml

<jsp-config> 
    <jsp-property-group> 
     <url-pattern>*.jsp</url-pattern> 
     <trim-directive-whitespaces>true</trim-directive-whitespaces> 
    </jsp-property-group> 
</jsp-config> 

有什麼可以做,對具有懸掛</img>,而不是短標籤<img/>。但是,如果你的目標是一個兼容Servlet 2.4/JSP 2.0的容器(在模板文本中支持EL),我寧願重寫一塊醜陋的東西(它完全沒有額外的優勢),如下所示:

<img src="${akamai}/images/prdLargerImage.gif" alt="Zoom Image" /> 
+0

在我可以信任'akamai'包含XML/URL安全值的所有情況下,這絕對是一個乾淨的解決方案(如果我只是在JSP 2.0中)。與此同時,它看起來像我將不得不編寫格式錯誤的XML JSP或打開/關閉'img'標籤。 – kojiro

+1

」不對其「值」進行XML/URL編碼。它只對所有嵌套的「」的「值」進行URL編碼。 – BalusC