pass the selected value from dropdown list

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

pass the selected value from dropdown list

cemal94
Hi i am new to thymeleaf,

So i want to pass the selected value from the dropdown list to the my controller with seatings id. Im not sure how to use the th:action and th:selected.


<form th:action="@{/shoppingCart/addItem}" method="post">
        <div class="row" style="margin-top: 120px;">
            <div class="col-xs-3">
                Back to movie list<br/>
            </div>

            <div class="col-xs-9">
                <div class="row">
                    <div class="col-xs-7">
                        <div class="panel panel-default" style="border-width: 5px; margin-top: 20px;">
                            <div class="panel-body">
                                <div class="row">
                                    <div class="col-xs-6">
                                        <p><strong>Movie: </strong></span</p>
                                        <p><strong>Seans: </strong></p>
                                        <p><strong>Saat: </strong></p>
                                        <p><strong>Salon: </strong></p>
                                    </div>
                                    <div class="col-xs-6">
                                        Koltuk
                                        <select name="Seating">
                                            <option th:each="seating : ${seatingList}" th:value="${seating.id}" th:text="${seating.seatNo}" th:selected="${seating.id}">
                                            </option>
                                        </select>

                                        <button type="submit" class="btn btn-warning" style="color:black;border:1px solid black; padding: 10px 40px 10px 40px;">Ekle</button>

                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>

                <hr/>

            </div>
        </div>

    </form>
Reply | Threaded
Open this post in threaded view
|

Re: pass the selected value from dropdown list

Reiju
1. U need pass some object with field "Seating.class"
public class Item {
    private Seating seating;
    //getters, setters
}

2. Controller
@GetMapping("/shoppingCart/addItem")
public String addItemForm(Model model) {
        model.addAttribute("item", new Item());
        model.addAttribute("seatingList", seatingService.getSeatingList());
        return "form";
}

@PostMapping("/shoppingCart/addItem")
public String addItem(@ModelAttribute("item") Item item) {
        //do something
}

3. In form tag use th:object and th:field
<form method="post" th:object="${item}" th:action="${'/shoppingCart/addItem'}">
        <select th:field="*{seating}">
            <option th:each="o : ${seatingList}"
                    th:value="${o.id}"
                    th:text="${o.seatNo}">
            </option>
        </select>
</form>

PS: you don't need to assign the name or selected attributes yourself. Thymeleaf will do everything for you
Reply | Threaded
Open this post in threaded view
|

Re: pass the selected value from dropdown list

cemal94
@Entity
public class ShowTime {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @ManyToOne
    @JoinColumn(name="movie_id")
    private Movie movie;

    @ManyToOne
    @JoinColumn(name="saloon_id")
    private Saloon saloon;

    @Temporal(TemporalType.DATE)
    private Date date;

    @Temporal(TemporalType.TIME)
    private Date time;

    @OneToMany(cascade=CascadeType.ALL,mappedBy = "showTime")
    @JsonIgnore
    private List<Seating> SeatingList;

//getters setters
}

@Entity
public class Seating {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @ManyToOne
    @JoinColumn(name="showtime_id")
    private ShowTime showTime;

    private int seatNo;

    private boolean isTaken=false;
//getter setters
}

so these are my entities showTime have multiple seats. I need the bind the selected value from the dropdown list on action either with the object or id to the controller. I changed my controller

@RequestMapping("/addItem")
        public String addItem(
                        @ModelAttribute("seating") Seating seating,
                        Model model, Principal principal
                        ) {
                User user = userService.findByUsername(principal.getName());
                System.out.println(seating.getSeatNo()); // to see i can get the choosen seating from the dropdown or not
}

<form th:action="@{/shoppingCart/addItem}" th:object="${seating}" method="post">

select name="Seating">
                                            <option th:each="seating : ${seatingList}" th:value="${seating.id}" th:text="${seating.seatNo}" th:selected="${seating.id}">
                                            </option>
                                        </select>

                                        <button type="submit" class="btn btn-warning" style="color:black;border:1px solid black; padding: 10px 40px 10px 40px;">Ekle</button>



so i cant figure out how can i do it th:field is not working for me. Pls help
Reply | Threaded
Open this post in threaded view
|

Re: pass the selected value from dropdown list

Reiju
1. Controller: pass ShowTime object and seatingList. In fact you have 2 similar lists: the first one will be used as th:field, the second as options

@GetMapping("/addItem")
public String addItemForm(Model model) {
        model.addAttribute("showTime", new ShowTime());
        model.addAttribute("seatingList", seatingService.getSeatingList());
        return "form";
}

@PostMapping("/addItem")
public String addItem(@ModelAttribute("showTime") ShowTime showTime) {
        showTime.getSeatingList().forEach(s -> System.out.println(s.getSeatNo()));
        return "redirect:/addItem";
}

2. HTML. Pay attention to symbol * in th:field
<form method="post" th:action="${'/addItem'}" th:object="${showTime}">
   
    <select th:field="*{seatingList}">
        <option th:each="o : ${seatingList}"
                    th:value="${o.id}"
                    th:text="${o.seatNo}"></option>
    </select>

    <button type="submit">Add</button>
</form>

3. Converter [optional, if u have some exceptions with conversion string data to java objects]
@Component
public class StringToSeatingConverter implements Converter<String, Seating> {

    @Autowired
    private SeatingService seatingService;

    @Override
    public Seating convert(String s) {
        //NumberFormatException!!!
        return seatingService.getById(Long.valueOf(s));
    }
}