단순 코드 기록/Spring

Spring_Ajax

일일일코_장민기 2024. 2. 15. 11:12
728x90
@ResponseBody //jsp 없이 비동기 처리

 

package com.controller;

import java.util.ArrayList;
import java.util.HashMap;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.dto.Login;

@Controller
public class MainController {
	//<mvc:view-controller path="/" view-name="home"/>
	/*
	 * @Override public void addViewControllers(ViewControllerRegistry registry) {
	 * registry.addViewController("/").setViewName("main"); }
	 */
	@RequestMapping("/aaa")
	public @ResponseBody Login aaa() {
		Login login= new Login();
		login.setUserid("홍길동");
		login.setPasswd("1234");
		return login ;
	}
	@RequestMapping("/bbb")
	public @ResponseBody ArrayList<Login> bbb() {
		ArrayList<Login> list = new ArrayList<Login>();
		list.add(new Login("홍길동1", "10"));
		list.add(new Login("홍길동2", "20"));
		list.add(new Login("홍길동3", "30"));
		return list ;
	}
	@RequestMapping("/ccc")
	public @ResponseBody HashMap<String, ArrayList<Login>> ccc() {
		System.out.println("/ccc 호출====================");
		HashMap<String, ArrayList<Login>> map= new HashMap<String, ArrayList<Login>>();
		ArrayList<Login> list1 = new ArrayList<Login>();
		list1.add(new Login("홍길동1", "10"));
		list1.add(new Login("홍길동2", "20"));
		list1.add(new Login("홍길동3", "30"));
		ArrayList<Login> list2 = new ArrayList<Login>();
		list2.add(new Login("이순신1", "10"));
		list2.add(new Login("이순신2", "20"));
		list2.add(new Login("이순신3", "30"));
		map.put("one", list1);
		map.put("two", list2);		
		return map;
	}
	
}
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://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script type="text/javascript">
	$(document).ready(function() {
	  $("#aaa").on("click",function(){
		  $.ajax({
			url:"aaa",
			type:"get",
			dataType:"json",
			success:function(data,status,xhr){
				console.log(data, data.userid,data.passwd);
				$("#result").text(JSON.stringify(data));
			},
			error:function(xhr,status,error){}
		  }); 
	  });
	  $("#bbb").on("click",function(){
		  $.ajax({
			url:"bbb",
			type:"get",
			dataType:"json",
			success:function(data,status,xhr){
				console.log(data, data.userid,data.passwd);
				$("#result").text(JSON.stringify(data));
			},
			error:function(xhr,status,error){}
		  }); 
	  });
	  
	  $("#ccc").on("click",function(){
		  console.log("============");
		  $.ajax({
			url:"ccc",
			type:"get",
			dataType:"json",
			success:function(data,status,xhr){
				console.log(data);
				console.log(data.one);
				console.log(data.two);
				$("#result").text(JSON.stringify(data));
			},
			error:function(xhr,status,error){
				console.log("=========",error);
			}
		  }); 
	  });
	});
</script>
</head>
<body>
<button id="aaa">aaa호출 : login</button>
<button id="bbb">bbb호출 : ArrayList</button>
<button id="ccc">ccc호출 : HashMap</button>
<div id="result"></div>
</body>
</html>

 

 

 

 

 

 

 

 

 

 

 

 

@RestController //주소처리가 자동으로 @ResponseBody 처리(함수에서 @ResponseBody가 빠짐)
package com.controller;

import java.util.ArrayList;
import java.util.HashMap;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.dto.Login;

//@Controller
@RestController
public class MainController {
	//<mvc:view-controller path="/" view-name="home"/>
	/*
	 * @Override public void addViewControllers(ViewControllerRegistry registry) {
	 * registry.addViewController("/").setViewName("main"); }
	 */
	@RequestMapping("/aaa")
	public  Login aaa() {
		Login login= new Login();
		login.setUserid("홍길동");
		login.setPasswd("1234");
		return login ;
	}
	@RequestMapping("/bbb")
	public  ArrayList<Login> bbb() {
		ArrayList<Login> list = new ArrayList<Login>();
		list.add(new Login("홍길동1", "10"));
		list.add(new Login("홍길동2", "20"));
		list.add(new Login("홍길동3", "30"));
		return list ;
	}
	@RequestMapping("/ccc")
	public  HashMap<String, ArrayList<Login>> ccc() {
		System.out.println("/ccc 호출====================");
		HashMap<String, ArrayList<Login>> map= new HashMap<String, ArrayList<Login>>();
		ArrayList<Login> list1 = new ArrayList<Login>();
		list1.add(new Login("홍길동1", "10"));
		list1.add(new Login("홍길동2", "20"));
		list1.add(new Login("홍길동3", "30"));
		ArrayList<Login> list2 = new ArrayList<Login>();
		list2.add(new Login("이순신1", "10"));
		list2.add(new Login("이순신2", "20"));
		list2.add(new Login("이순신3", "30"));
		map.put("one", list1);
		map.put("two", list2);		
		return map;
	}
	
}

 

ResponseEntity(@ResponseBody 역할 + 상태코드 및 필요한 데이터를 저장하여 전달)
package com.controller;

import java.util.ArrayList;
import java.util.HashMap;

import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.dto.Login;

//@Controller
@RestController    //@Controller + @ResponseBody
public class MainController {
	//<mvc:view-controller path="/" view-name="home"/>
	/*
	 * @Override public void addViewControllers(ViewControllerRegistry registry) {
	 * registry.addViewController("/").setViewName("main"); }
	 */
	
	@RequestMapping("/ddd")
	public  ResponseEntity<Login> ddd() {
		Login login= new Login();
		login.setUserid("홍길동");
		login.setPasswd("1234");
		ResponseEntity<Login>  entity=						//ResponseEntoty = @ResponseBody 역할
				 ResponseEntity.status(200).body(login);	//상태코드 및 필요한 데이터를 저장 후 전달 가능
		return entity;										//200 = 정상일 때
	}
	@RequestMapping("/aaa")
	public  Login aaa() {
		Login login= new Login();
		login.setUserid("홍길동");
		login.setPasswd("1234");
		return login ;
	}
	@RequestMapping("/bbb")
	public  ArrayList<Login> bbb() {
		ArrayList<Login> list = new ArrayList<Login>();
		list.add(new Login("홍길동1", "10"));
		list.add(new Login("홍길동2", "20"));
		list.add(new Login("홍길동3", "30"));
		return list ;
	}
	@RequestMapping("/ccc")
	public  HashMap<String, ArrayList<Login>> ccc() {
		System.out.println("/ccc 호출====================");
		HashMap<String, ArrayList<Login>> map= new HashMap<String, ArrayList<Login>>();
		ArrayList<Login> list1 = new ArrayList<Login>();
		list1.add(new Login("홍길동1", "10"));
		list1.add(new Login("홍길동2", "20"));
		list1.add(new Login("홍길동3", "30"));
		ArrayList<Login> list2 = new ArrayList<Login>();
		list2.add(new Login("이순신1", "10"));
		list2.add(new Login("이순신2", "20"));
		list2.add(new Login("이순신3", "30"));
		map.put("one", list1);
		map.put("two", list2);		
		return map;
	}
	
}

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

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