본문 바로가기

안녕하세요!

프로그래밍 언어/JSP

JSP_22-11-10

[ src/main/webapp/WEB-INF/views/common/header.jsp ]

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<c:set var="contextPath" value="${pageContext.request.contextPath}"/>
<% request.setCharacterEncoding("UTF-8"); %>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap-theme.min.css">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/js/bootstrap.min.js"></script>	

<% /*
	sticky-top은 상단 공간을 차지하면서 위에 고정하고,
	fixed-top은 상단 공간을 차지하지 않고 위에 고정된다.
	일부 내용이 상단 메뉴바에 가려져서 보이지 않을 수 있다.
	*/
%>

<nav class="navbar navbar-inverse navbar-sticky-top">
	<div class="container-fluid"> 
		<div class="navbar-header">
			<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#myNavbar">
				<span class="icon-bar"></span>
				<span class="icon-bar"></span>
				<span class="icon-bar"></span>
			</button>
			<a class="navbar-brand" href="${contextPath}/main/do">이제영의 쇼핑몰</a>
		</div>
		<!-- 실질적인 메뉴를 나열한다. -->
		<div class="collapse navbar-collapse" id="myNavbar">
			<ul class="nav navbar-nav navbar-left"> <!-- 상단 네비게이션 바의 왼쪽에 붙여서 보여주기 -->
				<li>
					<form class="navbar-form navbar-right" method="post" action="${contextPath}/member/login.do">
						<c:choose>
							<c:when test="${isLogOn == true && member != null}">
								<p class="navbar-text"><b>${member.name}님, 즐거운 시간되십니오.</b></p>
								<a href="${contextPath}/member/logout.do" class="btn btn-danger">
								<span class="gylphicon gylphicon-log-out"></span> 로그아웃</a>
							</c:when>
							<c:otherwise>
								<div class="form-group">
									<input type="text" class="form-control" name="id" size="12" maxlength="12" placeholder="아이디">
									<input type="password" class="form-control" name="pwd" size="12" maxlength="12" placeholder="비밀번호">
								</div>
								<button type="submit" class="btn btn-primary">
									<span class="glyphicon glyphicon-log-in"></span> 로그인
								</button>
							</c:otherwise>
						</c:choose>
					</form>
				</li>
				<li class="dropdown">
					<a class="dropdown-toggle" data-toggle="dropdown" href="#">회원관리 <span class="caret"></span></a>
					<ul class="dropdown-menu">
						<li><a href="${contextPath}/member/loginForm.do">로그인</a></li>
					</ul>
				</li>
			</ul>
		</div>
	</div>


</nav>

 

[ src/main/webapp/WEB-INF/views/common/footer.jsp ]

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>

<div class="container text-center">
	<p>E-mail : admin@testhop.com</p>
	<p>회사주소 : 서울특별시 종로구 관철동 좋은빌딩 305호</p>
	<p>찾아오시는 길 : <a href="#">약도</a></p>
</div>

 

[ src/main/webapp/WEB-INF/views/common/topMenu.jsp ]

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<c:set var="contextPath" value="${pageContext.request.contextPath}"/>
<%	request.setCharacterEncoding("UTF-8"); %>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap-theme.min.css">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/js/bootstrap.min.js"></script>	

<%	/*
	sticky-top은 상단 공간을 차지하면서 위에 고정하고,
	fixed-top은 상단 공간을 차지하지 않고 위에 고정된다.
	일부 내용이 상단 메뉴바에 가려져서 보이지 않을 수 있다.
	*/
