简体   繁体   中英

Spring MVC JSTL preselected list

I have 2 objects: Car and Person

Person has the following attributes: int id, String name, List<Car> cars Car has the following variables:

int id, String make

I am trying to create an update view for a person.

I pass the person object and the a model attribute list containing all cars

person.cars = {"BMW","MERCEDES"}

allCars = {"BMW","TOYOTA","HONDA","MERCEDES","KIA","HYUNDAI"}

When I use <form:select/> with <form:options items=${allCars}.. /> I see all the cars..

I was wondering if there is a way through spring without writing a lot of JSTL loops to preselect the cars that a person has..

Thanks

In this specific case, they key solution was to override the equals method of the Car class.

This way spring can know that the cars that we got fromt he database, equal the cars that the user has

Thanks

I guess you must be using Checkboxes for multiple selection of cars so you can try this.

<form:checkboxes 
path="person.cars" 
items="${allcars}" 
itemLabel="make" 
itemValue="id"/>

I think this would suit your requirement.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM