Announcement Announcement Module
No announcement yet.
[form:select] preselecting an option Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • [form:select] preselecting an option


    I've got a simple form controller in which I can edit the Card entity. The Card entity references two other entities CardColour and CardRarity.

    On the jsp-page I want the CardColour of the Card I'm editing to be pre-selected when I enter the page. This is not working (for either CardColour or CardRarity).

    Extra info:
    - I'm using Spring 2.5.6 and Hibernate 3.5.2.
    - Selecting a CardColour from the dropdownlist and saving it, is working fine.
    - This thread is not a solution, because the CardColour object is already available in the Card-object when I enter the jsp-page.

    Following are code listings, first the CardFormController:

    public class CardFormController {
        private CardService cardService;
        public void initBinder(WebDataBinder binder) {
            binder.registerCustomEditor(CardColour.class, new CardColourEditor(cardService));
            binder.registerCustomEditor(CardRarity.class, new CardRarityEditor(cardService));
        public List<CardColour> populateCardColours() {
            return cardService.getAllCardColours();
        public List<CardRarity> populateCardRarities() {
            return cardService.getAllCardRarities();
        @RequestMapping(method = RequestMethod.GET)
        public String setupForm(
                @RequestParam(required = false, value = "cardName") String cardName,
                ModelMap model) {
            Card card = cardService.query(cardName);
            model.addAttribute("card", card);      
            return "cardForm";
        @RequestMapping(method = RequestMethod.POST)
        public String processSubmit(
                @ModelAttribute("card") Card card,
                SessionStatus status) {
            return "redirect:card/list.htm";

    public class CardColourEditor extends PropertyEditorSupport {
        private CardService cardService;
        public CardColourEditor(CardService cardService) {
            this.cardService = cardService;
        public void setAsText(String text) throws IllegalArgumentException {
            if (StringUtils.isEmpty(text)) 
            int cardColourId = Integer.parseInt(text);
            CardColour cardColour = cardService.getCardColour(cardColourId);

    <%@ taglib prefix="form" uri="" %>
            .error {
                color: #ff0000;
                font-weight: bold;
    <form:form method="POST" commandName="card">
        <form:errors path="*" cssClass="error"/>
                <td><form:input path="name"/></td>
                <td><form:errors path="name" cssClass="error"/></td>
                    <form:select path="cardColour" >
                        <form:option value="" />
                        <form:options items="${cardColours}" itemLabel="externalIdentifier" itemValue="cardColourId" />
                <td><form:errors path="cardColour" cssClass="error"/></td>
                    <form:select path="cardRarity" >
                        <form:option value="" />
                        <form:options items="${cardRarities}" itemLabel="externalIdentifier" itemValue="cardRarityId" />
                <td><form:errors path="cardRarity" cssClass="error"/></td>
                <td colspan="3"><input type="submit"/></td>

    Can anybody help me get this working? If you need any more information, please let me know.

  • #2
    I've searched some more and I found the following thread:

    This seems to be the solution to my problem. I've not implemented an equals-method on the CardColour or CardRarity classes, so I'll try this tonight.

    I'll post my results in this thread for future reference.


    • #3
      Sure enough, adding an equals() method to CardColour and CardRarity was the solution.

      The equals() method must compare on the same field as the 'itemValue'-attribute of the <form:select>-tag.