1. 程式人生 > >web.xml中配置JSP屬性

web.xml中配置JSP屬性

為什麼要在web.xml配置JSP屬性

如果許多JSP有著相似的屬性,那麼在每個JSP檔案的頂部重複新增page指令是非常麻煩的工作。幸運的是,在部署描述符中可以配置通用的JSP屬性。

web.xml中新增JSP屬性樣例

<jsp-config>
	<jsp-property-group>
		<url-pattern>*.jsp</url-pattern>
		<url-pattern>*.jspf</url-pattern>
		<page-encoding>UTF-8</page-encoding>
		<scripting-invalid>false</scripting-invalid>
		<include-prelude>/WEB-INF/jsp/base.jspf</include-prelude>
		<trim-directive-whitespaces>true</trim-directive-whitespaces>
		<default-content-type>text/html</default-content-type>
	</jsp-property-group>
</jsp-config>

瞭解JSP屬性組

<jsp-config>中可以包含任意數目的<jsp-property-group>標籤。通過為<jsp-property-group>定義不同的<url-pattern>標籤來區分不同的屬性組。

<include-prelude>標籤,將告訴容器在所有屬於改該屬性組的JSP的頭部新增檔案/WEB-INF/jsp/base.jspf。

<include-coda>標籤定義了包含在組中所有JSP尾部的檔案。

在一個JSP組中可以同時使用這些標籤多次。
<page-encoding>與page指令的pageEncoding特性一致。

<default-content-type>標籤可以定義內容型別,預設為text/html
<trim-directive-whitespaces>也是一個特別有用的屬性,該屬性告訴JSP轉換器刪除響應輸出中的空白,只保留指令、宣告、指令碼和其他JSP標籤建立的文字。
<scripting-invalid>標籤可以實現完全禁止JSP中的Java
<el-ignored>的作用類似,不過它對應的是page指令中的isELIgnored特性。
除了<url-pattern>,<jsp-property-group>中所有標籤都是可選的,但在使用它們時必須按照下面的順序新增到<jsp-property-group>中(忽略掉部希望使用的標籤):<url-pattern>、<el-ignored>、<page-encoding>、<scripting-invalid>、<is-xml>、<include-prelude>、<include-coda>、<deferred-syntax-allowed-as-literal>、<trim-directive-whitespace>、<default-content-type>、<buffer>、<error-on-undeclared-namespace>。