tags:

views:

63

answers:

2

Hi,

I am in the middle of converting my controllers to annotated style controllers in spring mvc.

Basically I do this in the old style controller simpleformcontroller.

protected Map referenceData(HttpServletRequest request) throws Exception
{
    Map referenceData = new HashMap();

    List<ItemVo> lstItem1 = eqrManager
        .searchAllEqptCondQualItems("A1", "BOXES");
    List<ItemVo> lstItem2 = eqrManager
        .searchAllEqptFullQualItems("A2", "CANNED_GOODS");
    referenceData.put("BOX_ITEMS", lstItem1);
    referenceData.put("CANNED_ITEMS", lstItem2);
    return referenceData;
}

In the annotated, I do something like this:

@ModelAttribute("BOX_ITEMS")
public List<ItemVo> populateCondEQRItems() {
    List<ItemVo> lstCondQual = eqrManager
            .searchAllEqptCondQualItems("A1", "BOXES");
    return lstCondQual;
}

@ModelAttribute("CANNED_ITEMS")
public List<ItemVo> populateFullEQRItems() {
    List<ItemVo> lstFullQual = eqrManager
            .searchAllEqptFullQualItems("A2", "CANNED_GOODS");
    return lstFullQual;
}

My question is, is there a way to return all attributes in just a single method and not having to create multiple @ModelAttribute? In my case, I need to annotate 2 method? What if I need 3, should I create 3 annotated methods also?

+2  A: 

The rule is clear

If you need more than one model attribute, take model as a input argument

@RequestMapping(method=RequestMethod.GET)
public void setUp(Model model) {
    model.addAttribute("CANNED_ITEMS", eqrManager.searchAllEqptFullQualItems("A2", "CANNED_GOODS"))
         .addAttribute("BOX_ITEMS", eqrManager.searchAllEqptCondQualItems("A1", "BOXES"));
}

Good lucky!

Arthur Ronald F D Garcia
@Arthur, hey it works like a charm.. I am beginning to like this annotation style of configuration.. Thanks Thanks..
Mark Estrada
+1  A: 

I cannot get it clearly

Ok! I was telling that @ModelAttribute can be put at Method level as well as Method Parameter level. And it behaves differently depends on where you've put it.

    @ModelAttribute(user)
public void preRender(Model model) {
        /* this method will be invoked before resolving @ModelAttribute Method Parameter i.e. before invoking render/processCreate method */
       /* codes are available to CreateUser.jsp if render request comes */
       /* codes are available to CreateUser.jsp if validation fails */
        model.addAttribute("countryCodes", I18Nservice.getCountryISOCodes());
        model.addAttribute("languageCodes", I18Nservice.getLanguageISOCodes());
}

public String renderCreate(@ModelAttribute(value="user") User user) {
    return "/user/create";
}

@Override
public String processCreate(@ModelAttribute(value="user") User user, BindingResult result) {
           if(result.hasErrors() {
             return "/user/create";
           }
            securityService.createUser(user);
            return "/user/detail/user.getId()";
}

If you are new in Spring MVC 3 arena:

  1. read Web MVC framework
  2. Check @RequestMapping JavaDoc
  3. And play with Petcinic & mvc-showcase
becomputer06
@becomputer06..Thank you. I am beginning to like this site.. Hope you would always be of help to newbie like me..
Mark Estrada