%>
<nav class="navbar navbar-inverse navbar-sticky-top">
	<div class="container-fluid">
		<div class="navbar-header">
			<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#myNavbar">
				<span class="icon-bar"></span>
				<span class="icon-bar"></span>
				<span class="icon-bar"></span>
			</button>
			<a class="navbar-brand" href="${contextPath}/main.do">쇼핑몰</a>
		</div>
		<!-- 실질적인 메뉴를 나열한다. -->
		<div class="collapse navbar-collapse" id="myNavbar">
			<ul class="nav navbar-nav navbar-left">
				<li>
					<form class="navbar-form navbar-right" method="post" action="${contextPath}/member/login.do">
						<c:choose>
							<c:when test="${isLogOn == true && member != null}">
								<p class="navbar-text"><b>${member.name}님, 즐거운 시간되십시요.</b></p>
								<a href="${contextPath}/member/logout.do" class="btn btn-danger">
								<span class="glyphicon glyphicon-log-out"></span> 로그아웃</a>
							</c:when>
							<c:otherwise>
								<div class="form-group">
									<input type="text"     class="form-control" name="id"  size="12" maxlength="12" placeholder="아이디"/>
									<input type="password" class="form-control" name="pwd" size="12" maxlength="12" placeholder="비밀번호"/>
								</div>
								<button type="submit" class="btn btn-primary">
									<span class="glyphicon glyphicon-log-in"></span> 로그인
								</button>
							</c:otherwise>
						</c:choose>
					</form>
				</li>
				<li class="dropdown">
					<a class="dropdown-toggle" data-toggle="dropdown" href="#">회원관리 <span class="caret"></span></a>
					<ul class="dropdown-menu">
						<li><a href="${contextPath}/member/loginForm.do">로그인</a></li>
					</ul>
				</li>
			</ul>
		</div>
	</div>
</nav>

 

[ src/main/webapp/WEB-INF/views/main.jsp ] 

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c"	uri="http://java.sun.com/jsp/jstl/core" %>
<c:set var="contextPath" value="${pageContext.request.contextPath}"/>
<%  request.setCharacterEncoding("UTF-8"); %>

<!DOCTYPE html>
<html>
<head>
	<meta charset="UTF-8">
	<title>메인화면</title>
	<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
	<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css">
	<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap-theme.min.css">
	<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/js/bootstrap.min.js"></script>	
</head>
<body>

<!-- 메뉴바 -->
<jsp:include page="./common/topMenu.jsp" flush="false"/>

<!-- 메뉴바 하단에 페이지의 소개를 보여준다. -->
<div class="container">
	<div class="jumbotron">
		<div class="container text-center">
			<h1>도서 쇼핑몰</h1>
			<p>방문을 환영합니다.</p>
		</div>
	</div>
</div>

<!-- 화면 중앙에 이미지를 보여준다. -->
<div class="container">
	<div id="myCarousel" class="carousel slide" data-ride="carousel">
		<!-- Indicators -->
		<ol class="carousel-indicators">
			<li data-target="#myCarousel" data-slide-to="0" class="active"></li>
			<li data-target="#myCarousel" data-slide-to="1"></li>
			<li data-target="#myCarousel" data-slide-to="2"></li>
		</ol>
		
		<!-- Wrapper for slides -->
		<div class="carousel-inner">
			<div class="item active">
				<!-- class="img-responsive center-block" : 반응형 이미지를 가운데 정렬한다. -->
				<img class="img-responsive center-block" src="${contextPath}/resources/images/books01.jpg" width="80%" height="200px"/>
				<!-- carousel에 설명을 달아준다. -->
				<div class="carousel-caption">
					<h2>책방 내부 시설 1</h2>
				</div>
			</div> 
			<div class="item">
				<!-- class="img-responsive center-block" : 반응형 이미지를 가운데 정렬한다. -->
				<img class="img-responsive center-block" src="${contextPath}/resources/images/books02.jpg" width="80%" height="200px"/>
				<!-- carousel에 설명을 달아준다. -->
				<div class="carousel-caption">
					<h2>책방 내부 시설 2</h2>
				</div>
			</div> 
			<div class="item">
				<!-- class="img-responsive center-block" : 반응형 이미지를 가운데 정렬한다. -->
				<img class="img-responsive center-block" src="${contextPath}/resources/images/books03.jpg" width="80%" height="200px"/>
				<!-- carousel에 설명을 달아준다. -->
				<div class="carousel-caption">
					<h2>책방 내부 시설 3</h2>
				</div>
			</div> 
		</div>
		
		<!-- 좌측, 우측으로 그림을 움직일 수 있도록 좌/우버튼을 설정한다. -->
		<a class="left  carousel-control" href="#myCarousel" data-slide="prev">
			<span class="glyphicon glyphicon-chevron-left"></span>
		</a>
		<a class="right carousel-control" href="#myCarousel" data-slide="next">
			<span class="glyphicon glyphicon-chevron-right"></span>
		</a>
	</div>
</div>

<!-- 푸터영역 -->
<jsp:include page="./common/footer.jsp" flush="false"/>

</body>
</html>

 

[ src/main/java/com/edu/member/controller/MemberController.java ]

