Notice
Recent Posts
Recent Comments
ยซ   2024/11   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
Archives
Today
In Total
๊ด€๋ฆฌ ๋ฉ”๋‰ด

A Joyful AI Research Journey๐ŸŒณ๐Ÿ˜Š

[62] 230328 Spring: ํšŒ์›๊ฐ€์ž…, ๋กœ๊ทธ์ธ ๊ธฐ๋Šฅ ๊ตฌํ˜„ํ•˜๊ธฐ [K-๋””์ง€ํ„ธ ํŠธ๋ ˆ์ด๋‹ 62์ผ] ๋ณธ๋ฌธ

๐ŸŒณBootcamp Revision 2023โœจ/Spring Framework, Java

[62] 230328 Spring: ํšŒ์›๊ฐ€์ž…, ๋กœ๊ทธ์ธ ๊ธฐ๋Šฅ ๊ตฌํ˜„ํ•˜๊ธฐ [K-๋””์ง€ํ„ธ ํŠธ๋ ˆ์ด๋‹ 62์ผ]

yjyuwisely 2023. 3. 28. 12:49

230328 Tue 62nd class

Ch. 18 MVC ํŒจํ„ด ๊ตฌํ˜„
์ง„๋„: Ch. 18 MVC ํŒจํ„ด ๊ตฌํ˜„์— ๊ธฐ๋ฐ˜ํ•œ ์ž์ฒด ์ˆ˜์—… (๊ต์žฌ: ์ตœ๋ฒ”๊ท ์˜ JSP 2.3 ์›น ํ”„๋กœ๊ทธ๋ž˜๋ฐ: ๊ธฐ์ดˆ๋ถ€ํ„ฐ ์ค‘๊ธ‰๊นŒ์ง€, ์ €์ž: ์ตœ๋ฒ”๊ท )
์ฑ… ์˜ˆ์ œ ์ฝ”๋“œ: https://github.com/madvirus/jsp23
์ €์ž ๋ธ”๋กœ๊ทธ: https://javacan.tistory.com/

 

์ตœ๋ฒ”๊ท ์˜ JSP 2.3 ์›น ํ”„๋กœ๊ทธ๋ž˜๋ฐ: ๊ธฐ์ดˆ๋ถ€ํ„ฐ ์ค‘๊ธ‰๊นŒ์ง€ | ์ตœ๋ฒ”๊ท  - ๊ต๋ณด๋ฌธ๊ณ 

์ตœ๋ฒ”๊ท ์˜ JSP 2.3 ์›น ํ”„๋กœ๊ทธ๋ž˜๋ฐ: ๊ธฐ์ดˆ๋ถ€ํ„ฐ ์ค‘๊ธ‰๊นŒ์ง€ | [์ตœ๋ฒ”๊ท ์˜ JSP 2.3 ์›น ํ”„๋กœ๊ทธ๋ž˜๋ฐ ๊ธฐ์ดˆ๋ถ€ํ„ฐ ์ค‘๊ธ‰๊นŒ์ง€]๋Š” JSP 2.3์˜ ์ƒˆ๋กœ์šด ํŠน์ง• ๋ฐ˜์˜๊ณผ JSP๋ฅผ ์ง€์›ํ•˜๋Š” ์š”์†Œ์ธ ์„œ๋ธ”๋ฆฟ๊ณผ ํ‘œํ˜„ ์–ธ์–ด ๋“ฑ์˜ ์ƒˆ๋กœ์šด

product.kyobobook.co.kr


์˜ค๋Š˜ ๋ฐฐ์šด ๊ฒƒ ์ค‘ ๊ธฐ์–ตํ•  ๊ฒƒ์„ ์ •๋ฆฌํ–ˆ๋‹ค.


๋‹ค์‹œ ๊ธฐ์–ตํ•  ๊ฒƒ

Get ๋ฐฉ์‹

URL ์ฃผ์†Œ ๋’ค์— ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ๋ถ™์—ฌ์„œ ๋ฐ์ดํ„ฐ๋ฅผ ์ „๋‹ฌํ•˜๋Š” ๋ฐฉ์‹์ด๋‹ค.
(URL(Uniform Resource Locator): ์ธํ„ฐ๋„ท์—์„œ ์›น ํŽ˜์ด์ง€, ์ด๋ฏธ์ง€, ๋น„๋””์˜ค ๋“ฑ ๋ฆฌ์†Œ์Šค์˜ ์œ„์น˜๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋Š” ๋ฌธ์ž์—ด)
Get ๋ฐฉ์‹์ด Post ๋ฐฉ์‹๋ณด๋‹ค ๋น„๊ต์  ์ „์†ก๋˜๋Š” ์†๋„๊ฐ€ ๋น ๋ฅด๋‚˜ ๋ณด์•ˆ์—๋Š” ์ทจ์•ฝํ•˜๋‹ค.
method๋ฅผ ๋”ฐ๋กœ ์–ธ๊ธ‰ ์•ˆ ํ•˜๋ฉด ๊ธฐ๋ณธ์ ์œผ๋กœ Get ๋ฐฉ์‹์ด๋‹ค. 

