Path attribute in Spring

65,236

Long story short the path attribute is bound into java properties using java beans convention. For example for following form:

<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>

And following controller handler method:

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

Will bind automatically if the Student class is defined with following properties:

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; }
}

More info of the JavaBeans convetion is available at section 8.3 of the specification document.

Share:
65,236

Related videos on Youtube

Paritosh Ahuja
Author by

Paritosh Ahuja

Updated on March 04, 2020

Comments

  • Paritosh Ahuja
    Paritosh Ahuja over 4 years

    Can anyone please explain how path attribute works in binding objects from a html form to a Java class in Spring. I am newbie to spring web framework please help.

    • Oneb
      Oneb almost 11 years
      Try searching "spring form tags". It looks like html tags with spring attributes to bind an html form to an object.
    • Paritosh Ahuja
      Paritosh Ahuja almost 11 years
      i did that but no useful information,was unable to understand,so tried asking Stack :)
    • Admin
      Admin almost 11 years
  • Paritosh Ahuja
    Paritosh Ahuja almost 11 years
    what about command object?
  • gerrytan
    gerrytan almost 11 years
    It's just another syntax for modelAttribute. So commandObject="theStudent" will give same result
  • FrankelStein
    FrankelStein over 8 years
    Hello, this won't work unless you add commandName="command" in the form.