<?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="com.edu.member">

	<resultMap id = "memberResult" type = "com.edu.member.vo.MemberVO">
		<result property = "id"			column="id"/>
		<result property = "pwd"		column="pwd"/>
		<result property = "name"		column="name"/>
		<result property = "email"		column="email"/>
		<result property = "joinDate"	column="joinDate"/>
	</resultMap>

	<!-- 로그인 처리 -->
	<select id="loginByID" parameterType="com.edu.member.vo.MemberVO" resultType="com.edu.member.vo.MemberVO">
		<![CDATA[
			SELECT		*
			FROM		T_MEMBER
			WHERE		ID = #{id}
		]]>
		
	</select>

</mapper>

 

[ src/main/java/com/edu/member/controller/MemberControllerImpl.java ]

package com.edu.member.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.edu.member.service.MemberService;
import com.edu.member.vo.MemberVO;

//-------------------------------------------------------------------------------------
// public class MemberControllerImpl implements MemberController
//-------------------------------------------------------------------------------------
@Controller
@RequestMapping("/member") // url에서 /member로 시작하는 요청들을 처리한느 컨트롤러
public class MemberControllerImpl implements MemberController {

	private static final Logger logger = LoggerFactory.getLogger(MemberControllerImpl.class);
	
	//-------------------------------------------------------------------------------------
	// @Inject 	  : Java에서 지원하는 어노테이션, 특정 Framework에 종속적이지 않다.
	// @Autowired : Spring에서 지원하는 어노테이션
	//-------------------------------------------------------------------------------------
	@Autowired
	private MemberVO memberVO;
	
	// private MemberService memberService = new MemberService();
	@Autowired
	private MemberService memberService;
	
	//-------------------------------------------------------------------------------------
	// 로그인 처리
	// public ModelAndView login
	//-------------------------------------------------------------------------------------
	@Override
	@RequestMapping(value = "/login.do", method = RequestMethod.POST)
	public ModelAndView login(@ModelAttribute("member") MemberVO member, RedirectAttributes rAttr, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		
		logger.info("MemberControllImpl login() 시작......");
		System.out.println("로그인 정보 => " + member.getId() + " : " + member.getPwd());
		
		ModelAndView mav = new ModelAndView();
		
		// 로그인한 정보를 가지고 데이터베이스에 존재하는지 처리를 하고 그 결과를 가져온다.
		memberVO = memberService.login(member);
		System.out.println("로그인 처리 결과 ==> " + memberVO);
		
		// 로그인한 정보가 데이터베이스에 존재하는지에 따라 처리를 다르게 한다.
		if(memberVO != null) { // 로그인 정보에 해당하는 자료가 있으면
			
			
			if(member.getPwd().equals(memberVO.getPwd())) {
				// 아이디와 비밀번호가 일치하면 세션을 발급한다.
				HttpSession session = request.getSession();
				session.setAttribute("member", memberVO);
				session.setAttribute("isLogOn", true);
				// mav.setViewName("redirect:/member/listMembers.do");
				mav.setViewName("redirect:/main.do");	// 메인화면으로 이동
				
				
			} else { // 아이디는 있지만 비밀번호가 틀린 경우
				rAttr.addAttribute("result", "PasswordFailed");
				mav.setViewName("redirect:/member/loginForm.do");
			}
			
			
		} else { // 로그인한 아이디가 존재하지 않으면
			// 로그인 실패 메시지를 가지고 로그인화면으로 이동한다.
			rAttr.addAttribute("result", "loginFailed");
			mav.setViewName("redirect:/member/loginForm.do");
		}
		
		return mav;
				
	} // End - public ModelAndView login

} // End - public class MemberControllerImpl implements MemberController

 

[ src/main/java/com/edu/member/dao/MemberDAO.java ]

package com.edu.member.dao;

import org.springframework.dao.DataAccessException;

import com.edu.member.vo.MemberVO;

//-------------------------------------------------------------------------------------
// 회원정보 DAO
//-------------------------------------------------------------------------------------
public interface MemberDAO {

	//-------------------------------------------------------------------------------------
	// 로그인 처리
	//-------------------------------------------------------------------------------------
	public MemberVO loginByID(MemberVO memberVO) throws DataAccessException;
	
	
	
} // End - public interface MemberDAO

 

[ src/main/java/com/edu/member/dao/MemberDAOImpl.java ]

package com.edu.member.dao;

import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Repository;

import com.edu.member.vo.MemberVO;

