Notice
Recent Posts
Recent Comments
ยซ   2024/12   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
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 31
Archives
Today
In Total
๊ด€๋ฆฌ ๋ฉ”๋‰ด

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

[64] 230330 Spring: ํšŒ์› ์ƒ์„ธ ์ •๋ณด ๋ณด๋Š” ํŽ˜์ด์ง€ ๋งŒ๋“ค๊ธฐ [K-๋””์ง€ํ„ธ ํŠธ๋ ˆ์ด๋‹ 64์ผ] ๋ณธ๋ฌธ

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

[64] 230330 Spring: ํšŒ์› ์ƒ์„ธ ์ •๋ณด ๋ณด๋Š” ํŽ˜์ด์ง€ ๋งŒ๋“ค๊ธฐ [K-๋””์ง€ํ„ธ ํŠธ๋ ˆ์ด๋‹ 64์ผ]

yjyuwisely 2023. 3. 30. 10:49

230329 Thu 64th 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


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


void ํƒ€์ž…์˜ ๋ฉ”์†Œ๋“œ๋Š” ๊ฐ’์„ ์ „๋‹ฌ ํ•  ์ˆ˜ ์—†๊ณ , ๋‹จ์ง€ ์ถœ๋ ฅ๋งŒ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.
์ฐธ๊ณ : https://cloudstudying.kr/questions/49


ํ”„๋ก ํŠธ์—”๋“œ ์˜์—ญ์€ servlet-context.xml
๋ฐฑ์—”๋“œ ์˜์—ญ์€ root-context.xml์ด ๋งก๋Š”๋‹ค.

์ฝ”๋“œ) servlet-context.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:beans="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd
		http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">

	<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
	
	<!-- Enables the Spring MVC @Controller programming model -->
	<annotation-driven />

	<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
	<resources mapping="/resources/**" location="/resources/" />

	<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
	<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<beans:property name="prefix" value="/WEB-INF/views/" />
		<beans:property name="suffix" value=".jsp" />
	</beans:bean>
	
	<context:component-scan base-package="org.hj.controller" />
	
	<beans:bean id="multipartResolver" class="org.springframework.web.multipart.support.StandardServletMultipartResolver">
	</beans:bean>

</beans:beans>


์ฝ”๋“œ) root-context.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:mybatis-spring="http://mybatis.org/schema/mybatis-spring"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring-1.2.xsd
		http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
	
	<!-- Root Context: defines shared resources visible to all other web components -->
	<!-- DBCP(DB Connection Pool) -->
	
	<bean id="hikariConfig" class="com.zaxxer.hikari.HikariConfig">
		<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
		<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/sample?serverTimezone=Asia/Seoul"></property>
		<property name="username" value="root"></property>
		<property name="password" value="1234"></property>
	</bean>
	
	
	
	
	<bean id="dataSource" class="com.zaxxer.hikari.HikariDataSource" destroy-method="close">
		<constructor-arg ref="hikariConfig" />
	</bean>
	
	<!-- Mybatis ๊ด€๋ จ ์„ค์ • SessionFactory -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSource"></property>
	</bean>
		
	<mybatis-spring:scan base-package="org.hj.mapper"/>	
	
	<context:component-scan base-package="org.hj.service"/>
	
		
</beans>

์ฒซ๋ฒˆ์งธ resources: xmlํŒŒ์ผ๋“ค์ด ์žˆ๋‹ค. (select์ ˆ, insert์ ˆ ๋“ฑ)
๋‘๋ฒˆ์งธ resources: ์™ธ๋ถ€ css, jsํŒŒ์ผ ๋„ฃ๋Š” ๊ณต๊ฐ„ 


MySQL์˜ ๋ฐ์ดํ„ฐ LoginVO.java (์ปจํŠธ๋กค๋Ÿฌ)๋กœ ๊ฐ„๋‹ค.


์ฝ”๋“œ) 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;

}

listDetail.jsp๋กœ ๋„˜์–ด๊ฐ„๋‹ค. 


๋ฌธ์ œ ํ’€์–ด๋ณด๊ธฐ)

