애노테이션 기반 MVC로 코드를 작성해보자.
클래스 위에 @Controller를 붙이면 컴포넌트로 스캔한다.
클래스 위에 @RequestMapping("url정보") 를 붙이면 해당 url이 들어오면 그 함수가 실행된다!
RequestMappingHandlerMapping으로 핸들러 매핑하고
RequestMappingHandlerAdapter로 핸들러 어댑터.
Copy package hello . servlet . web . springmvc . v1 ;
import org . springframework . stereotype . Component ;
import org . springframework . stereotype . Controller ;
import org . springframework . web . bind . annotation . RequestMapping ;
import org . springframework . web . servlet . ModelAndView ;
@ Controller
public class SpringMemberFormControllerV1 {
@ RequestMapping ( "/springmvc/v1/members/new-form" )
public ModelAndView process (){
return new ModelAndView( "new-form" ) ;
}
}
해당하는 url이 들어오면 process() 메서드 실행된다.(논리 이름 new-form을 반환)
뷰리졸버는 우선순위에 따라 이 new-form을 처리한다!(이전 페이지 뷰리졸버 참고)
Copy package hello . servlet . web . springmvc . v1 ;
import org . springframework . stereotype . Component ;
import org . springframework . stereotype . Controller ;
import org . springframework . web . bind . annotation . RequestMapping ;
import org . springframework . web . servlet . ModelAndView ;
@ Component //컴포넌트 스캔을 통해 스프링 빈으로 등록
@ RequestMapping
public class SpringMemberFormControllerV1 {
@ RequestMapping ( "/springmvc/v1/members/new-form" )
public ModelAndView process (){
return new ModelAndView( "new-form" ) ;
}
}
회원 저장 정보 조회
ModelView=>ModelAndView
mv.getModel().put("member",member) => ModelAndView.addObject("member",member)
Copy package hello . servlet . web . springmvc . v1 ;
import hello . servlet . domain . member . Member ;
import hello . servlet . domain . member . MemberRepository ;
import org . springframework . stereotype . Controller ;
import org . springframework . web . bind . annotation . RequestMapping ;
import org . springframework . web . servlet . ModelAndView ;
import javax . servlet . http . HttpServletRequest ;
import javax . servlet . http . HttpServletResponse ;
import java . util . Map ;
@ Controller
public class SpringMemberSaveControllerV1 {
private MemberRepository memberRepository = MemberRepository . getInstance ();
@ RequestMapping ( "/springmvc/v1/members/save" )
public ModelAndView process ( HttpServletRequest req , HttpServletResponse res) {
String username = req . getParameter ( "username" );
int age = Integer . parseInt ( req . getParameter ( "age" ));
Member member = new Member(username , age) ;
memberRepository . save (member);
ModelAndView mv = new ModelAndView( "save-result" ) ;
//mv.getModel().put("member",member);
mv . addObject ( "member" , member);
return mv;
}
}
Copy package hello . servlet . web . springmvc . v1 ;
import hello . servlet . domain . member . Member ;
import hello . servlet . domain . member . MemberRepository ;
import org . springframework . stereotype . Controller ;
import org . springframework . web . bind . annotation . RequestMapping ;
import org . springframework . web . servlet . ModelAndView ;
import java . util . List ;
import java . util . Map ;
@ Controller
public class SpringMemberListControllerV1 {
private MemberRepository memberRepository = MemberRepository . getInstance ();
@ RequestMapping ( "/springmvc/v1/members" )
public ModelAndView process () {
List < Member > members = memberRepository . findAll ();
ModelAndView mv = new ModelAndView( "members" ) ;
mv . addObject ( "members" , members);
return mv;
}
}