Pre-load Data
Pre loaded data the data that is displayed at View HTML Forms irrespective of any error.
All drop-down lists on html forms are pre-loaded data.
Annotation @ModelAttribute can be used on a method of controller. When @ModelAttribute is applied on a method of controller then this method will be invoked before any method mapped with @RequestMapping annotation.
Here is example of UserCtl that pre-loads role list, displayed at User Form while added a User.
@Controller
@RequestMapping(value = "/User")
@SessionAttributes("userContext")
public class UserCtl {
@ModelAttribute
public void prepare(@RequestParam(required = false) String operation,Model model) {
System.out.println("Loding preloaded data Role List");
List<RoleDTO> l = new ArrayList<RoleDTO>();
RoleDTO admin = new RoleDTO();
admin.setId(1);
admin.setName("Admin");
l.add(admin);
RoleDTO member = new RoleDTO();
admin.setId(2);
admin.setName("Member");
l.add(member);
RoleDTO guest = new RoleDTO();
admin.setId(3);
admin.setName("Guest");
l.add(guest);
System.out.println("Size or Role List " + l.size());
model.addAttribute("roleList", l);
}
..
Q: When is preload method called?
A: When @ModelAttribute is applied on a method of controller then this method will be invoked before any method mapped with @RequestMapping annotation.
Q: How do you load pre-load data in your application?
A: We apply @ModelAttribue annotation on a method called preload(). This method is called before any method mapped with @RequestMappting annotation.