영상으로 공부중이고 안써봤던

1. @DeleteMapping  

2. @PathVariable   // 짝꿍 @RequestParam

3. 도움준 친구들  @GetMapping  / @PostMapping

관련한 정리를 합니다.

기본적 내용은 jsp에서 id입력해서 controll에서 값을 받는지까지입니다. 

전시간 정리했던 swagger로 한번더 확인하기 입니다.

 

1. @DeleteMapping  연습

	//controller
    @DeleteMapping(value="/member/deleteId/{id}")
	public String DeleteVariable(@PathVariable("id") int id) {
		
		System.out.println("회원탈퇴 하였습니다 : " + id);
		return "index";
	}
    
    //jsp파일
    <input type="int" id="delId" placeholder="번호입력하세요">
	<button id="delBtn">탈퇴</button>
    
    //스크립트
    	$('#delBtn').on('click', function(){
		var id = $('#delId').val();
		location.href="/member/deleteId?id=" + id;

에러~~~~~ jsp에는 post 또는 get만 된다는 것....혹시몰라서 void로도 해봤으나 주고 받기 모두 안됨...

현재 난 jsp파일로 화면구현을 하고 있기 때문에 요건 못쓰것다..  그래서 그냥 post와 get으로 하기로 함.

 

2번째 @PathVariable.....

@PostMapping(value="/member/deleteId/{id}")
public void DeleteVariable(@PathVariable("id") int id) {
    System.out.println("회원탈퇴 하였습니다 : " + id);
    return "index";
}

먼저 post로 변경하고 했더만....

2023-03-07 10:25:49.229 DEBUG 26592 --- [nio-8047-exec-9] o.s.web.servlet.DispatcherServlet : Completed 404 NOT_FOUND

2023-03-07 10:25:49.229 DEBUG 26592 --- [nio-8047-exec-9] o.s.web.servlet.DispatcherServlet : "ERROR" dispatch for POST "/error", parameters={}

2023-03-07 10:25:49.230 DEBUG 26592 --- [nio-8047-exec-9] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped to org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController#error(HttpServletRequest)

2023-03-07 10:25:49.230 DEBUG 26592 --- [nio-8047-exec-9] o.s.w.s.m.m.a.HttpEntityMethodProcessor : Using 'application/json', given [*/*] and supported [application/json, application/*+json, application/json, application/*+json]

2023-03-07 10:25:49.230 DEBUG 26592 --- [nio-8047-exec-9] o.s.w.s.m.m.a.HttpEntityMethodProcessor : Writing [{timestamp=Tue Mar 07 10:25:49 KST 2023, status=404, error=Not Found, message=JSP file [/WEB-INF/vie (truncated)...]

2023-03-07 10:25:49.231 DEBUG 26592 --- [nio-8047-exec-9] o.s.web.servlet.DispatcherServlet : Exiting from "ERROR" dispatch, status 404

가만보니 당연하다.... 요아이는  post인데 get 방식으로 넘겨준다....

jsp파일 수정

<p>회원탈퇴
<form action="/member/deleteId", method="post">
    <input type="int" name="id" placeholder="번호입력하세요">
	<button type="submit">탈퇴</button>
</form>

그래도 안된다....그래서 일단 익숙한

@RequestParam 변경한다.....

public String DeleteVariable(@RequestParam int id) {
    System.out.println("회원탈퇴 하였습니다 : " + id);
    return "index";
}

정상작동된다.....

왜일까.....검색또 시작 들어간다...

봐더니...특징이 있었던것이다....

@RequestParam 은 전달할때 주소 뒤 ? 이후 변수명=값?변수명=값?변수명=값 이고.

@PathVariable 은  url방식으로 주소뒤 /변수명/변수명/변수명 이렇게 추가되는 것이다.

 

결국  post방식으로 PathVariable도 안되는 것이다....."form으로 값을 전달하니깐"

 

그럼 다시 Get으로 돌아와서...

//jsp
 <input type="int" id="delId" placeholder="번호입력하세요">
 <button id="delBtn">탈퇴</button> 
 
  	$('#delBtn').on('click', function(){
		var id = $('#delId').val();
		location.href="/member/deleteId/" + id;		// '/'뒤에 붙는 것 유의
	})
    
    //controller
   	@GetMapping(value="/member/deleteId/{id}")
	public String DeleteVariable(@PathVariable("id") int id) {
		
		System.out.println("회원탈퇴 하였습니다 : " + id);
		return "index";
	}

콘솔도 잘 나오고 swagger도 200성공

마무리..정리.

1. @DeleteMappin  은 jsp파일에는 붙일 수 없다.

2. @PathVariable 은 post는 할수 없다. get으로 가야한다.

post를 쓸경우 전처럼 @RequestParam으로 값전달 한다...

 

혹시나 다른방법이 있는데 오늘의 내가 모를수 있다는거....양해부탁드립니다~~^^

 

+ Recent posts