How to display an particlar attribut in a selectbox in a ROO-generated application
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 r开发者_开发问答equest-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
:) 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" />
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.
For Spring roo 1.1.4 and above:
Read ApplicationConversionServiceFactoryBean.java carefully
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.
Copy the code CarConverter from 2 to 1, remove the long prefix. Change the code inside convert() method, as what you like.
Copy the related import statements from 2 to 1.
Copy the code installLabelConverters method from 2 to 1, remove the long prefix.
Now save the file 1.
Start roo, let it update the .aj file.
Use "mvn tomcat:run" to compile and run it again.
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.tag
x
update all option content:
<option value="${item}">
<spring:eval expression="item" />
</option>
to:
<option value="${item}">
${item}
</option>
精彩评论