1

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
  • There are no modelAttribute Objects in your last two methods. If you annotate a method with @ModelAttribute, it will be invoked before resolving the @ModelAttribute annotated method parameters. Commented Jul 9, 2010 at 6:28
  • Hi, pardon my understanding according to what you have said as I cannot get it clearly. I somewhat read from a link somewhere that if you annotate a method with the @ModelAttribute, this will look like the referencedata workflow of the simpleformcontroller? I didnt really use it as @ModelAttribute method parameter. Thanks. Commented Jul 9, 2010 at 6:33

2 Answers 2

6

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!

Sign up to request clarification or add additional context in comments.

1 Comment

hey it works like a charm.. I am beginning to like this annotation style of configuration.. Thanks Thanks..
1

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

1 Comment

. Thank you. I am beginning to like this site.. Hope you would always be of help to newbie like me..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.