์ง€๊ธˆ๊นŒ์ง€ ๋ฐฐ์šด ๋‚ด์šฉ์„ ํ† ๋Œ€๋กœ
http://localhost:8081/list(ํšŒ์›๋ชฉ๋ก๋ฆฌ์ŠคํŠธ)์—์„œ

1) ID๋ฅผ ํด๋ฆญํ•˜๋ฉด ๊ทธ ํšŒ์›์˜ ์ƒ์„ธ๋‚ด์šฉ์„ ์•Œ ์ˆ˜ ์žˆ๋Š” ํ™”๋ฉด์œผ๋กœ ์ด๋™ํ•จ๊ณผ
2) ๋™์‹œ์— ์ •๋ณด๋ฅผ ์กฐํšŒํ•˜๊ณ (select ๊ฒฐ๊ณผ 1 ๊ฑด - login์„ ์ฐธ๊ณ )

3) ๊ทธ ํ™”๋ฉด์— ์ˆ˜์ •๊ณผ ์‚ญ์ œ ๋ฒ„ํŠผ์„ ์ƒ์„ฑํ•˜์—ฌ,
4) ์ˆ˜์ • ๋ฐ
5) ์‚ญ์ œ(ํšŒ์› ๊ฐ€์ž… - insert๋ฅผ ์ฐธ์กฐ)๋ฅผ ํ•˜๋„๋ก ๊ตฌํ˜„ํ•ด๋ณด์„ธ์š”.

์ฐธ๊ณ : https://doublesprogramming.tistory.com/91

์ฐธ๊ณ : https://yoonbitnara.github.io/spring/53/


LoginMapper.xml์—์„œ <๋ฅผ ์น˜๋ฉด ์“ธ ์ˆ˜ ์žˆ๋Š” ์ฝ”๋“œ๋“ค์ด ๋‚˜์˜จ๋‹ค.


id ๊ฐ’์„ ๋„˜๊ธฐ๊ธฐ

(๋‚ด๊ฐ€ ์“ด ๊ฑด ๋ณ€์ˆ˜๋ช… memlistDetail์ด๋‹ค. ์ˆ˜์—…์—์„  memdetail๋กœ ์”€)

ํ™”๋ฉด์˜ id
http://localhost:8081/listDetail?id=abcd

MemberController.java
   memlistDetail()
   → ls.memlistDetail()

 LoginService.java

public void memlistDetail();

 LoginServiceImpl.java

public void memlistDetail(){
lm.memlistDetail()
}

 LoginMapper.java

public void memlistDetail();

LoginMapper.xml

select *
from member
where id=#{id}

๋ฐ˜๋Œ€ ๋ฐฉํ–ฅ์œผ๋กœ๋„ ๊ฐ€๋Šฅํ•˜๋‹ค.
return์„ ์ด์šฉํ•œ๋‹ค. 

LoginMapper.xml์˜ LoginVO(๋ชจ๋ธ)๊ฐ€ ๋„˜์–ด๊ฐˆ ๋•Œ
๋‹ค๋ฅธ ํŒŒ์ผ์˜ void ๋•Œ๋ฌธ์— return์ด ์•ˆ ๋˜๋ฏ€๋กœ ๋‹ค ๋ฐ”๊ฟ”์•ผํ•œ๋‹ค. 


๋‘ ๊ฐ€์ง€ ๋ฐฉ๋ฒ•
1. form ํƒœ๊ทธ๋ฅผ ์ด์šฉํ•œ๋‹ค.

<form action="listDetail">
	์•„์ด๋””<input type="text" name="id>
</form>


2. a ํƒœ๊ทธ ์ด์šฉํ•˜๋ฉด ๋œ๋‹ค.

<a href="listDetail?id=์•„์ด๋””๊ฐ’">์•„์ด๋””</a>

listDetail.jsp๋กœ ๊ฐ€๊ธฐ ์ „์— select๋กœ ์•„์ด๋”” ๊ฐ’์„ ๊ฐ€์ ธ๊ฐ„๋‹ค.


