1. 반환형
void | 요청 주소에 따라 viewName을 결정한다 |
String | 반환되는 값을 viewname으로 결정한다. |
ModelAndView | 생성자 매개변수 혹은 setViewName(String viewName)에 의해 viewName 결정 |
2. 매개변수
HttpServletRequest | JSP 내장 객체 request |
HttpServletResponse | JSP 내장 객체 response |
HttpSession | JSP 내장 객체 session |
pritimitive, String | request.getParameter에 대응된다 |
Model | request대신 attribute를 전달하는 가벼운 객체 |
커맨드 객체 | 임의의 클래스를 작성하여, 여러 파라미터를 객체로 처리할 수 있다. |
3. 어노테이션
@Controller | 클래스에 지정하여 스프링 빈으로 등록함과 동시에 컨트롤러의 역할을 수행한다. 요청 주소에 따라 실행할 함수를 작성하는 클래스이다. 요청에 따른 응답을 결정할 수 있다. |
@RequestMapping (요청에 대한 "if"문) |
클래스 혹은 메서드에 지정하여, 어떤 요청이 왔을 때 반응할지 결정한다. value와 method를 지정할 수 있으며, 다른 세부 속성도 있다. |
@RequestParam | request.getParameter(), 보통 매개변수 앞에서 생략된다 (@request.getParameter("~~~") String ~~~) String 앞이 생략 단, 파라미터를 Map으로 처리하는 경우 반드시 명시해야 한다. |
@ModelAttribute | model.addAttribute(), 파라미터를 곧바로 model에 추가한다 자주 사용되는 편은 아니다. |
@RequestMapping으로 변환되어 실행되는 5개의 어노테이션(4.1.0 ↑)
코드가 컴파일되면 3열처럼 바뀌어 실행된다
@RequestMapping | @GetMapping | @RequestMapping(value="", method=RequestMethod.GET) |
@PostMapping | @RequestMapping(value="", method=RequestMethod.POST) | |
@PutMapping | @RequestMapping(value="", method=RequestMethod.PUT) | |
@PatchMapping | @RequestMapping(value="", method=RequestMethod.PATCH) | |
@DeleteMapping | @RequestMapping(value="", method=RequestMethod.DELETE) |
@RequestParam _ Map의 경우
앞의 포스팅에서 한가지 더 추가된 경우로 RequestParam은 보통 매개변수 앞에서 생략되나
Map으로 매개변수를 받을 경우 꼭 붙여줘야한다.
package com.itbank.controller;
import java.util.HashMap;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import com.itbank.model.Ex04DTO;
@Controller
@RequestMapping("/ex05")
public class Ex05Controller {
@GetMapping
public void ex05() {}
@PostMapping
public ModelAndView ex05(@RequestParam HashMap<String, String> param) { // RequestParam을 생략할 시 404 에러 발생
ModelAndView mav = new ModelAndView("ex05-result");
String name = param.get("name");
int age = Integer.parseInt(param.get("age"));
String adult = age >= 20 ? "성인" : "미성년자";
String msg = String.format("%s의 나이는 %d살이고, %s입니다", name, age, adult);
mav.addObject("msg", msg);
return mav;
}
}
@RequestParam을 꼭 적어줘야한다.
Map을 사용하기 때문에.
@ModelAttribute
효용가치는 높지 않으나, 알아두면 좋으니 포스팅 하겠다.
RequestParam으로 받은 값을 바로 model 파라미터로 보내는 작업이다.
package com.itbank.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.itbank.model.Ex04DTO;
@Controller
@RequestMapping("/ex04")
public class Ex04Controller {
@GetMapping
public void abcd() {}
@PostMapping
public ModelAndView ex04(@ModelAttribute("test") Ex04DTO user) { // 스프링 커맨드 객체 활용
ModelAndView mav = new ModelAndView("ex04-result");
return mav;
}
}
mav.addObject를 생략가능한 장점이 있으나,..... 보통의 경우 받은 값으로 데이터 후처리(데이터 값을 불러오거나 등등)를 해줘야 하기때문에 많이 쓰일지는 모르겠다.
jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ include file="header.jsp" %>
<h3>
${test.name }의 나이는
${test.age }살이고,
${test.age >= 20 ? '성인' : '미성년자' }입니다.
</h3>
</body>
</html>
'개발자 > Spring' 카테고리의 다른 글
[Spring] 간단한 구구단 게임 구현 (0) | 2022.12.27 |
---|---|
[Spring] 간단한 UpDown 게임 구현 (0) | 2022.12.27 |
[SPRING] Spring MVC // + Controller 자세히. (0) | 2022.12.27 |
[SPRING] 인코딩 필터 (0) | 2022.12.27 |
[SPRING] 의존성(dependencies) (0) | 2022.12.26 |