Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is <form:select path> in spring tag used for?

Can someone please tell me what do I need to specify in <form:select> path attribute and what it's used for? actually I need to understand how the value of the selected item from the dropdown is passed onto the controller?

like image 746
Shivayan Mukherjee Avatar asked Apr 04 '14 10:04

Shivayan Mukherjee


People also ask

What is spring form path?

The 'path' attribute is the most important attribute for most of the tags in the library; it indicates what request-scoped property the input should bind to.

What is spring form tag?

The Spring MVC form tags are the configurable and reusable building blocks for a web page. These tags provide JSP, an easy way to develop, read and maintain. The Spring MVC form tags can be seen as data binding-aware tags that can automatically set data to Java object/bean and also retrieve from it.

What is the use of path attribute?

The path attribute is the most important attribute of the input tag. This path attribute binds the input field to the form-backing object's property.

What is the use of spring form?

A springform pan is a round cake pan that features a removable bottom and sides. The sides are held together with an interlocking band that can be opened and removed once your baked good is out of the oven, leaving your cake on the base.


1 Answers

Say you have a model (Dog for example), a Dog has various attributes:
name
age
breed

if you want to make a simple form for adding/editing a dog, you'd use something that looks like this:

<form:form action="/saveDog" modelAttribute="myDog">

    <form:input path="name"></form:input>
    <form:input path="age"></form:input>
    <form:select path="breed">
        <form:options items="${allBreeds}" itemValue="breedId" itemLabel="breedName" />
    </form:select>

</form:form>

As you can see, I've chosen the breed property to be a select, because I don't want the user to type whatever breed he want, I want him to choose from a list (allBreeds in this case, which the controller will pass to the view).

In the <form:select> I've used path to tell spring that the select has to bind to the breed of the Dog model.

I've also used <form:options> to fill the select with all the options available for the breed attribute.

The <form:select> is smart, and if it's working on a populated model (i.e a Dog fetched from the database or with default breed value) - it will automatically select the "right" option from the list.

In this case, the controller will look similar to this:

@RequestMapping(value="/saveDog")
public String saveDog(@ModelAttribute("myDog") Dog dogFromForm){
    //dogFromForm.getBreed() will give you the selected breed from the <form:select
...
//do stuff
...
}

I hope my answer gave you a general idea.

like image 127
Shay Elkayam Avatar answered Oct 12 '22 09:10

Shay Elkayam