[0602] 자바 웹 개발 과정🌞

Let's TECH🧐·2021년 6월 3일
0

자바 웹 개발 과정

목록 보기
22/31
post-thumbnail

Spring MVC

공통 url 분리

  • NoticeController
package com.newlecture.web.controller.admin;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController("adminNoticeController")
@RequestMapping("/admin/notice/") // 공통 url로 분리 가능!
public class NoticeController {

	@RequestMapping("list") // /admin/notice/list
	public String list() {
		return "admin notice list";
	}
	
	@RequestMapping("reg") // /admin/notice/reg
	public String reg() {
		return "admin notice reg";
	}
	
	@RequestMapping("edit") // /admin/notice/edit
	public String edit() {
		return "admin notice edit";
	}
}

Spring Boot Devtools

서버 코드 변경 시 서버 자동시작을 도와주는 도구

  • pom.xml

Get, PostMapping

Get 요청은 @GetMapping(), Post 요청은 @PostMapping() annotation을 설정해주면 된다.

  • NoticeController
// @RequestMapping(value = "reg", method = RequestMethod.GET) 
@GetMapping("reg")
public String reg() { // get, post 요청 둘 다 필요
	return "admin notice reg";
}

// @RequestMapping(value = "reg", method = RequestMethod.POST) 
@PostMapping("reg")
public String reg(String test) { // get, post 요청 둘 다 필요
	return "admin notice reg";
}

입력하기

test.html에서 입력한 값을 NoticeController의 reg 메소드를 거쳐 화면에 출력해보자!

예제 1) request.getParameter()

  • test.html
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
	<form action="/admin/notice/reg" method="get">
		<input type="text" name="x" value="0"><br>
		<input type="text" name="y" value="0"><br>
		<input type="submit" value="전송">
	</form>
</body>
  • NoticeController
@GetMapping("reg") 
public String reg(HttpServletRequest request) { 
	
	// 1. Servlet API를 이용한 방법(HttpServlet)
	String x = request.getParameter("x"); // test.html에서 입력된 값
	String y = request.getParameter("y"); // test.html에서 입력된 값
	
	return String.format("x: %s, y: %s", x, y);
}

예제 2) @RequestParam()

  • test.html
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
	<form action="/admin/notice/reg" method="get">
        <input type="text" name="f" value="0"><br>
		<input type="text" name="x" value="0"><br>
		<input type="text" name="y" value="0"><br>
		<input type="submit" value="전송">
	</form>
</body>
  • NoticeController
@GetMapping("reg") // get, post 요청 둘 다 필요
public String reg(
	@RequestParam(name = "f") String field, // parameter로 f가 왔을 때 field라는 변수로 이름 변경
	@RequestParam(defaultValue = "0") Integer x, // null, 빈 공백이 오면 기본값은 0, String을 Integer로 바꿔줌
	@RequestParam(defaultValue = "0") Integer y, HttpServletRequest request) { 
	          	
	
	// return String.format("x_: %s, y_: %s", x_, y_) + "<br>" 
	return String.format("x: %d, y: %d, result: %d, field: %s", x, y, x+y, field);
}
@GetMapping("reg") 
public String reg(
	@RequestParam(name = "f") String field, // parameter로 f가 왔을 때 field라는 변수로 이름 변경
	@RequestParam(defaultValue = "0") Integer x, // null, 빈 공백이 오면 기본값은 0, String을 Integer로 바꿔줌
	@RequestParam(defaultValue = "0") Integer y, 
	@CookieValue(/*name = "test", */defaultValue = "hi") String test, // 쿠키가 없을 때 기본값: hi, 쿠키 변수 이름은 test 
	HttpServletResponse response
		) { 
	
	if(test.equals("hi")) { // 쿠키가 없을 때는 쿠키를 담기
		Cookie cookie = new Cookie("test", "hello"); // 키: test
		cookie.setMaxAge(10*24*60*60); // expire되는 시간, 브라우저가 닫쳐도 10일 동안 쿠키가 유지됨
		cookie.setPath("/"); // 루트 아래 모든 경로에서 쿠키가 유지됨
		// 현재 path 이하에서만 쿠키를 볼 수 있도록 설정됨
		// admin/notice/* (o)
		// admin/notice/aa/bb/list (o)
		
		response.addCookie(cookie); // 처음 요청 시에는 쿠키가 없지만 응답에는 Set-Cookie에 키가 심어져서 옴
		// 다음에 페이지 재요청시 요청 헤더의 Cookie에 키값(query=hello)이 같이 감 			
	
	}
    
    	return String.format("x: %d, y: %d, result: %d, field: %s, test cookie: %s, uid: %s, id: %d\n", x, y, x+y, field, test, uid, id);
		
}
profile
Minju's Tech Blog

0개의 댓글