2016년 11월 4일 금요일

73day / Framework / springMVC 회원관리 Project

springMVC 회원관리 시스템
  • 주요업무
기능적 요구사항
  1. 아이디로 회원검색
  2. 주소로 회원검색
  3. 로그인
  4. 카페
  5. 로그아웃
  6. 회원수정
  7. 회원가입(아이디 중복확인 Ajax적용) 

비기능적 요구사항
  1. 인증여부에 따른 화면 제공
  2. 카페 / 로그아웃 / 회원수정
  3. 부분적으로 Ajax서비스
  • 작업단계
의존 라이브러리
maven - pom.xml, ajax, jsoon

web.xml
dispatcher-servlet.xml (Spring)
SqlSessionConfig.xml (MyBatis)
member.xml (MyBstis)

  • 구현
컨트롤러 테스트(간단 링크)
MemberController MemberDAO
MemberDAOImpl




  • distpatcher-servlet.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:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd">
    <!-- ViewResolver -->
    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/" />
        <property name="suffix" value=".jsp" />
    </bean>
    <!-- 어노테이션 IOC 설정 및 MVC 설정 -->
    <context:component-scan base-package="org.kosta.springmvc9" />
    <mvc:annotation-driven />
    <!-- dbcp와 MyBatis 설정 -->
    <bean id="dbcp" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
        <property name="url" value="jdbc:oracle:thin:@127.0.0.1:1521:xe" />
        <property name="username" value="scott" />
        <property name="password" value="tiger" />
        <property name="maxActive" value="20" />
    </bean>
    <!-- spring과 mybatis 연동설정 -->
    <!-- dbcp와 mybatis 설정파일 위치를 주입 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dbcp" />
        <property name="configLocation"
            value="classpath:mybatis/config/SqlSessionConfig.xml" />
    </bean>
    <!-- mybatis의 효율적인 개발을 지원하고 선언적 방식의 트랜잭션 지원 -->
    <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
        <constructor-arg ref="sqlSessionFactory" />
    </bean>
</beans>
cs


  • index.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 PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    <c:if test="${not empty registerOk}">
        <script>
            alert("회원가입을 축하합니다");
        </script>
    </c:if>
    <h3>springmvc회원관리</h3>
    <a href="test.do">controller test</a> <%-- result.jsp --%>
    <br />
    <form action="findMemberById.do">
        아이디 <input type="text" name="id">
        <input type="submit" value="검색">
    </form>
    <hr />
<%--
    findMemberById_ok.jsp     --> 검색결과
    findMemberById_fail.jsp     --> alert
 --%>
     <a href="getAddressList.do">주소검색페이지로</a>
<%--
    addressList.jsp
    주소의 종류를 select option으로 제공
 --%> 
     <form action="findMemberByAddress.do">
        주소 <input type="text" name="address">
        <input type="submit" value="검색">
    </form>
    <hr />
    <c:choose>
        <c:when test="${empty sessionScope.mvo}">
            <form action="login.do" method="post">
                아이디 <input type="text" name="id"> <br />
                비밀번호<input type="password" name="password"><br />
                <input type="submit" value="로그인" />
            </form>
            <hr>
            <a href="register.jsp">회원가입</a>
            <br>
        </c:when>
        <c:otherwise>
    <%--
    cafe.jsp 인증상태만 입장가능
    XX에 사는 XXX님 카페입장환영
    총회원수 5명
 --%> 
    <a href="enterCafe.do">카페가기</a>
    <a href="logout.do">로그아웃</a>
<h3>${sessionScope.mvo.name}고객님 방문해주셔서 감사합니다!</h3>
    <a href="updateMember.do">회원정보수정</a>    
        </c:otherwise>
    </c:choose>
</body>
</html>





cs


  • Controller
