How correctly use the Spring MVC <form:select> tag to show the value of a specific object field into a collection?

user4296472 picture user4296472 · Sep 16, 2015 · Viewed 16.4k times · Source

I am pretty new in Spring MVC and I have some difficulties to understand how exactly works the tag.

So I have the following situation.

Into a controller I have this method:

@RequestMapping(value = "/consultazioneMinisteriale", method = RequestMethod.GET)
public String consultazione(Locale locale, Model model) {

    List<Twb1012Regione> listaRegioni = geograficaService.getListaRegioni();

    System.out.println("Numero regioni: " + listaRegioni.size());

    model.addAttribute("listaRegioni", listaRegioni);

    return "utenteMinisteriale/consultazione";
}

As you can see this method retrieve a List of Twb1012Regione object and put it into the model object so it will be available into the consultazione.jsp page.

So the Twb1012Regione class is a model object like this:

@Entity
@Table(name="anagrafiche.TWB1012_REGIONE")
@NamedQuery(name="Twb1012Regione.findAll", query="SELECT t FROM Twb1012Regione t")
public class Twb1012Regione implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @Column(name="COD_REG")
    private String codReg;

    @Column(name="DES_REG")
    private String desReg;

    .....................................
    .....................................
    OTHER FIELDS
    .....................................
    .....................................
}

Where the codReg field univocally identify the object and the desReg contain the value that I want to show as value into the tag.

Finnaly this is the code of my consultazione.jsp view:

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<%@ page session="false"%>
<%@  taglib prefix="security" uri="http://www.springframework.org/security/tags" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>

<!DOCTYPE html>
<html>
    <head>
        <title>Home</title>
    </head>

    <body class="azure">
        <h1>Hello World</h1>

        <!--  <div> ${listaRegioni} </div> -->


        <div>
            <label>Regioni:</label>
            <form:select path="listaRegioni" items="${listaRegioni}"/>
        </div>

    </body>
</html>

The problem is that doing in this way I obtain the select dropdown but it show the reference of all my Twb1012Regione objects and not the name of the desReg field.

This is the HTML rendered output:

<select>
    <option value="it.myCompany.myProject.anagrafiche.Twb1012Regione@5a259924">it.myCompany.myProject.anagrafiche.Twb1012Regione@5a259924</option>
    <option value="it.myCompany.myProject.anagrafiche.Twb1012Regione@4a87c8d3">it.myCompany.myProject.anagrafiche.Twb1012Regione@4a87c8d3</option>
    <option value="it.myCompany.myProject.anagrafiche.Twb1012Regione@815b53a">it.myCompany.myProject.anagrafiche.Twb1012Regione@815b53a</option>
    .................................................
    .................................................
    .................................................
</select>

Why? What am I missing? How can I shoe the value of the desReg field of each Twb1012Regione instead the reference of the objects?

EDIT-1:

I tryied to change into:

<form:select path="regioni">
   <form:options items="${listaRegioni}" itemLabel="desReg" itemValue="codReg" />
</form:select>

But now when the page is rendered I obtain this error message into my stacktrace:

12:44:52,112 ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/WIFIPNSD].[jsp]] (http-localhost/127.0.0.1:8080-4) JBWEB000236: Servlet.service() for servlet jsp threw exception: java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'regioni' available as request attribute

Why? What is wrong? How can I solve it?

Answer

Jordi Castilla picture Jordi Castilla · Sep 16, 2015
  • In <form:select> tag you must put in the path the name of the bean attribute will keep the id of the selected item.
  • In <form:options>:
    • items: the bean attribute containing the list of selectable items
    • itemLabel: the description to be shown in the dropbox
    • itemValue: the field to be saved (usually id) in the path of the <form:select>

<form:select path="regioni">
    <form:options items="${listaRegioni}" itemLabel="desReg" itemValue="codReg" />
</form:select>

Will show you a dropbox with all descriptions (desReg) of the regions, and will keep the the codReg of the selected item in the bean attribute regioni