Spring中的Path属性

Par*_*uja 21 java spring class

任何人都可以解释一下path属性如何在Spring中将对象从html表单绑定到Java类.我是春天网页框架的新手,请帮忙.

ger*_*tan 46

长话短说,使用java bean约定将path属性绑定到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>
Run Code Online (Sandbox Code Playgroud)

并遵循控制器处理方法:

@RequestMapping(...)
public String updateStudent(@ModelAttribute("theStudent") Student student) {
  // ...
}
Run Code Online (Sandbox Code Playgroud)

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

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; }
}
Run Code Online (Sandbox Code Playgroud)

有关JavaBeans转换的更多信息,请参阅规范文档的第8.3节.

  • 它只是modelAttribute的另一种语法.所以`commandObject ="theStudent"`会给出相同的结果 (2认同)