基本问题,但未在SO上找到任何其他相关帖子。
我的JSP中有这个Spring代码,其中包含一个名为vacant
的属性:
<form:hidden path="vacant" value="false"/>
这是生成的输出:
<input id="vacant" name="vacant" value="false" type="hidden" value=""/>
为什么value
会以空的第二个打印两次?
(这是相关的,因为我试图在某些Javascript中使用value
。)
答案 0 :(得分:1)
您所看到的是&#34;正常&#34;。根据文件:
此标记呈现HTML&#39;输入&#39;标记类型&#39;隐藏&#39;使用 约束值
假设您的DTO中空缺Boolean vacant;
,因为其值为空,标记会将其打印为value=""
。此外,它还会打印您传递给它的任何其他字段,例如:
<form:hidden path="vacant" my-field="test"/>
<input id="vacant" name="vacant" my-field="test" type="hidden" value=""/>
因此,如果您恰好使用<form:hidden path="vacant" my-field="test" value="true"/>
中的值,则会将其视为附加字段:
<input id="vacant" name="vacant" my-field="test" value="false" type="hidden" value=""/>
以下是来源中发生的事情:
<强> org.springframework.web.servlet.tags.form.HiddenInputTag 强>
protected int writeTagContent(TagWriter tagWriter) throws JspException {
...
writeDefaultAttributes(tagWriter); // *) Here it'll print the value that you passed to the tag
...
//The next two statements get the bound value of vacant (null) and print it as value=""
String value = getDisplayString(getBoundValue(), getPropertyEditor());
tagWriter.writeAttribute("value", processFieldValue(getName(), value, "hidden"));
*)writeDefaultAttributes()调用writeOptionalAttributes(),打印传递的值(它在this.dynamicAttributes中与my-field一起):
if (!CollectionUtils.isEmpty(this.dynamicAttributes)) {
for (String attr : this.dynamicAttributes.keySet()) {
tagWriter.writeOptionalAttributeValue(attr, getDisplayString(this.dynamicAttributes.get(attr)));
}
}
因此,http:input旨在用于绑定值,因此在呈现JSP之前在DTO中设置所需的值。