2012-12-18 43 views
1

我有一點問題。我在primefaces中使用gmap,我們需要加載腳本獲取用戶腳本的語言環境

<script src="http://maps.google.com/maps/api/js?sensor=true" type="text/javascript"/> 

但是我需要根據用戶語言環境加載腳本。

如何在不對字符串「硬編碼」的情況下做到這一點?

我想是這樣的:

<script src="http://maps.google.com/maps/api/js?sensor=true&amp;language="#{template.userLocale} type="text/javascript"/> 
// {template.userLocale} has a string o the locale 

你能幫忙嗎?

回答

1

你在那裏有一個HTML語法錯誤。你最終得到看起來像這樣給出的en語言:

<script src="http://maps.google.com/maps/api/js?sensor=true&amp;language="en type="text/javascript"/> 

(在瀏覽器中右擊頁面並做查看源代碼,看看它自己)

您需要將雙引號移到屬性值的末尾:

<script src="http://maps.google.com/maps/api/js?sensor=true&amp;language=#{template.userLocale}" type="text/javascript"/> 

這樣的HTML最終會是:

<script src="http://maps.google.com/maps/api/js?sensor=true&amp;language=en" type="text/javascript"/> 

EL只能在模板文本中使用。你需要認識到JSF基本上生成HTML代碼。 HTML <script src>屬性和EL #{}不同步運行。相反,JSF/EL會生成它,您只需確保生成的HTML在語法上是有效的。

+0

這是一個新手的錯誤。感謝BaluscC – Zebedeu