user237865
user237865

Reputation: 1250

Spring : timely/late binding of @ModelAttribute

I'm using the following code to bind the users to the model [to be used in the view/jsp]:

@ModelAttribute("users")
public Collection<User> populateUsers() {
    return userService.findAllUsers();
}

But sometimes I just need to load few users with a particular Role, which I'm trying by using the following code:

int role = 2; //this is being set in a Controller within a method @RequestMapping(method = RequestMethod.GET) public String list(
@ModelAttribute("users")
public Collection<User> populateUsers() {
    if(role == 2)
        return userService.findAllUsersByRole(role);
    else
        return userService.findAllUsers();
}

but the populateUsers is always called at the start of the controller, before the role is set in list method, Could you please help me on how to set the users [something like late binding]

Regards

-- adding code

@Controller
@RequestMapping("/users")
public class UserController {

@Autowired
UserService userService;

@RequestMapping(method = RequestMethod.POST)
public String create(@Valid User user, BindingResult bindingResult,
        Model uiModel, HttpServletRequest httpServletRequest) {
    if (bindingResult.hasErrors()) {
        uiModel.addAttribute("user", user);
        addDateTimeFormatPatterns(uiModel);
        return "users/create";
    }
    uiModel.asMap().clear();
    userService.saveUser(user);
    return "redirect:/users/"
            + encodeUrlPathSegment(user.getId().toString(),
                    httpServletRequest);
}

@RequestMapping(params = "form", method = RequestMethod.GET)
public String createForm(Model uiModel) {
    uiModel.addAttribute("user", new User());
    addDateTimeFormatPatterns(uiModel);
    return "users/create";
}

@RequestMapping(value = "/{id}", method = RequestMethod.GET)
public String show(@PathVariable("id") Long id, Model uiModel) {
    addDateTimeFormatPatterns(uiModel);
    uiModel.addAttribute("user", userService.findUser(id));
    return "users/show";
}

@RequestMapping(value = "/{id}", params = "form", method = RequestMethod.GET)
public String updateForm(@PathVariable("id") Long id, Model uiModel) {
    uiModel.addAttribute("user", userService.findUser(id));
    addDateTimeFormatPatterns(uiModel);
    return "users/update";
}

@ModelAttribute("users")
public Collection<User> populateUsers() {
    return userService.findAllUsers();
}

@ModelAttribute("userroles")
public Collection<UserRole> populateUserRoles() {
    return Arrays.asList(UserRole.class.getEnumConstants());
}

void addDateTimeFormatPatterns(Model uiModel) {
    uiModel.addAttribute(
            "user_modified_date_format",
            DateTimeFormat.patternForStyle("M-",
                    LocaleContextHolder.getLocale()));
}

}

@PathVariable("id") Long id is the ID I require in populateUsers, hope it is clear.

Upvotes: 2

Views: 553

Answers (2)

user237865
user237865

Reputation: 1250

Setting the model attribute in the required method has solved my issue:

model.addAttribute("users", return userService.findAllUsersByRole(role));

Thanks!

Upvotes: 0

Jhonathan
Jhonathan

Reputation: 1601

If role is in the current request, this method binding role to variable role.

        @ModelAttribute("users")
        public Collection<User> populateUsers(@RequestParam(required=false) Integer role) {
            if(role != null && role == 2)
                return userService.findAllUsersByRole(role);
            else
                return userService.findAllUsers();
        }

Upvotes: 1

Related Questions