@Controller
public class MemberController {
    @Resource
    private MemberDAO memberDAO;
    @RequestMapping("test.do")
    public String result() {
        return "result";
    }
    @RequestMapping("findMemberById.do")
    public ModelAndView findMemberById(String id) {
        MemberVO vo = memberDAO.findMemberById(id);
        if (vo == null) {
            return new ModelAndView("findMemberById_fail");
        } else {
            return new ModelAndView("findMemberById_ok""vo", vo);
        }
    }
    @RequestMapping("findMemberByAddress.do")
    public ModelAndView findMemberByAddress(String address) {
        List<MemberVO> lvo = memberDAO.findMemberByAddress(address);
        if (lvo.isEmpty()) {
            return new ModelAndView("findMemberByAddress_fail");
        } else {
            return new ModelAndView("findMemberByAddress_ok""lvo", lvo);
        }
    }
    @RequestMapping("getAddressList.do")
    public ModelAndView getAddressList() {
        List<MemberVO> lvo = memberDAO.getAddressList();
        if (lvo.isEmpty()) {
            return new ModelAndView("error");
        } else {
            return new ModelAndView("addressList""lvo", lvo);
        }
    }
    @RequestMapping("findMemberByAddressAjax.do")
    @ResponseBody
    public List<MemberVO> findMemberByAddressAjax(String address) {
        return memberDAO.findMemberByAddress(address);
    }
    @RequestMapping("idCheckAjax.do")
    @ResponseBody
    public int idCheckAjax(String id) {
        int data = memberDAO.idCheckAjax(id);
        return data;
    }
    @RequestMapping("login.do")
    public ModelAndView login(MemberVO mvo, HttpServletRequest request) {
        MemberVO loginVo = memberDAO.login(mvo, request);
        if (loginVo == null) {
            return new ModelAndView("error");
        } else {
            HttpSession session = request.getSession();
            session.setAttribute("mvo", loginVo);
            return new ModelAndView("index""vo", loginVo);
        }
    }
    @RequestMapping("logout.do")
    public ModelAndView logout(HttpServletRequest request) {
        HttpSession session = request.getSession(false);
        if (session != null) {
            session.invalidate();
            return new ModelAndView("index");
        } else {
            return new ModelAndView("error");
        }
    }
    @RequestMapping("enterCafe.do")
    public ModelAndView enterCafe(HttpServletRequest request) {
        HttpSession session = request.getSession(false);
        int count = memberDAO.count();
        if (session == null) {
            return new ModelAndView("index");
        } else {
            return new ModelAndView("cafe""count", count);
        }
    }
    @RequestMapping("register.do")
    public ModelAndView register(MemberVO mvo) {
        memberDAO.register(mvo);
        String msg = "ok";
        return new ModelAndView("redirect:index.jsp""registerOk", msg);
    }
    @RequestMapping("updateMember.do")
    public ModelAndView updateMember(MemberVO mvo, HttpServletRequest request) {
        HttpSession session = request.getSession(false);
        if (session == null) {
            return new ModelAndView("index");
        } else {
            System.out.println(mvo.getId());
            return new ModelAndView("update");
        }
    }
    @RequestMapping("update.do")
    public ModelAndView update(MemberVO mvo) {
        System.out.println(mvo);
        memberDAO.update(mvo);
        return new ModelAndView("redirect:index.jsp");
    }
}
cs



  • DAO & DAOImpl
@Repository
public interface MemberDAO {
    MemberVO findMemberById(String id);
    List<MemberVO> findMemberByAddress(String address);
    List<MemberVO> getAddressList();
    MemberVO login(MemberVO mvo, HttpServletRequest request);
    
    void register(MemberVO mvo);
    
    int count();
    
    void update(MemberVO mvo);
    int idCheckAjax(String id);
}
@Repository
public class MemberDAOImpl implements MemberDAO {
    @Resource
    private SqlSessionTemplate template;
    @Override
    public MemberVO findMemberById(String id) {
        return template.selectOne("member.findMemberById", id);
    }
    @Override
    public List<MemberVO> getAddressList() {
        return template.selectList("member.getAddressList");
    }
    
    @Override
    public List<MemberVO> findMemberByAddress(String address) {
        return template.selectList("member.findMemberByAddress", address);
    }
    public MemberVO login(MemberVO mvo, HttpServletRequest request) {
        return template.selectOne("member.login", mvo);
    }
    @Override
    public void register(MemberVO mvo) {
        template.insert("member.register", mvo);
    }
    @Override
    public int count() {
        return template.selectOne("member.count");
    }
    @Override
    public void update(MemberVO mvo) {
        template.update("member.update", mvo);
    }
    @Override
    public int idCheckAjax(String id) {
        return template.selectOne("member.idCheckAjax", id);
    }
}
cs

회원관리 project는
Java SE 시절 Array / Array List / Map / Exeption / IO
Servlt Model1 / JSP & Servlet Model2 에서도 겪어봤는 데
적용기술이 바뀐 것만으로도 또 새롭고 어려웠다
확실히 spring & MyBatis 등 새로운 환경에 설정시간과 오류시간은 늘었지만
실질적인 코딩 시간과 길이 그리고 파일의 개수까지 많이 줄었다.
앞으로 배울 기술도 중요하지만 오늘만큼은 복습의 중요성이 와닿는다

0 개의 댓글:

댓글 쓰기