ex) ๊ฒ€์ƒ‰์–ด
ex) file:///D:/221229/html/WebContent/ch03_02_req.html?id=1234&password=5678

Post ๋ฐฉ์‹ 

์‚ฌ์šฉ์ž๊ฐ€ ์ž…๋ ฅํ•œ ๋ฐ์ดํ„ฐ๋ฅผ URL ์ฃผ์†Œ์— ๋ถ™์ด์ง€ ์•Š๊ณ  HTTP Request ํ—ค๋”์— ํฌํ•จ์‹œ์ผœ ์ „๋‹ฌํ•˜๋Š” ๋ฐฉ์‹์ด๋‹ค.
Post ๋ฐฉ์‹์ด Get ๋ฐฉ์‹๋ณด๋‹ค ๋ณด์•ˆ์—๋Š” ๊ฐ•ํ•˜๋‚˜, ์†๋„๋Š” ๋Š๋ฆฌ๋‹ค.
๊ธธ์ด ์ œํ•œ์ด ์—†์œผ๋ฉฐ, ๋ณด์•ˆ์ด ์œ ์ง€๋œ๋‹ค.  

ex) ๋กœ๊ทธ์ธ 
ex) file:///D:/221229/html/WebContent/ch03_02_req.html

2023.01.02 - [๐ŸŒณK-Digital Revision 2023โœจ/HTML5] - [4] 230102 Ch. 3 HTML5 ๋ฉ€ํ‹ฐ๋ฏธ๋””์–ด & ์ž…๋ ฅ์–‘์‹: ์›น ๋ธŒ๋ผ์šฐ์ €์™€ ๋ฉ€ํ‹ฐ๋ฏธ๋””์–ด, ์˜ค๋””์˜ค, ๋น„๋””์˜ค, iframe,์™€ , HTML ์ž…๋ ฅ ์–‘์‹, ์ž…๋ ฅ ํƒœ๊ทธ #1 · #2, HTML5 ์ž…๋ ฅ ์š”์†Œ [K-๋””์ง€ํ„ธ ํŠธ๋ ˆ์ด๋‹ 4์ผ]

 


์ดํด๋ฆฝ์Šค์— ์„œ๋ฒ„ ์‹œ์ž‘ ์ดˆ๋ก์ƒ‰ ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๊ณ  ์•„๋ž˜ localhost:8081๋กœ ๋“ค์–ด๊ฐ”๋‹ค.

๋ฉ”์ธ ํŽ˜์ด์ง€๊ฐ€ ๋œฌ๋‹ค.

์ปจํŠธ๋กค๋Ÿฌ๋Š” ์ด 6๊ฐœ์ธ๋ฐ HomeController.java๋ฅผ ์‹คํ–‰ํ•˜๋Š” ์ด์œ ๋Š” /์— ๋‹ต์ด ์žˆ๋‹ค.

์•„๋ž˜ ์ฝ”๋“œ๋ฅผ ๋ณด๋ฉด value = "/"์ธ ๊ฒŒ HomeController.java ๋ฟ์ด๋ฏ€๋กœ ๊ทธ๋ ‡๋‹ค. 

value๋Š” ์š”์ฒญ๋ฐ›์„ url์„ ์„ค์ •ํ•˜๊ฒŒ ๋œ๋‹ค.
method๋Š” ์–ด๋–ค ์š”์ฒญ์œผ๋กœ ๋ฐ›์„์ง€ ์ •์˜ํ•˜๊ฒŒ ๋œ๋‹ค.(GET, POST, PUT, DELETE ๋“ฑ)


์ฐธ๊ณ : https://mungto.tistory.com/436

@RequestMapping(value = "/", method = RequestMethod.GET)

์ „์ฒด 6๊ฐœ์˜ ์ปจํŠธ๋กค๋Ÿฌ ํŒŒ์ผ์ด๋‹ค.

์ฝ”๋“œ) HomeController.java

package org.hj.controller;

