java spring

[Java Spring MVC] Maria DB와 MyBatis로 로그인 화면 만들기(2)

윤돌_99 2021. 2. 6. 12:36

<MainController.java>

 

나는 controller를 두개 만들었다. 이 controller는 프로젝트를 돌렸을 때 처음 시행되는 controller이다. 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
package com.spring.test.controller;
 
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
 
 
@Controller
public class MainController {
    
    @RequestMapping("/")
    public String getMain() {
        return "login"// View 이름 리턴
    }
}
cs

 

<BoardController.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
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package com.spring.test.controller;
 
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.request;
 
import java.io.IOException;
import java.util.List;
 
import javax.servlet.http.HttpServletRequest;
import javax.annotation.Resource;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
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.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.multipart.MultipartFile;
import javax.inject.Inject;
import com.spring.test.domain.BoardVO;
import com.spring.test.service.BoardService;
import javax.servlet.http.HttpSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
 
@Controller
@SessionAttributes("/Board")
public class BoardController {
    
    private static final Logger logger = LoggerFactory.getLogger(BoardController.class); 
    @Autowired
    private BoardService boardService;
    
    //회원가입 성공화면 가기
    @RequestMapping(value= "/login.do", method = RequestMethod.POST)
    public String login(BoardVO vo, HttpServletRequest req, RedirectAttributes rttr) throws Exception{
        logger.info("post login");
        
        HttpSession session = req.getSession();
        
        BoardVO login = boardService.login(vo);
        
        if(login == null) {
            session.setAttribute("member"null);
            rttr.addFlashAttribute("msg"false);
        }else {
            session.setAttribute("member", login);
        }
        
        return "redirect:/";
    }
    
