단순 코드 기록/Spring

Spring_ajax&JSON(@RequestBody)

일일일코_장민기 2024. 2. 14. 15:55
728x90
main.jsp

 

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script src="https://code.jquery.com/jquery-3.6.4.min.js"></script>
</head>
<body>

main.jsp<br>
<input type="button" id="aaa" value="aaa"><br>
<input type="button" id="bbb" value="bbb"><br>
<input type="button" id="ccc" value="ccc"><br>
<input type="button" id="ddd" value="ddd"><br>
<input type="button" id="eee" value="eee">


<script type="text/javascript">

$("#aaa").on("click", function(){
	$.ajax({
		type: "post", 
		url: "aaa",
		headers: {"Content-Type": "application/json"},			
		data: JSON.stringify({userid: "강감찬", passwd: "5656"}),
		dataType: "text", 
		success : 
			function(data, status, xhr){
			console.log(data);				
		}, 
		error :
			function(xhr, status, error){
			console.log(error);
		}
	})
})

$("#bbb").on("click", function(){
		$.ajax({
			type: "post", 
			url: "bbb",
			headers: {"Content-Type": "application/json"},			
			data: JSON.stringify(							//자동으로 배열에 담아 저장
					[{userid: "AAAA", passwd: "aaaa"},
					{userid: "BBBB", passwd: "bbbb"}]
					),
			dataType: "text", 
			success : 
				function(data, status, xhr){
				console.log(data);				
			}, 
			error :
				function(xhr, status, error){
				console.log(error);
			}
		})
	})

	$("#ccc").on("click", function(){
		$.ajax({
			type: "post", 
			url: "ccc",
			data: {userid: "유관순", passwd: "1111"},
			dataType: "text", 
			success : 
				function(data, status, xhr){
				console.log(data);				
			}, 
			error :
				function(xhr, status, error){
				console.log(error);
			}
			
		})
	})

	$("#ddd").click(function(){
		$.ajax({
			type : "post",						//controller에서 method: RequestMethod.POST이어야 함
			url : "ddd",						//controller에 연결할 url
			data : {userid: "홍길동"},			//controller에서 파싱하면 "홍길동" 출력
			dataType : "text",
			success : 
				function(data, status, xhr){
				console.log(data);				//ddd.jsp의 코드를 읽어와서 console에 출력
			}, 
			error :
				function(xhr, status, error){
				console.log(error);
			}
		});
	});

	$("#eee").on("click", function(){
		$.ajax({
			type: "post", 
			url: "eee",
			data: {userid: "이순신", passwd: "1234"},
			dataType: "text", 
			success : 
				function(data, status, xhr){
				console.log(data);				//eee.jsp의 코드를 읽어와서 console에 출력
			}, 
			error :
				function(xhr, status, error){
				console.log(error);
			}
			
		})
	})
	

</script>

</body>
</html>

 

 

 

 

 

 

Controller
package com.app.test;

import java.util.ArrayList;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class TestController {

	@RequestMapping(value =  "/main", method = RequestMethod.GET)
	public String main() {
		System.out.println("main");
		return "main";
	}
	
	@RequestMapping(value =  "/aaa", method = RequestMethod.POST)
	public String aaa(@RequestBody LoginDTO dto) {
		System.out.println("aaa");
		System.out.println(dto.getUserid() + "\t" + dto.getPasswd());
		return "aaa";
	}
	
	@RequestMapping(value =  "/bbb", method = RequestMethod.POST)
	public String bbb(@RequestBody ArrayList<LoginDTO> list) {
		System.out.println("bbb");
		System.out.println(list);
		return "bbb";
	}
	
	
	
	
	
	

	@RequestMapping(value =  "/ccc", method = RequestMethod.POST)
	public String ccc(String userid, String passwd) {
		System.out.println("ccc");
		System.out.println(userid + "\t" + passwd);
		return "eee";
	}
	
	@RequestMapping(value =  "/ddd", method = RequestMethod.POST)
	public String ddd(String userid) {
		System.out.println("ddd");
		System.out.println(userid);
		return "ddd";
	}

	@RequestMapping(value =  "/eee", method = RequestMethod.POST)
	public String eee(LoginDTO dto) {
		System.out.println("eee");
		System.out.println(dto.getUserid() + "\t" + dto.getPasswd());
		return "eee";
	}

	
	
	
}

'단순 코드 기록 > Spring' 카테고리의 다른 글

Spring_JsonIgnore&Default  (0) 2024.02.15
Spring_Ajax  (0) 2024.02.15
Spring_보안폴더에서 외부파일 사용+xml주소처리  (0) 2024.02.14
Spring_Intercept_for_login  (0) 2024.02.14
Spring_Intercept  (0) 2024.02.14