Good day. There is a controller from which Enum is sent to the page for the drop-down list.

@RequestMapping("/registration") public String registration(Model model){ ArrayList<CourseLocation> location = new ArrayList<CourseLocation>(); for(CourseLocation loc : CourseLocation.values()) { location.add(loc); } model.addAttribute("locat", location); return "registration"; } 

On the page, the information is inserted in a loop,

  LocationBtn:<br/> <select class="selectpicker" > <c:forEach items="${location}" var="location"> <option value ="${location.toString()}">${location.toString()}</option> </c:forEach> </select><br/> 

This good is in the form with other input type = "text" and the button is transferred to another controller to create a user. How can I transfer the required enum from the drop-down list to it?

  @RequestParam(required = false) CourseLocation location, 

Maybe I can transfer to the page from the first controller not ArrayList but Enum itself? But I did not succeed.

I tried to transfer to the second controller @RequestParam (required = false) CourseLocation location.toString () but the compiler was not happy with this solution.

Perhaps there is an option to override option value = "$ {loc}" and assign it somewhere to $ {loc = location.toString ()}?

Someone faced with such a task?

    2 answers 2

    You can pass the enum. First you need to import it to the page:

     <%@page import="my.package.CourseLocation"%> 

    Then write:

     <c:forEach items="<%=CourseLocation.values()%>" var="location"> <option value ="${location.name()}">${location.toString()}</option> </c:forEach> 

    (I did not check it myself. I found it on the Internet)

    It is important that value = location.name()
    toString() can be overridden, and name() always the same. This is the name of the item. The element can then be restored using valueOf(String name) .

     @RequestParam(required = false) String locationName, ... CourseLocation location = CourseLocation.valueOf(locationName); 

    Spring trying to apply? It would be worth adding a spring tag.
    And it is worth seeing - can spring help you?
    Suddenly and @RequestParam(required = false) CourseLocation location, will work when value=location.name() . toString wrote it in toString , here’s the standard recovery mechanism for enum, and it didn’t work.

    Well, there is another option using ordinal() . But with textual titles, it’s somehow clearer and therefore simpler. Immediately you can see where.

    By the way, I never redefine toString for enum. After all, you can add your own methods and fields to enum. For example, some getLabel ()

     <option value ="${location.name()}">${location.label}</option> 
    • Thank you, I accept your answer, especially since you answered it in my question, which was duplicated every other day, which was already more structured. I will add that what you found on the Internet works, and it can even be simplified. The only thing is that @RequestParam (required = false) CourseLocation location will not work when value = location.name (), the page will scream when loading that Course Course has no .name () method, but this is unnecessary. You can pass value = location. - Victor Morozov

    How the code for solving the problem has changed:

    I am not sending anything from the first servlet

      @RequestMapping("/registration") public String registration(){ return "registration"; } 

    Enum pass directly to the page

      HEAD <%@ page import="enum" %> /HEAD 

    The cycle for the drop-down list on the page looks like this

      <select class="selectpicker" id ="loc" name ="loc"> <c:forEach items="<%=CourseLocation.values()%>" var="location"> <option value = "${location}">${location}</option> </c:forEach> </select> 

    In a servlet that accepts value, I catch

      @RequestParam(required = false) CourseLocation loc 

    where loc is the name = "" of the Select tag, since

    Select itself determines which option was selected on the page without additional manipulations. It takes value = "val" from the selected option.

    In order for Select to send a value to the server, we must give it the name = "n" parameter. On the server in the servlet just catch the "n" which stores the "val" of the selected item in the drop-down menu