2016년 5월 9일 월요일

[spring] difference between @ModelAttribute method and parameter @modelattribute 차이점

@Controller@RequestMapping("/admin/survey")
public class SurveyManageController {

    @Autowired    private SurveyService surveyService;
    @Autowired    private StoreService storeService;
    @ModelAttribute("hello")
     public SurveyOpen testShowModelAttribute() {

        SurveyOpen surveyOpen = new SurveyOpen();        surveyOpen.setStoreNum(100);        surveyOpen.setMenuNum(200);        surveyOpen.setName("테스트");
        return surveyOpen;
    }

    @RequestMapping(method = RequestMethod.GET)
    public String showSurveyList(Model model) {

        List<SurveyListVo> surveyList = surveyService.getOpenSurveyList();        model.addAttribute("surveyList", surveyList);        return "/admin/survey_list";    }

    @RequestMapping(value="/open", method=RequestMethod.GET)
    public String showRegisterForm(Model model) {

        List<Store> storeList = storeService.getStoreList();        model.addAttribute("storeList", storeList);        model.addAttribute("surveyForm", new SurveyOpen());        return"/admin/survey_submit_form";    }
  @RequestMapping(value="/open", method=RequestMethod.POST)
    public String registerSurvey(@ModelAttribute("surveyForm") SurveyOpen survey){
        surveyService.registerSurvey(survey);        return"/admin/survey_submit_form";//        return "redirect:/admin/survey";    }


@ModelAttribute("model")
public ObjectToSendViewPage methodName() {
             return new ObjectToSEndViewPage();

}


  • @ModelAttribute at Method: whenever controller is called, this method will be populated to deliver the object to view page. 
    • upper code, hello object (
      @ModelAttribute("hello")
      will be delivered to view all the time when showRegisterForm function or showSurveyList function is called. 
    •  access in view (jsp page)
      ${model.attributeName}
  • @ModelAttribute at Parameter: it only populated when the function containing this parameter is called.
     public SurveyOpen testShowModelAttribute()
    • On the other hand, surveyForm object will be delivered only when registerSurvey method is called
-------------------------------------------------------------------------------------------
  • 컨트롤러 안에 @ModelAttribute메서드
     public SurveyOpen testShowModelAttribute()는, 위 메서드를 포함하는 컨트롤러중 어느것이라도 호출되면 뷰에 전달
  • 파라미터로 전달된 @ModelAttribute,
    @ModelAttribute("surveyForm") SurveyOpen surveyOpen

    public String registerSurvey
    호출시에만 view에 전달됨

댓글 없음:

댓글 쓰기