gpt4 book ai didi

java - Spring中的路径属性

转载 作者:IT老高 更新时间:2023-10-28 13:49:03 25 4
gpt4 key购买 nike

谁能解释一下 path 属性如何在 Spring 中将对象从 html 表单绑定(bind)到 Java 类。我是 Spring Web 框架的新手,请帮忙。

最佳答案

长话短说,path 属性使用 java beans 约定绑定(bind)到 java 属性中。例如以下形式:

<form:form method="post" modelAttribute="theStudent">
Name: <form:input type="text" path="name"/>
Cool?: <form:input type"checkbox" path="cool"/>
<button>Save</button>
</form:form>

以及以下 Controller 处理方法:

@RequestMapping(...)
public String updateStudent(@ModelAttribute("theStudent") Student student) {
// ...
}

如果 Student 类使用以下属性定义,将自动绑定(bind):

public class Student {
private String name;
public String getName() { return this.name; }
public void setName(String name) { this.name = name; }

private boolean cool;
public boolean isCool() { return this.cool; }
public void setCool(boolean cool) { this.cool = cool; }
}

更多关于 JavaBeans 的信息请访问 section 8.3 of the specification document .

关于java - Spring中的路径属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17647050/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com