开发者

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 &开发者_StackOverflowlt;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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