void add(int a){ //๋ฉ”์„œ๋“œ ์„ ์–ธ

}
add(10) //๋ฉ”์„œ๋“œ ํ˜ธ์ถœ

์œ„์˜ ํ˜•ํƒœ๋Š” ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

public void memlistDetail(String id);//[#๋ฉ”์„œ๋“œ ์„ ์–ธ#]

ls.memlistDetail(id);//[#๋ฉ”์„œ๋“œ ํ˜ธ์ถœ#]

id๋Š” ๊ธฐ๋ณธํ‚ค์ด๋ฏ€๋กœ ์ค‘๋ณต ์•ˆ ๋œ๋‹ค. ํ•œ ๊ฑด๋งŒ ๋‚˜์˜จ๋‹ค.


์ฝ”๋“œ 1) list.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>     
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>ํšŒ์› ๊ด€๋ฆฌ ํŽ˜์ด์ง€</title>
</head>
<body>
<!--  ${memlist} -->
	<table border="1">
	<tr>
		<td>ID</td><td>๋น„๋ฐ€๋ฒˆํ˜ธ</td><td>์ฃผ์†Œ</td><td>ํฐ๋ฒˆํ˜ธ</td><td>์ด๋ฉ”์ผ</td><td>์ด๋ฆ„</td><td>์ƒ์ผ</td>
	</tr> <!-- c:forEach var="๋ณ€์ˆ˜" items="์•„์ดํ…œ" -->
	<c:forEach var="list" items="${memlist}"> <!-- ๋ฐ˜๋ณตํ•œ๋‹ค. -->
		<tr>
		<td><a href="listDetail?id=${list.id}">${list.id}</a></td> 
		<!-- ๋‚ด๊ฐ€ ํ‹€๋ฆฐ ๋ถ€๋ถ„: <a href="listDetail"> ์—์„œ ?id=${list.id} ์ถ”๊ฐ€ํ•˜๋ฉด id๊ฐ’์ด listDetail.jsp๋กœ ๋„˜์–ด๊ฐ€๊ฒŒ ๋œ๋‹ค. -->
		<td>${list.password}</td>
		<td>${list.addr}</td>
		<td>${list.phone}</td>
		<td>${list.email}</td>
		<td>${list.name}</td>
		<td>${list.age}</td>
	</tr>
	</c:forEach>
	</table>
</body>
</html>



์ฝ”๋“œ 2) listDetail.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>๊ฐœ์ธ ํšŒ์› ๊ด€๋ฆฌ ์ƒ์„ธ ํŽ˜์ด์ง€</title>
</head>
<body>

<!-- DB ์ž‘์—…์„ ํ•ด์•ผํ•œ๋‹ค. ์„œ๋น„์Šค์™€ Mapper์— ๋ณด๋‚ด์ค€๋‹ค. -->

<%-- ${listDetail} --%>

<a href="listDetail?id=${list.id}">์•„์ด๋””</a>

	<table border="1">
	<tr>
		<td>ID</td><td>${listDetail.id}</td>
	</tr>
	<tr>	
		<td>๋น„๋ฐ€๋ฒˆํ˜ธ</td><td>${listDetail.password}</td>
	</tr>
	<tr>	
		<td>์ฃผ์†Œ</td><td>${listDetail.addr}</td>
	</tr>
	<tr>
		<td>ํฐ๋ฒˆํ˜ธ</td><td>${listDetail.phone}</td>
	</tr>
	<tr>
		<td>์ด๋ฉ”์ผ</td><td>${listDetail.email}</td>
	</tr>	
	<tr>
		<td>์ด๋ฆ„</td><td>${listDetail.name}</td>
	</tr>
	<tr>
		<td>์ƒ์ผ</td><td>${listDetail.age}</td>
	</tr>
	</table>
<form>
<input type="submit" value="์ˆ˜์ •" formaction="modifyMember.jsp">
<input type="submit" value="์‚ญ์ œ" formaction="removeMember.jsp">
</form>	
</body>
</html>



์ฝ”๋“œ 3) MemberController.java

package org.hj.controller;

