공부한것들을 정리하는 블로그 입니다.
3. Controller 통합(@RequestMapping), JSP에서 Command객체 사용(EL,@ModelAttribute), Servlet API 사용(HttpServletRequest, HttpSession) 본문
(2019) 사이드 프로젝트/BoardWeb2(게시판-SpringMVC)
3. Controller 통합(@RequestMapping), JSP에서 Command객체 사용(EL,@ModelAttribute), Servlet API 사용(HttpServletRequest, HttpSession)
호 두 2019. 11. 26. 13:08반응형
1. Controller 통합하기
- 기존 Controller들 전부 삭제
- BoardController 신규 생성
package com.springbook.view.board;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import com.springbook.biz.board.BoardVO;
import com.springbook.biz.board.impl.BoardDAO;
@Controller
/*@SessionAttributes("board")*/
public class BoardController {
/*// 검색 조건 목록 설정
@ModelAttribute("conditionMap")
public Map<String, String> searchConditionMap() {
Map<String, String> conditionMap = new HashMap<String, String>();
conditionMap.put("제목", "TITLE");
conditionMap.put("내용", "CONTENT");
return conditionMap;
}*/
@RequestMapping(value="/insertBoard.do")
public String insertBoard(BoardVO vo, BoardDAO boardDAO) {
boardDAO.insertBoard(vo);
return "getBoardList.do";
}
@RequestMapping("/updateBoard.do")
public String updateBoard(/*@ModelAttribute("board")*/ BoardVO vo, BoardDAO boardDAO) {
System.out.println("번호: " + vo.getWriter());
System.out.println("제목: " + vo.getTitle());
System.out.println("작성자: " + vo.getWriter());
System.out.println("내용: " + vo.getContent());
System.out.println("등록일: " + vo.getRegDate());
System.out.println("조회수: " + vo.getCnt());
boardDAO.updateBoard(vo);
return "getBoardList.do";
}
@RequestMapping("/deleteBoard.do")
public String deleteBoard(BoardVO vo, BoardDAO boardDAO) {
boardDAO.deleteBoard(vo);
return "getBoardList.do";
}
@RequestMapping("/getBoard.do")
public String getBoard(BoardVO vo, BoardDAO boardDAO, Model model) {
model.addAttribute("board", boardDAO.getBoard(vo));
return "getBoard.jsp";
}
@RequestMapping("/getBoardList.do")
public String getBoardList(BoardVO vo, BoardDAO boardDAO, Model model) {
/*@RequestParam(value="searchCondition", defaultValue="TITLE", required=false) String condition,
@RequestParam(value="searchKeyword", defaultValue="", required=false) String keyword,
BoardVO vo, BoardDAO boardDAO, Model model) {*/
// System.out.println("검색 조건: " + condition);
// System.out.println("검색 단어: " + keyword);
model.addAttribute("boardList", boardDAO.getBoardList(vo));
return "getBoardList.jsp";
}
}
2. method 속성변경 실습하기 (@RequestMapping method)
- 변경전
- 변경후
package com.springbook.view.user;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.springbook.biz.user.UserVO;
import com.springbook.biz.user.impl.UserDAO;
@Controller
public class LoginController {
@RequestMapping(value="/login.do", method=RequestMethod.GET)
public String loginView(UserVO vo) {
System.out.println("로그인 화면으로 이동");
vo.setId("user1");
vo.setPassword("user1");
return "login.jsp";
}
@RequestMapping(value="/login.do", method=RequestMethod.POST)
public String login(UserVO vo, UserDAO userDAO) {
System.out.println("로그인 처리");
if(userDAO.getUser(vo) != null) return "getBoardList.do";
else return "login.jsp";
}
}
3. JSP에서 Command 객체 사용 (EL $태그 사용)
- ex) login.jsp 수정
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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>로그인</title>
</head>
<body>
<center>
<h1>로그인</h1>
<hr>
<form action="login.do" method="post">
<table border="1" cellpadding="0" cellspacing="0">
<tr>
<td bgcolor="orange">아이디</td>
<td>
<input type="text" name="id" value="${userVO.id}">
</td>
</tr>
<tr>
<td bgcolor="orange">비밀번호</td>
<td>
<input type="password" name="password" value="${userVO.password}">
</td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value="로그인">
</td>
</tr>
</table>
</form>
<hr>
</center>
</body>
</html>
- 테스트를 위해 index.jsp 신규생성
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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>Main Page</title>
</head>
<body>
<center>
<h1>게시판 프로그램</h1>
<hr>
<a href="login.do">로그인</a><br><br><br>
<a href="getBoardList.do">글 목록 바로가기</a>
<hr>
</center>
</body>
</html>
4. @ModelAttribute 사용하기
- LoginController.java
- login.jsp
5. Servlet API 사용(HttpServletRequest, HttpSession)
- LoginController.java
package com.springbook.view.user;
import javax.servlet.http.HttpSession;
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 com.springbook.biz.user.UserVO;
import com.springbook.biz.user.impl.UserDAO;
@Controller
public class LoginController {
@RequestMapping(value="/login.do", method=RequestMethod.GET)
public String loginView(@ModelAttribute("user") UserVO vo) {
System.out.println("로그인 화면으로 이동");
vo.setId("user1");
vo.setPassword("user1");
return "login.jsp";
}
@RequestMapping(value="/login.do", method=RequestMethod.POST)
public String login(UserVO vo, UserDAO userDAO, HttpSession session) {
System.out.println("로그인 처리");
UserVO user = userDAO.getUser(vo);
if(user != null) {
session.setAttribute("userName", user.getName());
return "getBoardList.do";
} else {
return "login.jsp";
}
}
}
- getBoardList.jsp
===========================================================
실습화면
Controller 통합(@RequestMapping), JSP에서 Command객체 사용(EL,@ModelAttribute), Servlet API 사용(HttpServletRequest, HttpSession) : https://github.com/cyh789/BoardWeb2_2019
반응형
'(2019) 사이드 프로젝트 > BoardWeb2(게시판-SpringMVC)' 카테고리의 다른 글
7. gmail로 email 인증번호 보내기(비밀번호 찾기 기능/ 회원가입시 이메일 인증) (0) | 2020.01.06 |
---|---|
6. 프레젠테이션 레이어와 비즈니스 레이어 통합 (0) | 2019.11.29 |
5. @SessionAttributes, @ModelAttribute 사용하기 (0) | 2019.11.26 |
4. @RequestParam, @ModelAttribute 사용하기 (0) | 2019.11.26 |
2. 게시판 구조 변경 : (Spring MVC 구조) -> (어노테이션 기반 MVC) (0) | 2019.11.26 |
1. 시작하기 : 게시판 만들기(Spring MVC 구조) (0) | 2019.11.25 |
0. 환경설정 (Spring MVC) (0) | 2019.11.22 |
Comments