How to display member attribute in selectbox in ROO-generated application

I am currently getting into Spring-Roo and Spring-MVC. I have a pretty simple app that Roo created for me. It consists of two objects: Record and Car, where Record has a link to one car.

After the initial setup, I change one of the views for using the field: select and display the combo box to select available cars and add them to the entry.

<field:select field="car" id="c_de_recordcars_domain_Record_car" items="${cars}" path="/cars" /> 

This tag gives me a headache. Since the comboxbox currently displays all available cars ... but it does it by displaying all attributes (for example, "Car 1 Tue Jan 18 00:00:00 CET 2011 Friver1"). All I want is that the name attribute ("Car 1") is displayed in the combo box.

Inside the tag there is only the attribute "itemValue" -Attribute, but this only displays the value that is placed in the request parameter ... I need something like "displayValue", where I can point to the java field that is used for display.

How can i achieve this? Thanks

+7
source share
4 answers

Spring Roo (using Spring MVC functionality) suggests using an application conversion service. You must implement the Converter<Car, String> getCarConverter() method inside ApplicationConversionServiceFactoryBean .

See the link for more details.

+8
source

:) Just spent all Sunday fighting the same problem. Just add itemLabel = "the name of your field from the Car class."

 <field:select field="car" id="c_de_recordcars_domain_Record_car" items="${cars}" **itemLabel="CarName"** itemValue="id" path="/cars" /> 
+11
source

For Spring roo 1.1.4 and higher:

  • Read the ApplicationConversionServiceFactoryBean.java carefully

  • Read the ApplicationConversionServiceFactoryBean_Roo_ConversionService.aj application carefully. Here you should find the static CarConverter inner class. It must have a very long prefix. Here you should find the installLabelConverters method with a long prefix.

  • Copy the CarConverter code from 2 to 1, remove the long prefix. Change the code inside the convert () method as you like.

  • Copy the corresponding import statements from 2 to 1.

  • Copy the installLabelConverters code from 2 to 1, remove the long prefix.

  • Now save the file 1.

  • Run roo, let it update the .aj file.

  • Use "mvn tomcat: run" to compile and run it again.

+2
source

you can try adding the toString method to entity Car , in which the Car name field will be returned. and check this profile in path:/src/main/webapp/WEB-INF/tags/form/fields/select.tag x update all option contents:

 <option value="${item}"> <spring:eval expression="item" /> </option> 

in

 <option value="${item}"> ${item} </option> 
0
source

All Articles