스프링

 

컨트롤

ExampleParameterController

import java.util.HashMap;
import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import kr.net.macaronics.mvc.domain.dto.ExampleParameter;
import kr.net.macaronics.mvc.domain.dto.ExampleRequestBodyUser;

@Controller
@RequestMapping("/example/parameter")
public class ExampleParameterController {

	/**
	 * 자료형으로 파라미터 받는 방법
	 * @param id
	 * @param code
	 * @param model
	 */
	@GetMapping("/example1")
	public void example1(@RequestParam String id, @RequestParam String code, Model model) {
		model.addAttribute("id" ,id);
		model.addAttribute("code" ,code);
	}
	
	/**
	 * Map을 활용한 파라미터 받는 방법
	 * @param paramMap
	 * @param model
	 */
	@GetMapping("/example2")
	public void example2(@RequestParam Map<String,Object> paramMap, Model model) {
		model.addAttribute("paramMap" ,paramMap);
	}
	
	/**
	 * Class를 활용한 파라메터 받는 방법
	 * @param parameter
	 * @param model
	 */
	@GetMapping("/example3")
	public void example3(ExampleParameter parameter, Model model) {
		model.addAttribute("parameter" ,parameter);
	}
	
	
	/**
	 * @PathVariable 을 활용한 파라메터 받는 방법
	 * @param id
	 * @param code
	 * @param model
	 * @return
	 */
	@GetMapping("/example4/{id}/{code}")
	public String example4(@PathVariable String id, @PathVariable String code, Model model) {
		model.addAttribute("id" ,id);
		model.addAttribute("code" ,code);
		return "/example/parameter/example4";
	}
	
	
	
	
	/**
	 * Stirng[] 배열을 받는방법
	 * @param ids
	 * @param model
	 * @return
	 */
	@GetMapping("/example5")
	public String example5(@RequestParam String[] ids, Model model) {
		model.addAttribute("ids" ,ids);		
		return "/example/parameter/example5";
	}
	
	
	
	/**
	 * example6 json Form 
	 */
	@GetMapping("/example6/form")
	public String example6() {
		return "/example/parameter/example6/form";
	}
	
	
	/**
	 * json Map 으로 받는 방법
	 * @param requestBody
	 * @param model
	 * @return
	 */
	@PostMapping("/example6/saveData")
	@ResponseBody
	public Map<String, Object> example6(@RequestBody Map<String,Object> requestBody, Model model) {
		Map<String, Object> resultMap=new HashMap<String, Object>();
		resultMap.put("requestBody", requestBody);
		resultMap.put("result", true);		
		return resultMap;
	}
	
	
	
	/**
	 * example7 json Form 
	 */
	@GetMapping("/example7/form")
	public String example7Form() {
		return "/example/parameter/example7/form";
	}
	
	/**
	 * json class 으로 받는 방법
	 * @param requestBody
	 * @param model
	 * @return
	 */
	@PostMapping("/example7/saveData")
	@ResponseBody
	public Map<String, Object> example7(@RequestBody ExampleRequestBodyUser requestBody, Model model) {
		Map<String, Object> resultMap=new HashMap<String, Object>();
		resultMap.put("requestBody", requestBody);
		resultMap.put("result", true);		
		return resultMap;
	}
	
	
	
	
	
}

 

 

1.자료형으로 파라미터 받는 방법

	/**
	 * 자료형으로 파라미터 받는 방법
	 * @param id
	 * @param code
	 * @param model
	 */
	@GetMapping("/example1")
	public void example1(@RequestParam String id, @RequestParam String code, Model model) {
		model.addAttribute("id" ,id);
		model.addAttribute("code" ,code);
	}
	

 

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Example</title>
</head>
<body>
<h1>example1</h1>
<h2> id : {{id}}</h2>
<h2> code : {{code}}</h2>
</body>
</html>

 

 

 

2.Map을 활용한 파라미터 받는 방법

	/**
	 * Map을 활용한 파라미터 받는 방법
	 * @param paramMap
	 * @param model
	 */
	@GetMapping("/example2")
	public void example2(@RequestParam Map<String,Object> paramMap, Model model) {
		model.addAttribute("paramMap" ,paramMap);
	}
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Example</title>
</head>
<body>
<h1>example2</h1>
<h2> paramMap.id : {{paramMap.id}}</h2>
<h2> paramMap.code : {{paramMap.code}}</h2>
</body>
</html>

 

 

 

3.Class를 활용한 파라메터 받는 방법

	/**
	 * Class를 활용한 파라메터 받는 방법
	 * @param parameter
	 * @param model
	 */
	@GetMapping("/example3")
	public void example3(ExampleParameter parameter, Model model) {
		model.addAttribute("parameter" ,parameter);
	}

ExampleParameter

import lombok.Data;

@Data
public class ExampleParameter {

	private String id;
	private String code;
}
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Example</title>
</head>
<body>
<h1>example3</h1>
<h2> parameter.id : {{parameter.id}}</h2>
<h2> parameter.code : {{parameter.code}}</h2>
</body>
</html>

 

 

 