import java.util.Locale;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class HomeController {
	
	@RequestMapping(value = "/", method = RequestMethod.GET)
        // ์ ‘๊ทผ์ œ์–ด์ž  ๋ฐ˜ํ™˜ํƒ€์ž…  ๋ฉ”์„œ๋“œ๋ช…(๋งค๊ฐœ๋ณ€์ˆ˜, ๋งค๊ฐœ๋ณ€์ˆ˜)
        //public ์ ‘๊ทผ ์ œํ•œ์ž, String ๋ฆฌํ„ด ํƒ€์ž…, home ๋ฉ”์„œ๋“œ, (๋งค๊ฐœ๋ณ€์ˆ˜ํƒ€์ž…,๋งค๊ฐœ๋ณ€์ˆ˜๋ช…)
	public String home(Locale locale, Model model) { //์ง€๊ธˆ์€ ์“ฐ์ž„์ด ์—†๋‹ค. (๊ทธ๋ƒฅ ์žˆ๋Š” ๊ฒƒ.)
		return "home"; //home์ด๋ผ๋Š” ๋ฉ”์„œ๋“œ๋ฅผ ์‹คํ–‰ํ•œ๋‹ค. //๋ฉ”๋ชจ์žฅ์˜ URL์ฃผ์†Œ๊ฐ€ ํ˜ธ์ถœํ•œ ๊ฒƒ์ด๋‹ค. 
						// /WEB-INF/views/home.jsp
	}
	@RequestMapping(value = "/drop", method = RequestMethod.GET)
	// ์ ‘๊ทผ์ œ์–ด์ž  ๋ฐ˜ํ™˜ํƒ€์ž…  ๋ฉ”์„œ๋“œ๋ช…(๋งค๊ฐœ๋ณ€์ˆ˜, ๋งค๊ฐœ๋ณ€์ˆ˜)
	public String drop() {
		return "drop";
	}
}

return "home";์˜ home.jsp๋ฅผ ์‹คํ–‰ํ•œ ๊ฒฐ๊ณผ๊ฐ€ ์•„๋ž˜์™€ ๊ฐ™๋‹ค.
home์„ index.jsp๋กœ ๋ฐ”๊ฟ”์„œ ์šฐ๋ฆฌ๊ฐ€ ์ƒ๊ฐํ•˜๊ฑฐ๋‚˜ ์›ํ•˜๋Š” ๋ฉ”์ธ์œผ๋กœ ๋ฐ”๊ฟ€ ์ˆ˜ ์žˆ๋‹ค.

์šฐ๋ฆฌ๊ฐ€ ํ•œ ๊ฒƒ์€ ์ „๋ถ€ GET ๋ฐฉ์‹์ด๊ณ 
form ํƒœ๊ทธ๋งŒ POST ๋ฐฉ์‹์ด๋‹ค.


ํšŒ์›๊ฐ€์ž…, ๋กœ๊ทธ์ธ ๊ธฐ๋Šฅ ๊ตฌํ˜„ํ•˜๊ธฐ

memberin.jsp์™€ MemberController.java ํŒŒ์ผ + ์„œ๋ฒ„ ์‹คํ–‰์‹œ์ผฐ๋‹ค.

๋‘ ๊ฐœ์˜ ํŒŒ์ผ์ด ๋‹ค๋ฅด๋ฏ€๋กœ void ํƒ€์ž…์œผ๋กœ ์ฃผ๋ฉด ์•ˆ ๋œ๋‹ค.

์ฝ”๋“œ) ํšŒ์›๊ฐ€์ž… ํŽ˜์ด์ง€๊ฐ€ ๋œฌ๋‹ค. memberin.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>
</head>
<body>

<h1>ํšŒ์›๊ฐ€์ž…</h1>

<form action="/member" method="post"><!-- ๋ฉ”์„œ๋“œ๋ฐฉ์‹:POST -->
์•„์ด๋”” <input type="text" name="id">
๋น„๋ฐ€๋ฒˆํ˜ธ <input type="text" name="password">
๋ณ„๋ช… <input type="text" name="name">
<input type="submit" value="๊ฐ€์ž…ํ•˜๊ธฐ">

</form>


</body>
</html>

๊ฒฐ๊ณผ

์ฝ”๋“œ) ํšŒ์› ๊ฐ€์ž…, ํšŒ์› ๊ฐ€์ž… ์„œ๋ฒ„, ๋กœ๊ทธ์ธ, ๋กœ๊ทธ์ธ ์„œ๋ฒ„ MemberController.java

package org.hj.controller;

import javax.servlet.http.HttpSession;

