开发者

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:

  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.


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>
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