4.@PathVariable 을 활용한 파라메터 받는 방법

	/**
	 * @PathVariable 을 활용한 파라메터 받는 방법
	 * @param id
	 * @param code
	 * @param model
	 * @return
	 */
	@GetMapping("/example4/{id}/{code}")
	public String example4(@PathVariable String id, @PathVariable String code, Model model) {
		model.addAttribute("id" ,id);
		model.addAttribute("code" ,code);
		return "/example/parameter/example4";
	}

 

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Example</title>
</head>
<body>
<h1>example4</h1>
<h2> id : {{id}}</h2>
<h2> code : {{code}}</h2>
</body>
</html>

 

 

 

 

5. Stirng[] 배열을 받는방법

	/**
	 * Stirng[] 배열을 받는방법
	 * @param ids
	 * @param model
	 * @return
	 */
	@GetMapping("/example5")
	public String example5(@RequestParam String[] ids, Model model) {
		model.addAttribute("ids" ,ids);		
		return "/example/parameter/example5";
	}

 

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Example</title>
</head>
<body>
<h1>example5</h1>
<h2> ids :  {{ids}}
</h2> 
{{#ids}}
	{{.}}
{{/ids}}
</body>
</html>

 

 

 

 

6.json Map 으로 받는 방법

	/**
	 * example6 json Form 페이지 이동
	 */
	@GetMapping("/example6/form")
	public String example6() {
		return "/example/parameter/example6/form";
	}
	
	
	/**
	 * json Map 으로 받는 방법
	 * @param requestBody
	 * @param model
	 * @return
	 */
	@PostMapping("/example6/saveData")
	@ResponseBody
	public Map<String, Object> example6(@RequestBody Map<String,Object> requestBody, Model model) {
		Map<String, Object> resultMap=new HashMap<String, Object>();
		resultMap.put("requestBody", requestBody);
		resultMap.put("result", true);		
		return resultMap;
	}
	
	

 

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Example</title>
</head>
<body>
<h1>example6</h1>
<textarea  style="width: 250px; height: 250px"></textarea> 
<br>
<button type="button" onclick="dataSubmit()">데이터 전송</button>
<script>
function dataSubmit(){
	 const json={
			user:{
				name:"홍길동",
				age :14,
				address:"대한민국"
			}	
		}

		fetch("/example/parameter/example6/saveData",{
			method:"POST",
			headers:{
				"Content-Type":"application/json"
			},
			body:JSON.stringify(json)
		})
		.then((response)=>response.json())
		.then((res)=>{
			console.log(res);
			document.querySelector("textarea").value=JSON.stringify(res, null, 4);
		}).catch((error)=>{	
			console.log("error :  {}", error);
		});
		
}
</script>
</body>
</html>

 

 

 

 

 

7.json class 으로 받는 방법

/**
	 * example7 json Form  페이지 이동
	 */
	@GetMapping("/example7/form")
	public String example7Form() {
		return "/example/parameter/example7/form";
	}
	
	/**
	 * json class 으로 받는 방법
	 * @param requestBody
	 * @param model
	 * @return
	 */
	@PostMapping("/example7/saveData")
	@ResponseBody
	public Map<String, Object> example7(@RequestBody ExampleRequestBodyUser requestBody, Model model) {
		Map<String, Object> resultMap=new HashMap<String, Object>();
		resultMap.put("requestBody", requestBody);
		resultMap.put("result", true);		
		return resultMap;
	}
	
	

 

ExampleRequestUser

import lombok.Data;

@Data
public class ExampleRequestUser {
	private String name;
	private String age;
	private String address;
	
}

 

ExampleRequestBodyUser

import lombok.Data;

@Data
public class ExampleRequestBodyUser {
	private ExampleRequestUser user;
}

 

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Example</title>
</head>
<body>
<h1>example6</h1>
<textarea  style="width: 250px; height: 250px"></textarea> 
<br>
<button type="button" onclick="dataSubmit()">데이터 전송</button>
<script>
function dataSubmit(){
	 const json={
			user:{
				name:"이순신",
				age :24,
				address:"대한민국"
			}	
		}

		fetch("/example/parameter/example7/saveData",{
			method:"POST",
			headers:{
				"Content-Type":"application/json"
			},
			body:JSON.stringify(json)
		})
		.then((response)=>response.json())
		.then((res)=>{
			console.log(res);
			document.querySelector("textarea").value=JSON.stringify(res, null, 4);
		}).catch((error)=>{	
			console.log("error :  {}", error);
		});
		
}
</script>
</body>
</html>

 

 

 

소스 : 

https://github.com/braverokmc79/sprig_boot_2.7.0_mybatis_board_project/commit/d6c8d8e7574b1350afd97c11c66be8cead731bce

 

 

 

 

 

 

spring

 

about author

PHRASE

Level 60  라이트

고난받는 자는 그 날이 다 험악하나 마음이 즐거운 자는 항상 잔치하느니라. -잠언

댓글 ( 4)

댓글 남기기

작성