import javax.servlet.http.HttpSession;

import org.apache.tomcat.util.net.openssl.ciphers.Authentication;
import org.hj.model.LoginVO; //๋ชจ๋ธ(LoginVO)์ด ์žˆ์Œ์„ ์•Œ๋ ค์ค€๋‹ค.
import org.hj.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
//import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;


@Controller

public class MemberController { //MVC์ค‘ ์ปจํŠธ๋กค๋Ÿฌ ์—ญํ• ์„ ํ•œ๋‹ค.

	@Autowired
	LoginService ls; //LoginService ls = new LoginService(); ํด๋ž˜์Šค๋ฅผ new๋ฅผ ์จ์„œ ๊ฐ์ฒดํ™”ํ•œ ๊ฒƒ. 
	//LoginService๋Š” ์ธํ„ฐํŽ˜์ด์Šค์ด๋ฏ€๋กœ ์›๋ž˜ new๋Š” ์•ˆ ๋˜๋Š” ๋ฐ ์œ„์— @AutoWired ์ด์šฉํ•ด์„œ ๊ฐ€๋Šฅํ•˜๊ฒŒ ํ–ˆ๋‹ค.
	
	// ํšŒ์›๊ฐ€์ž… //ํ™”๋ฉด ์‹คํ–‰์„ ์œ„ํ•œ ๋ฉค๋ฒ„ (GET๋ฐฉ์‹)
	@RequestMapping(value = "/member", method = RequestMethod.GET) //member ๋ฉ”์„œ๋“œ GET ๋ฐฉ์‹
	public String member () {
		return "member/memberin"; //๋ฉค๋ฒ„ ํด๋”์˜ memberin.jsp๋ฅผ ์‹คํ–‰ํ•œ๋‹ค.
	}
	
	// ํšŒ์›๊ฐ€์ž…์„œ๋ฒ„ //์‹ค์ œ๋กœ ๊ฐ€์ž…ํ•˜๊ธฐ ์œ„ํ•œ ๋ฉค๋ฒ„ (POST๋ฐฉ์‹)
	@RequestMapping(value = "/member", method = RequestMethod.POST) //๋‘˜ ๋‹ค GET์ด๋ฉด ์ถฉ๋Œ์ด ์ผ์–ด๋‚œ๋‹ค. ๋ฉ”์„œ๋“œ ๋ฐฉ์‹์ด POST์ด๋‹ค.
	//							int		a //member๋Š” ์ฐธ์กฐ ๋ณ€์ˆ˜
	//public String memberPost(String id, String password, String name) //๋ฉ”๋ชจ๋ฆฌ ์ฐจ์ง€๊ฐ€ ํฌ๋ฏ€๋กœ ์“ฐ์ง€ ์•Š๋Š” ๋ฐฉ๋ฒ•์ด๋‹ค. private ๊ฐ™์€ ์ ‘๊ทผ ์ œํ•œ์ž๋„ ์—†๋‹ค.
	public String memberPost (LoginVO member) { // ๋ชจ๋ธ: LoginVO
		System.out.println(member); //member.toString()์ด ์ƒ๋žต๋œ ๊ฒƒ์ด๋‹ค.
		//#####์ƒˆ๋กœ ์ถ”๊ฐ€ํ•จ#####
		ls.memreg(member);
		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) { //member: login.jsp์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์˜จ๋‹ค.
		// HttpSession session = new HttpSession; ์œ„ ()์•ˆ์˜ HttpSession session์„ ์ง€์šฐ๊ณ  ๋Œ€์‹  ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•˜๋‹ค.
/*		System.out.println("contorller="+member);
		System.out.println(ls.login(member));*/
		
		session.setAttribute("login", ls.login(member)); //์œ„ member์˜ ์•„์ด๋””, ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ๊ฐ€์ ธ๊ฐ„๋‹ค.
		
		if (ls.login(member)==null) {
			return "member/login";
			} else {
				session.setAttribute("login", ls.login(member)); //null์ด ์•„๋‹ˆ๋ฉด ์„ธ์…˜์— ์ €์žฅํ•œ๋‹ค.
				return "redirect:/list"; 
				//์œ„๋Š” ์ฃผ์†Œ ์ •์˜ํ•ด์ค€ ๊ฒŒ ์—†์–ด์„œ ์ฒ˜์Œ์— ์—๋Ÿฌ๋‚จ (board์˜ list.jsp๋กœ ๊ฐ€๋Š” ๊ฒŒ ์•„๋‹Œ ๋“ฏ -> member ํด๋”์˜ list.jsp ๋”ฐ๋กœ ๋งŒ๋“ค์–ด์•ผํ•œ๋‹ค.)
				//return "/board/list"; //๊ฒŒ์‹œํŒ ๋ฆฌ์ŠคํŠธ ๋ณด๊ธฐ ์œ„ํ•ด ๋‚ด๊ฐ€ ์•„๋ž˜ ์ฝ”๋“œ๋กœ ๋ฐ”๊ฟˆ. 
			}
		}
	
