7

I am currently getting myself into Spring-Roo and Spring-MVC. I have a fairly simple application that Roo generated for me. It consists of two entities, Record and Car, where Record has a reference to one particlar car.

After the initial setup I change one of the views to use field:select and display a combobox for selecting available cars and add them to a record.

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

This tag gives me a headache. As by now, the comboxbox displays all available cars...but it does so by displaying all attributes ( like "Car 1 Tue Jan 18 00:00:00 CET 2011 Friver1"). All I want is that the combobox only shows the name-attribute ("Car 1").

Within the tag, there is only the "itemValue"-Attribute but this only renders the value that is put into the request-param...I need something like "displayValue" where I can point to the java-field that is used to display.

How can I achieve this? Thanks

lostiniceland
  • 3,729
  • 6
  • 38
  • 50

4 Answers4

11

:) Just spent the whole Sunday struggling out of the same problem. Simply add itemLabel="your field name from Car class".

<field:select field="car" 
              id="c_de_recordcars_domain_Record_car" 
              items="${cars}" 
            **itemLabel="CarName"**
              itemValue="id"
              path="/cars" />
Mailis Toompuu
  • 1,709
  • 15
  • 10
  • 1
    This seems a lot cleaner than messing around with the converters. You can also create your own getter method if you want to return something more complex than a single field. – Andy Cochrane May 11 '12 at 18:20
  • In this way you can just post one label that for one to many is not sufficient – Ali.Mojtahed May 26 '15 at 06:35
8

Spring Roo (using Spring MVC functionality) offers use Application Conversion Service. You should implement method Converter<Car, String> getCarConverter() inside the ApplicationConversionServiceFactoryBean.

See reference for details.

viator
  • 1,413
  • 3
  • 14
  • 25
2

For Spring roo 1.1.4 and above:

  1. Read ApplicationConversionServiceFactoryBean.java carefully

  2. Read ApplicationConversionServiceFactoryBean_Roo_ConversionService.aj carefully You should find a static inner class CarConverter here. It should have a very long prefix. You should find a installLabelConverters method here, with a long prefix.

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

  4. Copy the related import statements from 2 to 1.

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

  6. Now save the file 1.

  7. Start roo, let it update the .aj file.

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

Alex Wen
  • 21
  • 2
0

you can try to add toString method in entity Car,in which return Car's name field. and verify this profile in path:/src/main/webapp/WEB-INF/tags/form/fields/select.tagx update all option content:

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

to:

<option value="${item}">
    ${item}
</option>
GDP
  • 8,109
  • 6
  • 45
  • 82