Java 为 spring 表单标签元素设置类和其他属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24674064/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 14:01:39  来源:igfitidea点击:

setting class and other attributes for spring form tag element

javahtmljspspring-mvc

提问by Aniket Thakur

I have the following page and it does not load

我有以下页面,但无法加载

<form:form action="/test.htm"  method="post" commandName="demoForm" >

<div id="testSection" style="margin-top: 1.5%;margin-left: 3.5%;">

    <span class="test-container">
            <label>UserName</label>
            <span class="test-container-right">
                <form:input path="username" value="${UNAME}"  class="text simpleTextField" maxlength="200" style="width:60%" disabled/>
            </span>
    </span>

    <span style="width:auto; padding-left: 30%; padding-bottom: 4%; text-align:center; float:right; clear:both;">
        <input type="submit" class="btn" style="width:auto;" value="Save" />
    </span>     

</div>

</form:form>

but when I change it to

但是当我把它改成

...
<span class="test-container-right">
        <form:input path="username" />
</span>
...

it works and loads correctly. Why am I not allowed to set html properties for form:input spring tag. How can I achieve this? On inspecting the element it expands to be

它可以正常工作并正确加载。为什么我不允许为 form:input spring 标签设置 html 属性。我怎样才能做到这一点?在检查元素时,它扩展为

<input id="username" type="text" value="" name="username"></input>

I need to populate its value as well as provide it with a class and additional attributes like width.

我需要填充它的值并为它提供一个类和额外的属性,比如宽度。

采纳答案by Aniket Thakur

Ok I resolved some part of it. Looks like tags used inside form:input tag are different than regular html tags. All of them are listed here. For ex styleis cssStyle.

好的,我解决了其中的一部分。看起来 form:input 标签中使用的标签与常规 html 标签不同。所有这些都在这里列出。对于 exstylecssStyle

So I change my code to

所以我将代码更改为

<form:input path="username" cssClass="text simpleTextField" maxlength="200" cssStyle="width:60%" disabled="true"/>

<form:input path="username" cssClass="text simpleTextField" maxlength="200" cssStyle="width:60%" disabled="true"/>

and now it works..

现在它起作用了..

I still don't know how to populate value in this input. These seems no equivalent of valuekeyword.

我仍然不知道如何在此输入中填充值。这些似乎不等同于value关键字。

回答by Santhosh

@Aniket You actually have an equivalent , consider in a case you have to populate the selectbox with the values from the model attribute . You can make use of itemsattribute.

@Aniket 您实际上有一个等效项,请考虑在您必须select使用模型属性中的值填充框的情况下。您可以使用items属性。

For instance ,

例如 ,

     <tr>
      <td>City :</td>
      <td><form:select path="city" items="${cityList}" /></td>
     </tr>

It will generate the select with the the list of objects. cityListhere refers the object that has been sent from the server side.

它将生成带有对象列表的选择。 cityList这里指的是已经从服务器端发送过来的对象。

Hope this helps !

希望这可以帮助 !