package com.example.demo.controller.page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
@RequestMapping("")
@Controller
public class DefaultController {
@GetMapping({"/index"})
public String index() {
return "index";
}
}
<!DOCTYPE html>
<html>
<head>
<title>타이틀</title>
</head>
<body>
create page입니다.
</body>
</html>
: 결과
<!DOCTYPE html>
<html>
<head>
<title>타이틀</title>
</head>
<body>
create page입니다.
<div>
글 입력 테스트
</div>
제목
<input type = "text" id = "create_board_title"/>
내용
<input type = "text" id = "create_board_content"/>
작성자
<input type = "text" id = "create_board_author"/>
<button onclick="create_board()">저장</button>
<button onclick="change_html()">글 바꿔보기!</button>
</body>
</html>
jqeury에서 사용하는 서버와의 통신 기술
<body>
태그 안에 추가한다. (<head>
에 넣어도 된다.)<script src="https://code.jquery.com/jquery-3.7.1.js" integrity="sha256-eKhayi8LEQwp4NKxN+CfCh+3qOVUtJn3QNZ0TciWLP4=" crossorigin="anonymous"></script>
List<Map<String, Object>> boardList = new ArrayList<>(); // 입력 데이터를 저장하는 List
@GetMapping({"/create"})
public Map<String, Object> create(@RequestParam Map<String, Object> params) {
// 세 가지의 값을 params으로부터 가져온다.
String title = (String) params.get("title");
String content = (String) params.get("content");
String author = (String) params.get("author");
int order = boardList.size(); // 일련 번호 (몇 번째 게시물인지)
Map<String, Object> boardMap = new HashMap<>();
boardMap.put("title", title);
boardMap.put("content", content);
boardMap.put("author", author);
boardMap.put("order", ++order);
boardList.add(boardMap);
System.out.println(boardList); // 정상적으로 입력되었는지 확인.
Map<String, Object> resultMap = new HashMap<>();
resultMap.put("resultCode", 200);
return resultMap;
}
: create 메소드를 선언함으로써, /api/board/create
경로의 get 요청 API를 제공한다.
function change_html() {
// $("#div_id").html("바꾸고 싶은 글자!");
let create_board_title = $("#create_board_title").val();
// id가 create_boart_title인 html tag를 찾아 해당 태그의 value을 변수에 저장한다.
$("#div_id").html(create_board_title);
// create_board_title 변수에 저장된 내용으로 id가 div_id인 html tag의 값으로 바꾼다.
}
: 결과 화면 (제목의 input에 입력된 내용을 바꿔준다.)
function create_board(){
$.ajax({
// 요청 URL
url : "/api/board/create",
// 요청 방식
type : "GET",
// 요청 데이터
contentType : 'application/json; charset=utf-8',
data : {
title : $("#create_board_title").val(),
content : $("#create_board_content").val(),
author : $("#create_board_author").val()
},
// 동기식 or 비동기식 (false 시 비동기, ture 시 동기)
cache : false,
// 성공 시 처리할 logic
success : (obj_data, status, xhr) => {
alert(JSON.stringify(obj_data));
},
// error 발생 시 처리할 logic
error: (obj_data, status, error) => {
alert("error!!");
alert(JSON.stringify(obj_data));
}
})
}
: 결과 화면 (정상적으로 작동 시 ajax로부터 받은 "200" String을 alert로 띄운다.)
@GetMapping({"/list"})
public List<Map<String, Object>> list() {
return boardList;
}
: /api/board/list로 get 요청이 왔을 때 처리할 함수 (boardList의 전체 내용을 반환)
<!DOCTYPE html>
<html>
<head>
<title>타이틀</title>
</head>
<body>
list page입니다.
<div id = "div_list_board">
</div>
<button onclick="list_board()">글 전체 목록</button>
<script>
function list_board(){
$.ajax({
url : "/api/board/list",
type : "GET",
contentType : 'application/json; charset=utf-8',
data : {},
cache : false,
success : (obj_data, status, xhr) => {
alert(JSON.stringify(obj_data));
for(let each of obj_data){
$("#div_list_board").append("<a href = '/board/detail/" + each["order"] + "'>" + each["order"] + "//" + each["title"] + "<br/>" + "<a/>");
}
},
error: (obj_data, status, error) => {
alert("error!!");
alert(JSON.stringify(obj_data));
}
})
}
</script>
<script src="https://code.jquery.com/jquery-3.7.1.js" integrity="sha256-eKhayi8LEQwp4NKxN+CfCh+3qOVUtJn3QNZ0TciWLP4=" crossorigin="anonymous"></script>
</body>
</html>
@GetMapping({"/detail"})
public List<Map<String, Object>> detail(@RequestParam String order) {
int index = Integer.parseInt(order) - 1;
Map<String, Object> boardMap = boardList.get(index);
Map<String, Object> resultMap = new HashMap<>();
resultMap.put("resultCOde", 200);
resultMap.put("detail_board", boardMap);
return boardList;
}
: /detail get 요청이 왔을 때 처리할 함수 (boardList의 index 값의 내용을 반환)
<!DOCTYPE html>
<html>
<head>
<title>타이틀</title>
</head>
<body>
detail page입니다.
<input type = "text" id = "detail_board_order"/>
<div id = "div_list_board">
</div>
<button onclick="detail_board()">글 전체 목록</button>
<script>
function detail_board(){
$.ajax({
url : "/api/board/detail",
type : "GET",
contentType : 'application/json; charset=utf-8',
data : { order : $("#detail_board_order").val()},
cache : false,
success : (obj_data, status, xhr) => {
alert(JSON.stringify(obj_data));
},
error: (obj_data, status, error) => {
alert("error!!");
alert(JSON.stringify(obj_data));
}
})
}
</script>
<script src="https://code.jquery.com/jquery-3.7.1.js" integrity="sha256-eKhayi8LEQwp4NKxN+CfCh+3qOVUtJn3QNZ0TciWLP4=" crossorigin="anonymous"></script>
</body>
</html>
: 1번째 값이 정상적으로 경고창에 잘 띄어지는 것을 볼 수 있다.
: redirect를 사용하고, '?id'를 사용하여 URL Parameter를 사용하여 몇 번째 게시물인지 지정한다.
: 페이지 로드 시 URL Parameter에 있는 값을 가져와서 input 태그 안의 value에 저장한다.