Back Forum Reply New

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(quot;/cardForm.htmquot;)
@SessionAttributes(quot;cardquot;)
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(quot;cardColoursquot;)   public Listlt;CardColourgt; populateCardColours() {       return cardService.getAllCardColours();   }
   @ModelAttribute(quot;cardRaritiesquot;)   public Listlt;CardRaritygt; populateCardRarities() {       return cardService.getAllCardRarities();   }
   @RequestMapping(method = RequestMethod.GET)   public String setupForm(@RequestParam(required = false, value = quot;cardNamequot;) String cardName,ModelMap model) {       Card card = cardService.query(cardName);       model.addAttribute(quot;cardquot;, card);             return quot;cardFormquot;;   }
   @RequestMapping(method = RequestMethod.POST)   public String processSubmit(@ModelAttribute(quot;cardquot;) Card card,SessionStatus status) {
       cardService.change(card);       status.setComplete();       return quot;redirect:card/list.htmquot;;   }
}
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:
lt;%@ taglib prefix=quot;formquot; uri=quot;tags/formquot; %gt;
lt;htmlgt;
lt;headgt;      lt;stylegt;       .error {color: #ff0000;font-weight: bold;       }   lt;/stylegt;
lt;/headgt;
lt;bodygt;
lt;form:form method=quotOSTquot; commandName=quot;cardquot;gt;   lt;form:errors path=quot;*quot; cssClass=quot;errorquot;/gt;   lt;tablegt;       lt;trgt;lt;tdgt;Namelt;/tdgt;lt;tdgt;lt;form:input path=quot;namequot;/gt;lt;/tdgt;lt;tdgt;lt;form:errors path=quot;namequot; cssClass=quot;errorquot;/gt;lt;/tdgt;       lt;/trgt;       lt;trgt;lt;tdgt;Colouclt;/tdgt;lt;tdgt;    lt;form:select path=quot;cardColourquot; gt;        lt;formption value=quot;quot; /gt;        lt;formptions items=quot;${cardColours}quot; itemLabel=quot;externalIdentifierquot; itemValue=quot;cardColourIdquot; /gt;    lt;/form:selectgt;lt;/tdgt;lt;tdgt;lt;form:errors path=quot;cardColourquot; cssClass=quot;errorquot;/gt;lt;/tdgt;       lt;/trgt;       lt;trgt;lt;tdgt;Raritylt;/tdgt;lt;tdgt;    lt;form:select path=quot;cardRarityquot; gt;        lt;formption value=quot;quot; /gt;        lt;formptions items=quot;${cardRarities}quot; itemLabel=quot;externalIdentifierquot; itemValue=quot;cardRarityIdquot; /gt;    lt;/form:selectgt;lt;/tdgt;lt;tdgt;lt;form:errors path=quot;cardRarityquot; cssClass=quot;errorquot;/gt;lt;/tdgt;       lt;/trgt;
       lt;trgt;lt;td colspan=quot;3quot;gt;lt;input type=quot;submitquot;/gt;lt;/tdgt;       lt;/trgt;   lt;/tablegt;
lt;/form:formgt;
lt;/bodygt;
lt;/htmlgt;

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

I've searched some more and I found the following thread:
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.

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 lt;form:selectgt;-tag.
¥
Back Forum Reply New