	// ํšŒ์›๊ด€๋ฆฌํŽ˜์ด์ง€
	@RequestMapping(value = "/list", method = RequestMethod.GET) //value:์š”์ฒญ๋ฐ›์„ url์„ ์„ค์ •ํ•˜๊ฒŒ ๋œ๋‹ค.
	public String mergerlist(Model model) { //์ฒ˜์Œ ์—๋Ÿฌ ๋ฉ”์‹œ์ง€ ์žˆ๋Š” ์ด์œ : ๋ฆฌํ„ด์ด ์žˆ์–ด์•ผ ํ•œ๋‹ค.
		//model.addAttribute(String name, Object value);
		//: value ๊ฐ์ฒด๋ฅผ name ์ด๋ฆ„์œผ๋กœ ์ถ”๊ฐ€ํ•จ. ๋ทฐ ์ฝ”๋“œ์—์„œ๋Š” name์œผ๋กœ ์ง€์ •ํ•œ ์ด๋ฆ„์„ ํ†ตํ•ด์„œ value๋ฅผ ์‚ฌ์šฉํ•จ.
		model.addAttribute("memlist", ls.memlist()); //ls.memlist()->memlist:๋ฐ์ดํ„ฐ ์ €์žฅํ•ด์„œ ๋„˜๊ธด๋‹ค.
		return "member/list"; //memberํด๋”์˜ list.jsp ๋งŒ๋“ค์–ด์ค€๋‹ค. /WEB-INF/views/member/list.jsp
	}
	
	//๊ณผ์ œ: ๊ฐœ์ธ ํšŒ์› ์ƒ์„ธ ์ •๋ณด ์กฐํšŒ ํŽ˜์ด์ง€
	@RequestMapping(value = "/listDetail", method = RequestMethod.GET) //list.jsp์˜ <a>ํƒœ๊ทธ์˜ ?์— ๋Œ๊ณ ๊ฐ€๋Š” ๊ฒŒ ์žˆ์œผ๋ฉด ๊ฐ™์ด ๊ฐ€๊ฒŒ ๋œ๋‹ค.
	//public String memberDetail(String id) {//๋งค๊ฐœ ๋ณ€์ˆ˜๋ฅผ ๋‘”๋‹ค. 
	public String memberDetail(LoginVO login, Model model) {
		// ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ (LoginVO login)๋กœ๋„ ํ•ด๋„ ๋œ๋‹ค (private String id๊ฐ€ ์ •์˜๋˜์–ด์žˆ์œผ๋ฏ€๋กœ) //์ฒ˜์Œ ์—๋Ÿฌ ๋ฉ”์‹œ์ง€: ๋ฆฌํ„ด์ด ์žˆ์–ด์•ผ ํ•œ๋‹ค.
		//ls.memlistDetail(id);//[#๋ฉ”์„œ๋“œ ํ˜ธ์ถœ#]
		model.addAttribute("listDetail", ls.memlistDetail(login));
		return "member/listDetail"; // /WEB-INF/views/member/listDetail.jsp
	}
	
