[Spring] 로그인,관리자만들기

 

 

AdminController.java => 컨트롤러   

AdminDAO. 인터페이스

java AdminDAOImpl.java  인터페이스 구현클랜스

.MemberVo.java 데이터 객체 

레코드 저장 admin.xml 매핑..sql
login.jsp admin.jsp 로그인

 

 

 

ADminDao

1
2
3
4
5
6
7
8
9
package com.example.spring05.model.member;
 
public interface AdminDao {
 
    String login(MemberVo vo);
    
    
}
 
cs

ADminDaoimpl.java 인터페이스 구현

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
package com.example.spring05.model.member;
 
import javax.inject.Inject;
 
import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Repository;
 
@Repository // 저장소
public class AdminDaolmpl implements AdminDao {
 
    
    @Inject  // 의존관계 주입
    SqlSession sqlSession;
 
    @Override
    public String login(MemberVo vo) {
        // 레코드 1개   네임스페이스.아이디
        return sqlSession.selectOne("admin.login", vo);
    }
    
    
    
    
}
 
cs

vo 객체

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
32
33
34
35
36
package com.example.spring05.model.member;
 
public class MemberVo {
 
    
    
    private String userid;
    private String passwd;
    private String name;
    
    
    
    @Override
    public String toString() {
        return "MemberVo [userid=" + userid + ", passwd=" + passwd + ", name=" + name + "]";
    }
    public String getUserid() {
        return userid;
    }
    public void setUserid(String userid) {
        this.userid = userid;
    }
    public String getPasswd() {
        return passwd;
    }
    public void setPasswd(String passwd) {
        this.passwd = passwd;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}
 
cs

resoyuces / mappers < 필수

 

//네임스페이스

controller

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package com.example.spring05.controller.member;
 
import javax.inject.Inject;
import javax.servlet.http.HttpSession;
 
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttribute;
import org.springframework.web.servlet.ModelAndView;
 
import com.example.spring05.model.member.AdminDao;
import com.example.spring05.model.member.MemberVo;
 
@Controller
@RequestMapping("/admin/*")
public class AdminController {
 
    @Inject // 의존관ㄱㅖ 가져옴!!
    AdminDao adminDao;
    
    
    
    @RequestMapping("login.do")
    public String login() {
        return "admin/login";
    }
    
    
    @RequestMapping("login_check.do")
    public ModelAndView login_check(MemberVo vo,HttpSession Session,ModelAndView modelAndView) {
        
        String name = adminDao.login(vo);
        
        if(name !=null ) {  // 로그인성공시
           Session.setAttribute("admin_name", name);
           Session.setAttribute("userid", vo.getUserid());
           Session.setAttribute("name", name);
           modelAndView.setViewName("admin/admin"); // 페이지이름
           modelAndView.addObject("message""success"); // 자료저장
           
           
        }else { // 로그인실패시
            modelAndView.setViewName("admin/login");
            modelAndView.addObject("message""error");
        }
        return modelAndView;
    }
    
    
    @RequestMapping("logout.do")
    public String logout(HttpSession session) {
        session.invalidate();// 세션 뺏음
        return "redirect:/admin/login.do?message=logout";
    }
}
 
cs

login

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<%@ 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>Insert title here</title>
<script src="http://code.jquery.com/jquery-3.6.0.min.js"></script>
 
<script>
 
$(function(){
    $("#btnLogin").click(function(){
        var userid=$("#userid").val();
        var passwd=$("#passwd").val();
        if(userid==""){
            alert("아이디를 입력하세요");
            $("#userid").focus();
            return;
        }
        if(passwd==""){
            alert("아이디를 입력하세요");
            $("#passwd").focus();
            return;
        }
        document.form1.action="/spring05/admin/login_check.do";
        document.form1.submit();
    })
})
 
</script>
 
</head>
<body>
 
<%@ include file="../include/menu.jsp" %>
<H2>관리자 로그인</H2>
<form name="form1" method="post">
<table border="1" width="700px">
 
<tr>
<Td>아이디</Td>
<Td><input id="userid" name="userid"></Td>
</tr>
 
<tr>
<Td>비밀번호</Td>
<Td><input type="password"  id="passwd" name="passwd"></Td>
</tr>
 
<tr>
<Td colspan="2" align="center">
<button type="button" id="btnLogin">로그인</button>
 
 
<c:if test="${message='error'}">
<div style="color:red">
아이디 또는 비밀번호가 일치 하지 않스비다
</div>
</c:if>
 
<c:if test="${param.message =='logout' }">
<div style="color:red">
로그아웃 됫습니다.
</div>
 
 
</c:if>
 
</Td>
 
</tr>
 
</table>
 
 
</form>
 
</body>
</html>
cs

menu 페이지

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
32
33
34
35
36
37
38
39
40
41
42
43
<%@ 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>
<a href="/spring05/upload/input.do">업로드테스트</a>
 
<div style="text-align:right;">
 
<c:choose>
 
<c:when test="${sessionScope.userid == null }">
 
<a href="/spring05/member/login.do">로그인</a> |
 
<a href="/spring05/admin/login.do">관리자 로그인</a>
 
</c:when>
 
<c:otherwise>
 
${sessionScope.name}님이 로그인중입니다.
 
<a href="/spring05/member/logout.do">로그아웃</a> |
 
<a href="/spring05/admin/logout.do">관리자 로그아웃</
 
a>
 
</c:otherwise>
 
</c:choose>
 
</div>
 
 
 
</body>
</html>
cs

admin 페이지

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
32
33
34
35
36
37
38
39
<%@ 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>Insert title here</title>
 
</head>
 
<body>
 
<%@ include file="../include/menu.jsp" %>
 
<c:if test="${message == 'success' }">
 
<h2>
 
${sessionScope.admin_name} ( ${sessionScope.userid}
 
)님
 
환영합니다.
 
</h2>
 
</c:if>
 
</body>
 
</html>
 
cs

젤중요한 데이터 객체랑 매퍼 연결 의존하는것 root-context.xml

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<?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:mybatis-spring="http://mybatis.org/schema/mybatis-spring"
    xsi:schemaLocation="http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring-1.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.1.xsd">
    
    <!-- Root Context: defines shared resources visible to all other web components -->
        
        
        <!--      bean 자바 객체
     id 변수명 class 자료형 -->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName"
            value="oracle.jdbc.driver.OracleDriver" />
        <property name="url"
value="jdbc:oracle:thin:@127.0.0.1:1521:oracle" />
        <property name="username" value="system" />
        <property name="password" value="1234" />
    </bean>
        
        
        
    <bean id="sqlSessionFactory"
        class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <!-- mybatis 설정파일의 위치 -->
        <property name="configLocation"
            value="classpath:/mybatis-config.xml" />
            <!-- mybatis mapper 파일의 경로 
            와일드카드 /** 모든 하위디렉토리   * 모든 이름
            -->
        <property name="mapperLocations"
            value="classpath:mappers/**/*.xml" />
    </bean>
 
    <bean id="sqlSession"  
        class="org.mybatis.spring.SqlSessionTemplate"
        destroy-method="clearCache">  <!-- auto close -->
        <constructor-arg name="sqlSessionFactory"
            ref="sqlSessionFactory" />
    </bean>
        
        
        
</beans>
 
cs