Fill combo box using Spring MVC

0

I'm starting with spring mvc and I would like to know how to fill select , I have seen examples on the Internet but they are not very clear, I do not know if there is a simple and clear way, I know that the form:options is used but I would like a complete and detailed example with its drivers, complete jsp files, I found this example code

@RequestMapping(value="/phone-page")
private ModelAndView selectTag() {
    ModelAndView mav = new ModelAndView("phone-form");

    Map< String, String > phones = new HashMap<String, String>();
    phones.put("samsung", "SAMSUNG");
    phones.put("nokia", "NOKIA");
    phones.put("iphone", "IPHONE");

    mav.addObject("phonesMap", phones);
    mav.addObject("smartphone", new Smartphone());

    return mav;
}

HTML code

<h1>Phone page</h1>
Select phone:
<form:form method="POST" commandName="smartphone" action="phone-result.html">
<table>
    <tr>
    <td>
        <ul>
            <form:select path="phone" items="${phonesMap}"/>
        </ul>
    </td>
    </tr>
    <tr>
        <td>
            <input type="submit" value="Submit"/>
        </td>
    </tr>
</table>  
</form:form>

My doubts are why is this line necessary?

mav.addObject("smartphone", new Smartphone());

Why does the form have to carry this?

commandName="smartphone"

Thank you.

    
asked by Lemi 14.03.2017 в 21:11
source

2 answers

0
  

My doubts are why is this line necessary?

     
    

mav.addObject ("smartphone", new Smartphone ());

  

In this line, a Smartphone instance is created and passed to the view. Its values are those that will be loaded into the jsp. That is, if the smartphone object has the "phone" property, that value is the one that would be selected in the select of the jsp.

  

Why does the form have to carry this?

     
    

commandName="smartphone"

  

The value of the "commandName" attribute indicates the name of the variable that is used to link the form data with a controller variable. In this case a variable called "smartphone" and corresponds to variable "smartphone" that you have in the controller:

mav.addObject("smartphone", new Smartphone());

In the new versions of Spring it is recommended to use the modelAttribute tag instead of the commandName. Its operation is the same:

modelAttribute="smartphone"
    
answered by 15.03.2017 в 10:23
0
mav.addObject("smartphone", new Smartphone());

This line says that you will send a new Smartphone object (with its default values) to mav , in this case the mav has been created to send all the values you want to view phone-form , in your example no says how is it called the jsp, but I assume it is phone-form.jsp

ModelAndView mav = new ModelAndView("phone-form");

The .jsp receives this value called "smartphone" and finds that it has a form called in the same way

<form:form method="POST" commandName="smartphone" action="phone-result.html">

What the .jsp will do is replace the paths of the form with the properties of the smartphone object (which in this case is a new Smartphone() )

On the other hand, in the form there is a select whose elements will be filled with the values of the variable ${phonesMap}

<form:select path="phone" items="${phonesMap}"/>

This variable ${phonesMap} , is a HashMap containing 3 elements sent from the controller with "samsung" id and "SAMSUNG" value for the first element of the select.

Map< String, String > phones = new HashMap<String, String>();
    phones.put("samsung", "SAMSUNG");
    phones.put("nokia", "NOKIA");
    phones.put("iphone", "IPHONE");
mav.addObject("phonesMap", phones);

And finally you should know that by submitting the form the form will go to where it indicates the action of the form

@RequestMapping(value="/phone-result.html")

In which you can pick the object Smartphone with the value of the chosen select within its property phone

    
answered by 15.03.2017 в 12:03