    @RequestMapping("/loginback.do"
    public String loginback() throws Exception{
        return "login";
    }
    
    @RequestMapping(value = "/GoFindId.do")
    public String GofindId() throws Exception{
        return "findId";
    }
    
    @RequestMapping(value = "/FindId.do", method = RequestMethod.POST)
    public String findId(BoardVO vo, Model model) {
        
        BoardVO user = boardService.findIdBoard(vo);
        
        if(user == null) {
            model.addAttribute("check"1);
        }else {
            model.addAttribute("check"0);
            model.addAttribute("id", user.getId());
        }
        
        return "findId";
    }
    
    @RequestMapping(value = "/GoFindPassword.do")
    public String GoFindPassword() throws Exception{
        return "findPassword";
    }
    
    @RequestMapping(value = "/FindPassword.do", method = RequestMethod.POST)
    public String FindPassword(BoardVO vo, Model model) {
        
        BoardVO user = boardService.findPwBoard(vo);
        
        if(user == null) {
            model.addAttribute("check"1);
        }else {
            model.addAttribute("check"0);
            model.addAttribute("pw", user.getPw());
        }
        
        return "findPassword";
    }
    
    @RequestMapping(value = "/logout.do", method = RequestMethod.GET)
    public String logout(HttpSession session) throws Exception{
        session.invalidate();
        
        return "redirect:/";
    }
    
    @RequestMapping("/getBoardList.do")
    public String getBoardList(BoardVO vo, Model model) {
         List<BoardVO> boardList = boardService.getBoardList();
         
        // Model 정보 저장
        model.addAttribute("boardList",boardList);
        return "boardList"// View 이름 리턴
    }
    
    // 회원정보 등록
    @RequestMapping(value="/insertBoard.do"
    public String insertBoard(BoardVO vo) throws IOException { 
            boardService.insertBoard(vo); 
            return "login"
    }
    
    // 글 쓰기 페이지 이동
    @RequestMapping("/moveInsertBoard.do"
    public String moveInsertBoard()throws Exception{
        return "insertBoard";
    }
    
    // 글 수정 페이지로 이동
    @RequestMapping(value = "/updateBoardView.do")
    public String updateBoardView() throws Exception{
 
        return "updateBoardView";
    }
     
    // 글 수정
    @RequestMapping(value = "/updateBoard.do", method = RequestMethod.POST)
    public String updateBoard(HttpSession session, BoardVO vo ) throws Exception{
        boardService.updateBoard(vo);
        
        return "login";
    }
 
    // 글 삭제
    @RequestMapping("/deleteBoard.do")
    public String deleteBoard(BoardVO vo,HttpSession session) {
        boardService.deleteBoard(vo);
        session.invalidate();
        return "login";
    }
}
 
cs

 

<BoardDAO.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
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package com.spring.test.controller;
 
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.request;
 
import java.io.IOException;
import java.util.List;
 
import javax.servlet.http.HttpServletRequest;
import javax.annotation.Resource;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
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.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.multipart.MultipartFile;
import javax.inject.Inject;
import com.spring.test.domain.BoardVO;
import com.spring.test.service.BoardService;
import javax.servlet.http.HttpSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
 
@Controller
@SessionAttributes("/Board")
public class BoardController {
    
    private static final Logger logger = LoggerFactory.getLogger(BoardController.class); 
    @Autowired
    private BoardService boardService;
    
    //회원가입 성공화면 가기
    @RequestMapping(value= "/login.do", method = RequestMethod.POST)
    public String login(BoardVO vo, HttpServletRequest req, RedirectAttributes rttr) throws Exception{
        logger.info("post login");
        
        HttpSession session = req.getSession();
        
        BoardVO login = boardService.login(vo);
        
        if(login == null) {
            session.setAttribute("member"null);
            rttr.addFlashAttribute("msg"false);
        }else {
            session.setAttribute("member", login);
        }
        
        return "redirect:/";
    }
    
    @RequestMapping("/loginback.do"
    public String loginback() throws Exception{
        return "login";
    }
    
    @RequestMapping(value = "/GoFindId.do")
    public String GofindId() throws Exception{
        return "findId";
    }
    
    @RequestMapping(value = "/FindId.do", method = RequestMethod.POST)
    public String findId(BoardVO vo, Model model) {
        
        BoardVO user = boardService.findIdBoard(vo);
        
        if(user == null) {
            model.addAttribute("check"1);
        }else {
            model.addAttribute("check"0);
            model.addAttribute("id", user.getId());
        }
        
        return "findId";
    }
    
    @RequestMapping(value = "/GoFindPassword.do")
    public String GoFindPassword() throws Exception{
        return "findPassword";
    }
    
    @RequestMapping(value = "/FindPassword.do", method = RequestMethod.POST)
    public String FindPassword(BoardVO vo, Model model) {
        
        BoardVO user = boardService.findPwBoard(vo);
        
        if(user == null) {
            model.addAttribute("check"1);
        }else {
            model.addAttribute("check"0);
            model.addAttribute("pw", user.getPw());
        }
        
        return "findPassword";
    }
    
    @RequestMapping(value = "/logout.do", method = RequestMethod.GET)
    public String logout(HttpSession session) throws Exception{
        session.invalidate();
        
        return "redirect:/";
    }
    
    @RequestMapping("/getBoardList.do")
    public String getBoardList(BoardVO vo, Model model) {
         List<BoardVO> boardList = boardService.getBoardList();
         
        // Model 정보 저장
        model.addAttribute("boardList",boardList);
        return "boardList"// View 이름 리턴
    }
    
    // 회원정보 등록
    @RequestMapping(value="/insertBoard.do"
    public String insertBoard(BoardVO vo) throws IOException { 
            boardService.insertBoard(vo); 
            return "login"
    }
    
    // 글 쓰기 페이지 이동
    @RequestMapping("/moveInsertBoard.do"
    public String moveInsertBoard()throws Exception{
        return "insertBoard";
    }
    
    // 글 수정 페이지로 이동
    @RequestMapping(value = "/updateBoardView.do")
    public String updateBoardView() throws Exception{
 
        return "updateBoardView";
    }
     
    // 글 수정
    @RequestMapping(value = "/updateBoard.do", method = RequestMethod.POST)
    public String updateBoard(HttpSession session, BoardVO vo ) throws Exception{
        boardService.updateBoard(vo);
        
        return "login";
    }
 
    // 글 삭제
    @RequestMapping("/deleteBoard.do")
    public String deleteBoard(BoardVO vo,HttpSession session) {
        boardService.deleteBoard(vo);
        session.invalidate();
        return "login";
    }
}
 
cs

 

<BoardDO.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
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
package com.spring.test.domain;
 
import java.util.Date;
 
public class BoardVO {
    private String id;
    private String pw;
    private String name;
    private String phone;
    private Date regDate;
 
    public String getId() {
        return id;
    }
 
    public void setId(String id) {
        this.id = id;
    }
 
    public String getPw() {
        return pw;
    }
 
    public void setPw(String pw) {
        this.pw = pw;
    }
 
    public String getName() {
        return name;
    }
    
    public void setName(String name) {
        this.name = name;
    }
 
    public String getPhone() {
        return phone;
    }
 
    public void setPhone(String phone) {
        this.phone = phone;
    }
 
    public Date getRegDate() {
        return regDate;
    }
 
    public void setRegDate(Date regDate) {
        this.regDate = regDate;
    }
 
    @Override
    public String toString() {
        return "BoardVO [idx=" + id + ", title=" + pw + ", name=" + name + ", phone=" + phone + ", regDate="
                + regDate + "]";
    }
 
    
}
cs

 

<BoardService.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
26
27
28
29
30
31
32
33
package com.spring.test.service;
 
import javax.servlet.http.HttpSession;
import java.util.List;
 
import com.spring.test.domain.BoardVO;
 
public interface BoardService {
        
        // 글 목록 조회
        List<BoardVO> getBoardList();
        
        public BoardVO login(BoardVO vo) throws Exception;
        
        // 글 상세 조회
        BoardVO getContent(BoardVO vo);
        
        //id 찾기
        BoardVO findIdBoard(BoardVO vo);
        
        //패스워드 찾기
        BoardVO findPwBoard(BoardVO vo);
                
        // 글 등록
        void insertBoard(BoardVO vo);
 
        // 글 수정
        void updateBoard(BoardVO vo);
 
        // 글 삭제
        void deleteBoard(BoardVO vo);
}
 
cs

 

<BoardServiceImpl.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
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
package com.spring.test.service.impl;
 
import java.util.List;
 
import javax.servlet.http.HttpSession;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import com.spring.test.dao.BoardDAO;
import com.spring.test.domain.BoardVO;
import com.spring.test.service.BoardService;
 
@Service
public class BoardServiceImpl implements BoardService{
 
    @Autowired
    private BoardDAO boardDAO;
    
    @Override
    public BoardVO login(BoardVO vo) throws Exception {
        return boardDAO.login(vo);
    }
    
    @Override
    public List<BoardVO> getBoardList() {
        return boardDAO.getBoardList();
    }
 
    @Override
    public BoardVO getContent(BoardVO vo) {
        return boardDAO.getContent(vo);
    }
    
    @Override
    public BoardVO findIdBoard(BoardVO vo) {
        return boardDAO.findIdBoard(vo);
    }
    
    @Override
    public BoardVO findPwBoard(BoardVO vo){
        return boardDAO.findPwBoard(vo);
    }
 
    @Override
    public void insertBoard(BoardVO vo) {
        boardDAO.insertBoard(vo);
    }
 
    @Override
    public void updateBoard(BoardVO vo) {
        boardDAO.updateBoard(vo);
    }
 
    @Override
    public void deleteBoard(BoardVO vo) {
        boardDAO.deleteBoard(vo);
    }
 
}
 
cs