	//ํšŒ์› ์ •๋ณด ์ˆ˜์ • ๋ฐ ์‚ญ์ œ
}



์ฝ”๋“œ 4) LoginService.java

package org.hj.service; //LoginService๋Š” ํŒจํ‚ค์ง€ ์•ˆ์— ์žˆ๋‹ค. 

import java.util.ArrayList;

import org.hj.model.LoginVO;
//์ธํ„ฐํŽ˜์ด์Šค(๋„๋ฉด): ์„ค๊ณ„ํ•œ๋‹ค. ๋งค๊ฐœ ๋ณ€์ˆ˜ ๋“ฑ ํŒŒ์•…ํ•ด์„œ ๋ฏธ๋ฆฌ ์„ค๊ณ„ํ•ด๋‘” ๊ณณ์ด๋‹ค.
public interface LoginService { //์ธํ„ฐํŽ˜์ด์Šค๊ฐ€ new()๋ฅผ ํ•  ์ˆ˜ ์—†์œผ๋ฏ€๋กœ MemberController.java์— @Autowired๋ฅผ ์„ค์ •
	//#####์ƒˆ๋กœ ์ถ”๊ฐ€ํ•จ#####
	public void memreg(LoginVO member); //์ถ”์ƒ๋ฉ”์„œ๋“œ์ด๋ฏ€๋กœ LoginServiceImpl.java์—์„œ ๊ตฌํ˜„ํ•œ๋‹ค.
	public LoginVO login(LoginVO member);
	public ArrayList<LoginVO> memlist(); //๋น„์›Œ๋‘”๋‹ค.
	//public LoginVO memlistDetail(String id);//[#๋ฉ”์„œ๋“œ ์„ ์–ธ#] //๋ฆฌํ„ด ํƒ€์ž…์„ ๋ชจ๋ฅด๊ฒ ์œผ๋ฉด ์šฐ์„  void๋กœ ๋‘”๋‹ค.
	public LoginVO memlistDetail(LoginVO login);
}



์ฝ”๋“œ 5) LoginServiceImpl.java

package org.hj.service; //ํŒŒ์ผ์˜ ์œ„์น˜๋Š” ํŒจํ‚ค์ง€๋ฅผ ๋ณด๋ฉด ์•Œ ์ˆ˜ ์žˆ๋‹ค.

import java.util.ArrayList;

import org.hj.mapper.LoginMapper;
import org.hj.model.LoginVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
//๊ตฌํ˜„ํ•œ๋‹ค.
@Service
public class LoginServiceImpl implements LoginService { //LoginService๋ฅผ ๊ตฌํ˜„ํ•œ๋‹ค.
	
	@Autowired
	LoginMapper lm; //LoginMapper lm = new LoginMapper();
	
	//#####์ƒˆ๋กœ ์ถ”๊ฐ€ํ•จ#####
	public void memreg(LoginVO member) { //์ถ”์ƒ๋ฉ”์„œ๋“œ LoginSerive.java๋ฅผ ๊ตฌํ˜„ํ•œ๋‹ค.
		lm.memreg(member);
	};
	
	public LoginVO login (LoginVO member) {
		System.out.println("service="+member);
		System.out.println("service return="+lm.login(member));
		return lm.login(member);
	}
//๋น„์ฆˆ๋‹ˆ์Šค๊นŒ์ง€ ๋๋‚ฌ๋‹ค. ์„œ๋น„์Šค -> DB ์—ฐ๊ฒฐํ•ด์•ผํ•œ๋‹ค.	
	public ArrayList<LoginVO> memlist() {
		
		return lm.memlist();
		
	}
	
	//public LoginVO memlistDetail(String id) {
	public LoginVO memlistDetail(LoginVO login) {
		
		return lm.memlistDetail(login);//[#๋ฉ”์„œ๋“œ ํ˜ธ์ถœ#]
		
	}
}



์ฝ”๋“œ 6) LoginMapper.java

package org.hj.mapper;

import java.util.ArrayList;

import org.hj.model.LoginVO;