//-------------------------------------------------------------------------------------
// 회원정보 DAO
//-------------------------------------------------------------------------------------
@Repository("memberDAO")
public class MemberDAOImpl implements MemberDAO {

	@Autowired
	private SqlSession sqlSession;
	
	private static final String Namespace = "com.edu.member";
	
	//-------------------------------------------------------------------------------------
	// 로그인 처리
	//-------------------------------------------------------------------------------------
	@Override
	public MemberVO loginByID(MemberVO memberVO) throws DataAccessException {
		
		MemberVO memVO = sqlSession.selectOne(Namespace + ".loginByID", memberVO); // memberVO 데이터 가져와서 처리한 후
		
		return memVO; // memVO 보여주기
	}

}

 

[ src/main/java/com/edu/member/service/MemberService.java ]

package com.edu.member.service;

import org.springframework.dao.DataAccessException;

import com.edu.member.vo.MemberVO;

//-------------------------------------------------------------------------------------
// 회원정보 서비스
//-------------------------------------------------------------------------------------
public interface MemberService {
	
	//-------------------------------------------------------------------------------------
	// 로그인 처리
	//-------------------------------------------------------------------------------------
	public MemberVO login(MemberVO memberVO) throws DataAccessException;

} // End - public interface MemberService

 

[ src/main/java/com/edu/member/service/MemberServiceImpl.java ]

package com.edu.member.service;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;

import com.edu.member.dao.MemberDAO;
import com.edu.member.vo.MemberVO;

//-------------------------------------------------------------------------------------
// public class MemberServiceImpl implements MemberService
//-------------------------------------------------------------------------------------
@Service("memberService")
public class MemberServiceImpl implements MemberService {

	private static final Logger logger = LoggerFactory.getLogger(MemberServiceImpl.class);

	
	@Autowired
	private MemberDAO memberDAO;
	
	//-------------------------------------------------------------------------------------
	// 로그인 처리
	//-------------------------------------------------------------------------------------
	@Override
	public MemberVO login(MemberVO memberVO) throws DataAccessException {
		
		
		logger.info("MemberControllImpl login() 시작......");

		
		return memberDAO.loginByID(memberVO);
		
		
		
	} // End - public MemberVO login(MemberVO memberVO) throws DataAccessException

} // End - public class MemberServiceImpl implements MemberService

 

[ src/main/java/com/edu/member/vo ]

package com.edu.member.vo;

import java.sql.Date;

import org.springframework.stereotype.Component;

import lombok.Data;

// -------------------------------------------------------------------------------------
// 회원 정보
// VO : Value Object / DTO : Data Transfer Object
// @Component 어노테이션 이용하면 Bean Configutration 파일에 Bean으로 따로 등록하지 않아도 사용 가능
//                            기본적으로 타입 기반의 자동주입 어노테이션이다.
// -------------------------------------------------------------------------------------
@Component("memberVO")
@Data
public class MemberVO {

	private String  id;			// 사용자 아이디
	private String  pwd; 		// 사용자 비밀번호
	private String  name;		// 사용자 이름
	private String  email;		// 이메일
	private Date	joinDate;	// 가입일자
	
	public MemberVO() {} 		// 기본생성자
	public MemberVO(String id, String pwd, String name, String email) {
		this.id		= id;
		this.pwd	= pwd;
		this.name	= name;
		this.email	= email;
	}
	
} // End - public class MemberVO

 

[ src/main/resources/mapper/memberMapper.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="com.edu.member">

	<resultMap id = "memberResult" type = "com.edu.member.vo.MemberVO">
		<result property = "id"			column="id"/>
		<result property = "pwd"		column="pwd"/>
		<result property = "name"		column="name"/>
		<result property = "email"		column="email"/>
		<result property = "joinDate"	column="joinDate"/>
	</resultMap>

	<!-- 로그인 처리 -->
	<select id="loginByID" parameterType="com.edu.member.vo.MemberVO" resultType="com.edu.member.vo.MemberVO">
		<![CDATA[
			SELECT		*
			FROM		T_MEMBER
			WHERE		ID = #{id}
		]]>
		
	</select>

</mapper>

 

728x90
반응형

'프로그래밍 언어 > JSP' 카테고리의 다른 글

JSP_22-11-11(2)  (0) 2022.11.11
JSP_22-11-11  (0) 2022.11.11
JSP_22-11-09  (0) 2022.11.09
JSP_22-11-08  (0) 2022.11.08

loading