import org.hj.model.LoginVO;
import org.hj.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class MemberController {

	@Autowired
	LoginService ls;
	
	// ํšŒ์›๊ฐ€์ž… //ํ™”๋ฉด ์‹คํ–‰์„ ์œ„ํ•œ ๋ฉค๋ฒ„ (GET๋ฐฉ์‹)
	@RequestMapping(value = "/member", method = RequestMethod.GET) //member ๋ฉ”์„œ๋“œ GET ๋ฐฉ์‹
	public String member () {
		return "member/memberin";
	}
	
	// ํšŒ์›๊ฐ€์ž…์„œ๋ฒ„ //์‹ค์ œ๋กœ ๊ฐ€์ž…ํ•˜๊ธฐ ์œ„ํ•œ ๋ฉค๋ฒ„ (POST๋ฐฉ์‹)
	@RequestMapping(value = "/member", method = RequestMethod.POST) //๋‘˜ ๋‹ค GET์ด๋ฉด ์ถฉ๋Œ์ด ์ผ์–ด๋‚œ๋‹ค. ๋ฉ”์„œ๋“œ ๋ฐฉ์‹์ด POST์ด๋‹ค.
	//							int		a //member๋Š” ์ฐธ์กฐ ๋ณ€์ˆ˜
	public String memberPost (LoginVO member) {
		System.out.println(member); //member.toString()์ด ์ƒ๋žต๋œ ๊ฒƒ์ด๋‹ค.
		return "board/list"; //board ํด๋”์˜ list.jsp๋ฅผ ์‹คํ–‰ํ•œ๋‹ค. ๊ฒŒ์‹œํŒ ๋ชฉ๋ก์„ ๋ณด์—ฌ์ค€๋‹ค.
	}
	
	
	
	
	// ๋กœ๊ทธ์ธ ํŽ˜์ด์ง€
	@RequestMapping(value = "/login", method = RequestMethod.GET)
	public String login () {
		System.out.println("aaaa");
		return "member/login";
	}
	
	// ๋กœ๊ทธ์ธ ์„œ๋ฒ„
	@RequestMapping(value = "/login", method = RequestMethod.POST)
	public String loginPost (HttpSession session, LoginVO member) {
		
/*		System.out.println("contorller="+member);
		System.out.println(ls.login(member));*/
		
		session.setAttribute("login", ls.login(member));
		
		if (ls.login(member)==null) {
			return "member/login";
			} else {	
				return "redirect:/list";	
			}
		
		
		}
	
	// ํšŒ์›๊ด€๋ฆฌํŽ˜์ด์ง€
	// ํšŒ์›์ •๋ณด์ˆ˜์ • ๋ฐ ์‚ญ์ œ
	
}

๊ฒฐ๊ณผ) localhost:8081/member๋กœ ์ ‘์†ํ•˜๊ณ  ์•„๋ž˜์˜ ํšŒ์›๊ฐ€์ž… ํŽ˜์ด์ง€๊ฐ€ ๋–ด๋‹ค. 

ํšŒ์›๊ฐ€์ž…์„ ํ•œ๋‹ค.


์ฝ”๋“œ) ํšŒ์› ๊ฐ€์ž…, ํšŒ์› ๊ฐ€์ž… ์„œ๋ฒ„, ๋กœ๊ทธ์ธ, ๋กœ๊ทธ์ธ ์„œ๋ฒ„ MemberController.java ์ผ๋ถ€

	// ํšŒ์›๊ฐ€์ž…์„œ๋ฒ„ //์‹ค์ œ๋กœ ๊ฐ€์ž…ํ•˜๊ธฐ ์œ„ํ•œ ๋ฉค๋ฒ„ (POST๋ฐฉ์‹)
	@RequestMapping(value = "/member", method = RequestMethod.POST) //๋‘˜ ๋‹ค GET์ด๋ฉด ์ถฉ๋Œ์ด ์ผ์–ด๋‚œ๋‹ค. ๋ฉ”์„œ๋“œ ๋ฐฉ์‹์ด POST์ด๋‹ค.
	//							int		a //member๋Š” ์ฐธ์กฐ ๋ณ€์ˆ˜
        public String memberPost (LoginVO member) {
		System.out.println(member); //member.toString()์ด ์ƒ๋žต๋œ ๊ฒƒ์ด๋‹ค.
		return "board/list"; //board ํด๋”์˜ list.jsp๋ฅผ ์‹คํ–‰ํ•œ๋‹ค. ๊ฒŒ์‹œํŒ ๋ชฉ๋ก์„ ๋ณด์—ฌ์ค€๋‹ค.
	}

 

return "board/list";: board ํด๋”์˜ list.jsp๋ฅผ ์‹คํ–‰ํ•œ๋‹ค. 