public interface LoginMapper {
	//#####์ƒˆ๋กœ ์ถ”๊ฐ€ํ•จ#####
	public void memreg(LoginVO member);
	public LoginVO login(LoginVO member);
	public ArrayList<LoginVO> memlist();
	//public LoginVO memlistDetail(String id);//[#๋ฉ”์„œ๋“œ ์„ ์–ธ#]
	public LoginVO memlistDetail(LoginVO login);
}



์ฝ”๋“œ 7) LoginMapper.xml

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE mapper
  PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

  
  <mapper namespace="org.hj.mapper.LoginMapper">
  
  <!-- #####์ƒˆ๋กœ ์ถ”๊ฐ€ํ•จ##### -->
	<insert id="memreg"> <!-- LoginMapper.java์˜ memreg๋ฅผ ์—ฐ๊ฒฐํ•œ๋‹ค. insertํƒœ๊ทธ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.-->
  		insert into member (id, password, name, birthday, gender)
  		values(#{id},#{password},#{name},sysdate(),"f");
	</insert>
  
  	
	<select id="login" resultType="org.hj.model.LoginVO"> <!-- ๊ตฌํ˜„๋ฉ”์„œ๋“œ๊ฐ€ ์—†์œผ๋‹ˆ๊นŒ return์ด ์—†๋‹ค. -->
	<!-- ๋Œ€์‹  select๋Š” resultType์ด๋ผ๋Š” ํƒ€์ž…์ด return ์—ญํ• ์ด๋‹ค. -->
  		select id, password <!-- ๊ฒฐ๊ณผ๋Š” ํ•œ ๊ฑด์ด๋‹ค. id๋Š” ๊ธฐ๋ณธํ‚ค์ด๋‹ค. -->
  		from member
  		where id=#{id} and password=#{password}
	</select>
  	
  	<!-- ํšŒ์› ๊ด€๋ฆฌ ๋ฆฌ์ŠคํŠธ ํŽ˜์ด์ง€ -->
  	<select id="memlist" resultType="org.hj.model.LoginVO">
  	
  		select * from member <!-- ๊ฒฐ๊ณผ๋Š” ์—ฌ๋Ÿฌ ๊ฑด์ด๋‹ค. LoginVOํƒ€์ž…์˜ ๋ฐฐ์—ด์ด ํ•„์š”ํ•˜๋‹ค!-->
  		  		
  	</select>
  	
	<!-- ๊ฐœ์ธ ํšŒ์› ์ƒ์„ธ ์ •๋ณด ์กฐํšŒ ํŽ˜์ด์ง€ -->
  	<select id="memlistDetail" resultType="org.hj.model.LoginVO"> <!-- select๋œ ๊ฒฐ๊ณผ๋ฅผ org.hj.model.LoginVO ํด๋ž˜์Šค์— ์ €์žฅํ•œ๋‹ค. -->
  	
  		select * 
  		from member 
  		where id=#{id} <!-- id๋ฅผ ๋Œ๊ณ ์˜ค๋Š” ์ž‘์—…์ด ํ•„์š”ํ•˜๋‹ค. ๋ฉ”์„œ๋“œ์˜ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๊ฐ€๋Šฅํ•˜๋‹ค. ์ด ๋‘ ๊ฐ€์ง€ ๋ฐฉ๋ฒ•: String id, LoginVO๋กœ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ๋‹ค.-->
  		  		
  	</select>
  </mapper>

๊ฒฐ๊ณผ) ํšŒ์› ๋ฆฌ์ŠคํŠธ๋กœ ๊ฐ„๋‹ค.

ํšŒ์› ๋ฆฌ์ŠคํŠธ

์•„์ด๋””๋ฅผ ํด๋ฆญํ•˜๋ฉด URL์— ?id="์„ ํƒํ•œ ์•„์ด๋””"๊ฐ€ ๋œจ๊ณ  ์ƒ์„ธ ํŽ˜์ด์ง€๊ฐ€ ๋œฌ๋‹ค.

ํšŒ์› ์ •๋ณด ์ƒ์„ธ ํŽ˜์ด์ง€ ํ™”๋ฉด


 

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