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

  • [form:select] preselecting an option

    Hi,

    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:

    Code:
    @Controller
    @RequestMapping("/cardForm.htm")
    @SessionAttributes("card")
    public class CardFormController {
    
        @Autowired
        private CardService cardService;
    
        @InitBinder
        public void initBinder(WebDataBinder binder) {
            binder.registerCustomEditor(CardColour.class, new CardColourEditor(cardService));
            binder.registerCustomEditor(CardRarity.class, new CardRarityEditor(cardService));
        }
    
        @ModelAttribute("cardColours")
        public List<CardColour> populateCardColours() {
            return cardService.getAllCardColours();
        }
    
        @ModelAttribute("cardRarities")
        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) {
    
            cardService.change(card);
            status.setComplete();
            return "redirect:card/list.htm";
        }
    }
    CardColourEditor:

    Code:
    public class CardColourEditor extends PropertyEditorSupport {
        private CardService cardService;
    
        public CardColourEditor(CardService cardService) {
            this.cardService = cardService;
        }
    
        @Override
        public void setAsText(String text) throws IllegalArgumentException {
    
            if (StringUtils.isEmpty(text)) 
                return;
    
            int cardColourId = Integer.parseInt(text);
            CardColour cardColour = cardService.getCardColour(cardColourId);
            setValue(cardColour);
        }
    
    }
    Jsp-page:

    Code:
    <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
    <html>
    <head>
        
        <style>
            .error {
                color: #ff0000;
                font-weight: bold;
            }
        </style>
    </head>
    <body>
    <form:form method="POST" commandName="card">
        <form:errors path="*" cssClass="error"/>
        <table>
            <tr>
                <td>Name</td>
                <td><form:input path="name"/></td>
                <td><form:errors path="name" cssClass="error"/></td>
            </tr>
            <tr>
                <td>Colour</td>
                <td>
                    <form:select path="cardColour" >
                        <form:option value="" />
                        <form:options items="${cardColours}" itemLabel="externalIdentifier" itemValue="cardColourId" />
                    </form:select>
                </td>
                <td><form:errors path="cardColour" cssClass="error"/></td>
            </tr>
            <tr>
                <td>Rarity</td>
                <td>
                    <form:select path="cardRarity" >
                        <form:option value="" />
                        <form:options items="${cardRarities}" itemLabel="externalIdentifier" itemValue="cardRarityId" />
                    </form:select>
                </td>
                <td><form:errors path="cardRarity" cssClass="error"/></td>
            </tr>
    
            <tr>
                <td colspan="3"><input type="submit"/></td>
            </tr>
        </table>
    </form:form>
    </body>
    </html>

    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:
    http://forum.springsource.org/showthread.php?t=35719

    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.

    Comment


    • #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.

      Comment

      Working...
      X