Java Spring 表单标签问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7000901/
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-10-30 18:13:14  来源:igfitidea点击:

Java Spring form tag question

javaspringjspspring-mvc

提问by Jaanus

I am using JSPs, and this is my contacts.jsp

我正在使用 JSP,这是我的 contacts.jsp

<%@taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
<html>
<head>
    <title>Spring 3.0 MVC series: Contact manager</title>
</head>
<body>
<h2>Contact Manager</h2>
<form:form method="post" action="addContact.html">

    <table>
        <tr>
            <td><form:label path="firstName">First Name</form:label></td> // LABEL
            <td><form:input path="firstName" /></td>
        </tr>
        <tr>
            <td><form:label path="lastName">Last Name</form:label></td>
            <td><form:input path="lastName" /></td>
        </tr>
        <tr>
            <td><form:label path="lastName">Email</form:label></td>
            <td><form:input path="email" /></td>
        </tr>
        <tr>
            <td><form:label path="lastName">Telephone</form:label></td>
            <td><form:input path="telephone" /></td>
        </tr>
        <tr>
            <td colspan="2">
                <input type="submit" value="Add Contact"/>
            </td>
        </tr>
    </table>  

</form:form>
</body>
</html>

My questions is, that when i change form:label path="firstName"into form:label path="firstname", why tomcat starts throwing errors? Doesn't the model only need the input path, since the value inside input path, is the value it will use?

我的问题是,当我form:label path="firstName"变成 时form:label path="firstname",为什么 tomcat 开始抛出错误?模型是否只需要输入路径,因为输入路径内的值是它将使用的值吗?

EDIT:

编辑:

should i even use the form:label tag, why is it used?

我什至应该使用form:label tag,为什么使用它?

回答by Jeremy

It's because firstNameis either used literally, or is translated into the method getFirstName().

这是因为firstName要么按字面使用,要么翻译成方法getFirstName()

Either way, the capitalization matters when the lookup is performed.

无论哪种方式,在执行查找时大小写都很重要。



For your question on whether or not you need the pathin the label: If you provide the path, then the label will know which input it belongs to. In HTML, that allows you to click on the label and have the input field gain focus. I assume the output HTML for your code is something like this:

对于您是否需要path标签中的问题:如果您提供路径,则标签将知道它属于哪个输入。在 HTML 中,这允许您单击标签并使输入字段获得焦点。我假设您的代码的输出 HTML 是这样的:

<tr>
  <td><label for="firstName"></td>
  <td><input type="text" id="firstName"></td>
</tr>