๊ฒฐ๊ณผ) ํšŒ์› ๊ฐ€์ž… ํ›„ ๋ฐ”๋กœ ๊ฒŒ์‹œํŒ ๋ชฉ๋ก์ด ๋œฌ๋‹ค.

memberin.jsp, MemberController.java, LoginVO.java

์œ„ ์„ธ ๊ฐœ ํŒŒ์ผ์˜(memberin.jsp, MemberController.java, LoginVO.java)์˜ id, password, name์„ ์ผ์น˜์‹œํ‚ค๋ฉด ์Šคํ”„๋ง์€ ์ž๋™์œผ๋กœ ์ˆ˜์ง‘ํ•œ๋‹ค.

์ฝ”๋“œ) LoginVO.java

package org.hj.model;

public class LoginVO {
	
	private String id;
	private String password;
	private String addr;
	private String phone;
	private String email;
	private String name;
	private int age;
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getAddr() {
		return addr;
	}
	public void setAddr(String addr) {
		this.addr = addr;
	}
	public String getPhone() {
		return phone;
	}
	public void setPhone(String phone) {
		this.phone = phone;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	
	@Override
	public String toString() {
		return "LoginVO [id=" + id + ", password=" + password + ", addr=" + addr + ", phone=" + phone + ", email="
				+ email + ", name=" + name + ", age=" + age + "]";
	}
}

์ƒ์†์— ๋Œ€ํ•ด์„œ ~๋ฅผ ์ƒ์†๋ฐ›๊ฒ ๋‹ค๋ผ๊ณ  ์ •์˜๋ฅผ ์•ˆ ํ•˜๋ฉด ๊ธฐ๋ณธ์ ์œผ๋กœ ๊ฐ์ฒด์˜ ์ตœ์ƒ์œ„ ํด๋ž˜์Šค์ธ ์˜ค๋ธŒ์ ํŠธ ํด๋ž˜์Šค๋ฅผ ์ƒ์†๋ฐ›๊ฒŒ ๋˜์–ด์žˆ๋‹ค.
์–ด๋–ค ํด๋ž˜์Šค๋ฅผ ๋ถ€๋ชจ ํด๋ž˜์Šค๋กœ ์ƒ์†๋ฐ›๊ฒ ๋‹ค๊ณ  ํ•˜๋ฉด ์˜ค๋ธŒ์ ํŠธ ํด๋ž˜์Šค๋Š” ๊ด€๋ จ ์—†๋‹ค.

์˜ค๋ธŒ์ ํŠธ ํด๋ž˜์Šค ์•ˆ์— toString()์ด๋ผ๋Š” ๋ฉ”์„œ๋“œ๊ฐ€ ์žˆ๋‹ค.
toString()์„ ์‹คํ–‰ํ•˜๋ฉด ํ•ด๋‹น๋˜๋Š” ๊ฐ์ฒด์˜ ์ฃผ์†Œ, ์ฆ‰ ์—ฌ๊ธฐ์„œ๋Š” LoginVO์˜ ์ฃผ์†Œ๊ฐ’์„ ์ถœ๋ ฅํ•˜๊ฒŒ ๋˜์–ด์žˆ๋‹ค.

ํ•˜์ง€๋งŒ ๋ถ€๋ชจ ํด๋ž˜์Šค์˜ toString()์ด ๋งˆ์Œ์— ๋“ค์ง€ ์•Š๊ณ , ์ฃผ์†Œ๊ฐ’ ๋Œ€์‹  ๋“ค์–ด๊ฐ„ ๋‚ด์šฉ(id, password, name ๋“ฑ)์ด ๊ถ๊ธˆํ•˜๋ฏ€๋กœ ๋ฐ”๊พผ ๊ฒƒ์ด๋‹ค. ์ € ์ฝ”๋“œ๋ฅผ ์ง€์šฐ๊ณ  ์‹คํ–‰ํ•˜๋ฉด LoginVO์˜ ์ฃผ์†Œ๊ฐ’์ด ๋‚˜์˜จ๋‹ค. 
์ฐธ๊ณ : https://kephilab.tistory.com/92


์ฝ˜์†”์ฐฝ์„ ๋ณด๋ฉด ์ž…๋ ฅํ–ˆ๋˜ id, password, name์ด ๋“ค์–ด๊ฐ„ ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค. 

์ฝ˜์†”์ฐฝ
์ฝ˜์†”์ฐฝ ๋ฉ”์‹œ์ง€ ํ™•๋Œ€. ์ž…๋ ฅํ–ˆ๋˜ id, password, name(๋ณ„๋ช…)์ด ๋‚˜์˜จ๋‹ค.


728x90
๋ฐ˜์‘ํ˜•
Comments