2012-07-18 18 views
3

每當我使用#springFormInput這樣的:速度 - #springFormInput和定製ID

#springFormInput("command.name") 

我獲得ID設置爲name標記:

<input type="text" id="name" ... /> 

在許多情況下,我想簡短的名稱,如 「類別」或「名稱」,我不覺得讓他們在ID(這應該是唯一的)。我如何指示Spring爲元素使用不同的ID?

我想是這樣的:

#springFormInput("command.name" 'id="myid" data-zaza="test"') 

...但我的ID仍然被改寫:

<input type="text" id="name" data-zaza="test" ... /> 

我想它發出id="myid"代替。

回答

0

你不能。看看宏的代碼:

#** 
* springFormInput 
* 
* Display a form input field of type 'text' and bind it to an attribute 
* of a command or bean. 
* 
* @param path the name of the field to bind to 
* @param attributes any additional attributes for the element (such as class 
* or CSS styles or size 
* 
*# 
#macro(springFormInput $path $attributes) 
    #springBind($path) 
    <input type="text" id="${status.expression}" name="${status.expression}" 
          value="$!status.value" ${attributes}#springCloseTag() 
#end 

所以,你可以看到,使用#springFormInput("command.name" 'id="myid" data-zaza="test"')實際上產生:

<input type="text" id="name" id="myid" data-zaza="test" /> 

但速度抹了重複的ID,只留下第一個(id="name")。當你做一個測試


這種「消滅」的行爲變得清晰。

<input type="text" id="testA" id="testB" id="testC" name="myTest" value="test!"> 

什麼在生成的HTML走的是:例如,通過手工申報

<input type="text" id="testA" name="myTest" value="test!"> 


如當時的選擇,你可以明確地使用宏代碼,生成你需要:

#springBind("command.name") 
<input type="text" id="myid" name="${status.expression}" 
         value="$!status.value" data-zaza="test"#springCloseTag() 

${status.expression} and $!status.value設置在宏觀#springBind,不要擔心它們。