- 스프링 데이터 받아오기
- 데이터 전송
- input 태그의 name과 매개변수 명을 일치시키면 바로 값을 받아올 수 있다.
- model.addAttribute("데이터명", 데이터) 를 통해 값을 넘겨줄 수 있다.
- @ModelAttribute("데이터명") 을 매개변수로 받아온 데이터 앞에 붙이면 model.addAttribute("데이터명", 데이터)를 생략할 수 있다.
package com.test.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 com.test.domain.SpringDTO;
@Controller
public class Ex05Controller {
@GetMapping("/ex05.do")
public String ex05() {
//글쓰기 페이지(add.do 역할)
return "ex05";
}
//데이터 수신
// - 기존: req.getParameter("")
/*
@PostMapping("/ex05ok.do")
public String ex05ok(HttpServletRequest req) {
//System.out.println(req == null);
//System.out.println(resp == null);
//System.out.println(session == null);
String data = req.getParameter("data");
req.setAttribute("data", data);
//글 쓴 후 확인 페이지(addok.do 역할)
return "ex05ok";
}
*/
/*
@PostMapping("ex05ok.do")
public String ex05ok(@RequestParam("data") String data, Model model) {
//String data = req.getParameter("data"); == @RequestParam("data") String data
model.addAttribute("data", data); //req.setAttribute
return "ex05ok";
}
*/
@PostMapping("ex05ok.do")
public String ex05ok(Model model, String data) {
//@RequestParam("data") 생략 가능!
//System.out.println(data);
model.addAttribute("data", data);
return "ex05ok";
}
/*
@PostMapping("ex05ok.do")
public String ex05ok(@ModelAttribute("data") String data) {
//@ModelAttribute("data") String data
// = model.addAttribute("data", data)
return "ex05ok";
}
*/
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix = "c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<link rel="stylesheet" href="https://me2.do/5BvBFJ57">
<style>
</style>
</head>
<body>
<h1>데이터 전송</h1>
<form method="POST" action="/web/ex05ok.do">
<div><input type="text" name="data"></div>
<div><input type="submit" value="보내기"></div>
</form>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.4/jquery.min.js"></script>
<script>
</script>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix = "c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<link rel="stylesheet" href="https://me2.do/5BvBFJ57">
<style>
</style>
</head>
<body>
<h1>결과</h1>
<div class="message" title="데이터">
${data}
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.4/jquery.min.js"></script>
<script>
</script>
</body>
</html>
- 복합 데이터 전송
- input 태그의 name을 DTO 변수명과 일치시키면 @PostMapping의 매개변수로 DTO를 사용할 때 set 과정을 생략할 수 있다. (처리되어 받아온다.)
- hidden 태그도 name을 일치시켜 바로 받아올 수 있다.
package com.test.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class Ex05Controller {
@GetMapping("/ex05.do")
public String ex05() {
//글쓰기 페이지(add.do 역할)
return "ex05";
}
//복합 데이터 전송
/*
@PostMapping("ex05ok.do")
public String ex05ok(
Model model,
@RequestParam("name") String name,
@RequestParam("age") String age,
@RequestParam("address") String address
) {
SpringDTO dto = new SpringDTO();
dto.setName(name);
dto.setAge(age);
dto.setAddress(address);
model.addAttribute("dto", dto);
return "ex05ok";
}
*/
/*
@PostMapping("ex05ok.do")
public String ex05ok(
Model model,
String name,
String age,
String address
) {
SpringDTO dto = new SpringDTO();
dto.setName(name);
dto.setAge(age);
dto.setAddress(address);
model.addAttribute("dto", dto);
return "ex05ok";
}
*/
@PostMapping("ex05ok.do")
public String ex05ok(Model model, SpringDTO dto, String seq) {
//System.out.println(dto);
model.addAttribute("dto", dto);
//System.out.println(seq);
return "ex05ok";
}
}
package com.test.domain;
import lombok.Data;
@Data
public class SpringDTO {
private String name;
private String age;
private String address;
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix = "c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<link rel="stylesheet" href="https://me2.do/5BvBFJ57">
<style>
</style>
</head>
<body>
<h1>복합 데이터 전송</h1>
<form method="POST" action="/web/ex05ok.do">
<div><input type="text" name="name"></div>
<div><input type="text" name="age"></div>
<div><input type="text" name="address"></div>
<div><input type="submit" value="보내기"></div>
<input type="hidden" name="seq" value="10">
</form>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.4/jquery.min.js"></script>
<script>
</script>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix = "c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<link rel="stylesheet" href="https://me2.do/5BvBFJ57">
<style>
</style>
</head>
<body>
<h1>결과</h1>
<div class="message" title="DTO">
<div>${dto.name}</div>
<div>${dto.age}</div>
<div>${dto.address}</div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.4/jquery.min.js"></script>
<script>
</script>
</body>
</html>
- 다중 데이터 전송
package com.test.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 com.test.domain.SpringDTO;
@Controller
public class Ex05Controller {
@GetMapping("/ex05.do")
public String ex05() {
//글쓰기 페이지(add.do 역할)
return "ex05";
}
//다중 데이터 받는 방법
@PostMapping("ex05ok.do")
public String ex05ok(Model model,
//@RequestParam("cb") String[] cb
String[] cb
//@RequestParam("cb") List<String> cb
//List<String> cb > 동작(X)
) {
//String[] list = req.getParameterValues("cb")
model.addAttribute("cb", cb);
return "ex05ok";
}
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix = "c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<link rel="stylesheet" href="https://me2.do/5BvBFJ57">
<style>
</style>
</head>
<body>
<h1>다중 데이터 전송</h1>
<form method="POST" action="/web/ex05ok.do">
<div><input type="checkbox" name="cb" value="1">사과</div>
<div><input type="checkbox" name="cb" value="2">바나나</div>
<div><input type="checkbox" name="cb" value="3">딸기</div>
<div><input type="checkbox" name="cb" value="4">귤</div>
<div><input type="checkbox" name="cb" value="5">포도</div>
<div><input type="submit" value="보내기"></div>
</form>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.4/jquery.min.js"></script>
<script>
</script>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix = "c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<link rel="stylesheet" href="https://me2.do/5BvBFJ57">
<style>
</style>
</head>
<body>
<h1>결과</h1>
<div class="list">
<c:forEach items="${cb}" var="item">
<div>${item}</div>
</c:forEach>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.4/jquery.min.js"></script>
<script>
</script>
</body>
</html>
- 요청 메서드의 반환 자료형
1. String
- JSP 파일명
- ViewResolver
package com.test.controller;
import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.test.domain.SpringDTO;
@Controller
public class Ex06Controller {
//요청 메서드의 반환 자료형
//1. String > 가장 많이 사용
@GetMapping("/ex06.do")
public String test() {
return "ex06";
}
}
2. void
- 요청 주소와 동일한 이름의 JSP 페이지를 자동으로 호출한다.
- 관리하기 불편..
package com.test.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class Ex06Controller {
//2. void
@GetMapping("/ex06.do")
public void test() {
}
}
3.String + 키워드
- redirect: URL
- forward: URL
- RedirectAttributes rttr 을 매개변수로 넣고 rttr.addAttribute("데이터명", 값) 을 해주면 get 형식으로 url 뒤에 데이터가 추가된다.
package com.test.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class Ex06Controller {
//3. String + 키워드
/*
@GetMapping("/ex06.do")
public String test() {
// = response.sendRedirect()
return "redirect:/ex05.do"; //> 종종 사용
// = pageContext.forward() > 쓸일이 많지 X
//return "forward:/ex05.do";
}
*/
@GetMapping("/ex06.do")
public String test(RedirectAttributes rttr) {
String seq = "5";
String type= "1";
rttr.addAttribute("seq", seq);
rttr.addAttribute("type", type);
//return "redirect:/ex05.do?seq=" + seq + "&type=" + type;
return "redirect:/ex05.do";
}
}
4. 객체
- @ResponseBody : JSON 반환값을 생성하는 어노테이션(JSON을 돌려준다.) > ajax 사용할 때
package com.test.controller;
import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.test.domain.SpringDTO;
@Controller
public class Ex06Controller {
//4. 객체
/*
@GetMapping("/ex06.do")
public @ResponseBody SpringDTO test() {
SpringDTO dto = new SpringDTO();
dto.setName("홍길동");
dto.setAge("20");
dto.setAddress("서울시");
return dto;
}
*/
@GetMapping("/ex06.do")
public @ResponseBody List<SpringDTO> test() {
List<SpringDTO> list = new ArrayList<SpringDTO>();
SpringDTO dto = new SpringDTO();
dto.setName("홍길동");
dto.setAge("20");
dto.setAddress("서울시");
list.add(dto);
return list;
}
}
'서버 > Spring' 카테고리의 다른 글
[스프링(Spring)] MyBatis (1) | 2023.06.16 |
---|---|
[스프링(Spring)] MVC 에러 처리 (0) | 2023.06.16 |
[스프링(Spring)] AOP (1) | 2023.06.15 |
[스프링(Spring)] JUnit (0) | 2023.06.14 |
[스프링(Spring)] DI, IoC (0) | 2023.06.14 |