JAVA Basic 강의자료] 컨트롤러 작성

 

JAVA Basic 강의자료] 컨트롤러 작성

 

 

 

 

실무개발자를위한 실무교육 전문교육센터학원
www.oraclejava.co.kr에 오시면 보다 다양한 강좌를 보실 수 있습니다.

 

 

컨트롤러 작성(1)

 

 

<소스코드>

 

@Controller
@RequestMapping(value="/test/customizeDateBind")

public class CustomizeDateBindController {    
    @InitBinder
    protected void initBinder(WebDataBinder binder) {
        //데이터 바인드를 체크하기 위해 SimpleDateFormat를 생성
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
        dateFormat.setLenient(false);
 
        // 형을 지정한 바이드 설정
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
        // 프러퍼티를 지정한 바인드 설정
        binder.registerCustomEditor(Date.class, "endDate", new CustomDateEditor(dateFormat, true, 10));
    }
     // 더이터 바인드시 에러 처리위해(반드시 필요!)
    @ModelAttribute("command")
    public CustomizeDateBindCommand createInitCommand() {
        CustomizeBindCommand command = new CustomizeDateBindCommand();
        return command;
    }

 

컨트롤러 작성(2)

 

 

<소스코드>

 

 

@RequestMapping(method=RequestMethod.GET)
    public void setupForm(Model model) {
        CustomizeDateBindCommand command = createInitCommand();
        // 시작일자 초기값은 현재일자
        command.setStartDate(new Date());        
        model.addAttribute("command", command);
    }
   
    @RequestMapping(method=RequestMethod.POST)
    public ModelAndView doAction1(@ModelAttribute("command") CustomizeDateBindCommand command,
            BindingResult bindingResult) {
       
        System.out.println(command.toString());
       
        // 바인드 에러 처리
        if(bindingResult.hasErrors()) {
            ModelAndView mav = new ModelAndView();
            mav.getModel().putAll(bindingResult.getModel());
            return mav;
        }
       
        ModelAndView mav = new ModelAndView("forward:/hello.html");
        return mav;
    }

+ Recent posts