Request Mapping
- GetMapping/PostMapping으로 처리할 method를 지정하거나 RequestMapping을 사용할 수 있다.
Access request parameter : HttpServletRequest
1@PostMapping("/register/step2")
2public String handleStep2(HttpServletRequest request) {
3 String agreeParam = request.getParameter("agree");
4 if (agreeParam == null || !agreeParam.equals("true")) {
5 return "register/step1";
6 }
7 return "register/step2";
Access request parameter : RequestParam Annotation
1@PostMapping("/register/step2")
2public String handleStep2(
3@RequestParam(value="agree", defaultValue="false") Boolean agree) {
4 if (!agree) {
5 return "register/step1";
6 }
7 return "register/step2";
Redirect
1@GetMapping("/register/step2")
2public String handleStep2Get() {
3 return "redirect:/register/step1";
Command Object
Use request parameter as command object
- RegisterController.java
1@PostMapping("/register/step3")
2public String handleStep3(RegisterRequest regReq) {...}
- step3.jsp
1<p><strong>${registerRequest.name}님</strong></p>
UnsatisfiedDependencyException: Error creating bean with name ‘controllerConfig’: Unsatisfied dependency expressed through field ‘memberRegSvc’; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type ‘spring.MemberRegisterService’ available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
1<param-value>
2 config.MvcConfig
3
4 <!-- new!! -->
5 config.MemberConfig
6
7 config.ControllerConfig
8</param-value>
- web.xml에 MemberConfig 클래스가 빠져있어서 bean이 만들어지지 않아 발생한 오류이다
1<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
2<!-- ... -->
3<form:form action="step3" modelAttribute="registerRequest">
4 <!-- ... -->
5 <form:input path="email"
6/></form:form>
- Spring MVC에서 지원하는 Custom Tag를 이용해 Command 객체와 간단하게 연동할 수 있다.
jsp로 for문 사용하기
1<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
2<!-- ... -->
3<c:forEach var="response" items="${ansData.responses}" varStatus="status">
4 <li>${status.index + 1}번 문항: ${response}</li>
5</c:forEach>
jsp로 데이터 보내기
1@GetMapping
2public String form(Model model) {
3 List<Question> questions = createQuestions();
4 model.addAttribute("questions", questions);
5 return "survey/surveyForm";
6}
1<c:forEach var="q" items="${questions}" varStatus="status"></c:forEach>
ModelAndView 활용
1@GetMapping
2public ModelAndView form(Model model) {
3 List<Question> questions = createQuestions();
4 ModelAndView mav = new ModelAndView();
5 mav.addObject("questions", questions);
6 mav.setViewName("survey/surveyForm");
7 return mav;
8}
- 데이터 설정, 뷰이름 리턴 두가지를 한번에 처리할 수 있다