내용

글번호 700
작성자 heojk
작성일 2017-06-30 15:45:05
제목 [Re]회원관리기능 구현하기(숙제) 2
내용 10. 컨트롤러 메서드를 만들고 파라미터를 읽어 서비스 메서드를 실행시킬 수 있습니까? - @RequestMapping, @RequestParam, @PathVariable UserController.java
package com.coderby.myapp.user.controller;

import java.util.List;

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.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.coderby.myapp.user.model.UserVO;
import com.coderby.myapp.user.service.IUserService;

@Controller
@RequestMapping("/user")
public class UserController {
	
	private static final Logger logger = LoggerFactory.getLogger(UserController.class);
	
	@Autowired
	IUserService userService;
	
	@RequestMapping(value="/insert", method=RequestMethod.GET)
	public String insert() {
		logger.info("insert");
		return "user/insertform";
	}
	
	@RequestMapping(value="/insert", method=RequestMethod.POST)
	public String insert(UserVO user) {
		userService.insertUser(user);
		return "redirect:/";
	}
	
	@RequestMapping(value="/login", method=RequestMethod.GET)
	public String login() {
		return "user/loginform";
	}
	
	@RequestMapping(value="/login", method=RequestMethod.POST)
	public String login(String userId, String userPassword, Model model, HttpSession session) {
		try {
			if(userService.checkPassword(userId, userPassword)) {
				session.setMaxInactiveInterval(300);//5분
				session.setAttribute("userId", userId);
				return "redirect:/user/view";
			}else {
				model.addAttribute("message", ": 아이디 또는 비밀번호가 잘 못 됐습니다");
				session.invalidate();
				return "user/loginform";
			}
		}catch(Exception e) {
			session.invalidate();
			model.addAttribute("message", e.getMessage());
			return "user/loginform";
		}
	}
	
	@RequestMapping(value="/logout", method=RequestMethod.GET)
	public String logout(HttpSession session) {
		session.invalidate();
		return "redirect:/";
	}
	
	@RequestMapping(value="/view", method=RequestMethod.GET)
	public String getUser(HttpSession session, Model model) {
		String userId = (String)session.getAttribute("userId");
		if(userId == null || userId.equals("")) {
			model.addAttribute("message", ": 로그인 사용자가 아닙니다");
			return "user/loginform";
		}else {
			model.addAttribute("user", userService.selectUser(userId));
			return "user/view";
		}
	}
	
	@RequestMapping(value="/update", method=RequestMethod.GET)
	public String updateUser(HttpSession session, Model model) {
		String userId = (String)session.getAttribute("userId");
		if(userId == null || userId.equals("")) {
			model.addAttribute("message", ": 로그인 사용자가 아닙니다");
			return "user/loginform";
		}else {
			model.addAttribute("user", userService.selectUser(userId));
			return "user/updateform";
		}
	}
	
	@RequestMapping(value="/update", method=RequestMethod.POST)
	public String updateUser(UserVO user, HttpSession session, Model model) {
		String userId = (String)session.getAttribute("userId");
		if(userId == null || userId.equals("")) {
			model.addAttribute("message", ": 로그인 사용자가 아닙니다.");
			return "user/loginform";
		}else {
			userService.updateUser(user);
			return "redirect:/user/view";
		}
	}
	
	@RequestMapping(value="/delete", method=RequestMethod.GET)
	public String delete() {
		return "user/deleteform";
	}
	
	@RequestMapping(value="/delete", method=RequestMethod.POST)
	public String delete(String userPassword, HttpSession session, Model model) {
		String userId = (String)session.getAttribute("userId");
		if(userId==null || userId.equals("")) {
			model.addAttribute("message", ": 로그인한 사용자가 아닙ㄴ다.");
			session.invalidate();
			return "user/loginform";
		}else {
			if(userService.checkPassword(userId, userPassword)) {
				userService.deleteUser(userId, userPassword);
				session.invalidate();
				return "redirect:/";
			}else {
				model.addAttribute("message", ": 비밀번호가 다릅니다.");
				return "user/deleteform";
			}
		}
	}
	
	@RequestMapping(value="/list")
	public String list(Model model) {
		List<UserVO> users = userService.selectAllUser();
		model.addAttribute("users", users);
		return "user/list";
	}
	
}
11. 서비스결과를 모델에 저장할 수 있습니까?
model.addAttribute("user", user);
12. 리다이렉트와 포워드를 구분해서 뷰를 리턴할 수 있습니까?
return "redirect:/";
return "user/deleteform";
13. JSP 페이지를 생성할 수 있습니까? insertform.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>Insert title here</title>
</head>
<body>
<h1>회원가입 ${message}</h1>
<form action="insert" method="POST">
아이디 : <input type="text" name="userId"><br>
이름 : <input type="text" name="userName"><br>
비밀번호 : <input type="password" name="userPassword"><br>
<!-- 비밀번호 확인-->
역할 : <input type="text" name="userRole"><br>
<input type="submit" value=" 저장 ">
<input type="reset" value=" 취소 ">
</form>
</body>
</html>
loginform.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>Insert title here</title>
</head>
<body>
<h1>로그인 ${message}</h1>
<form action="login" method="post">
아이디 : <input type="text" name="userId"><br>
비밀번호 : <input type="password" name="userPassword"><br>
<input type="submit" value="로그인">
<input type="reset" value=" 취소 ">
</form>

</body>
</html>
view.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>Insert title here</title>
</head>
<body>
<h1>사원 정보</h1>
아이디 : ${user.userId}<br>
이름 : ${user.userName}<br>
비밀번호 : ${user.userPassword}<br>
역할 : ${user.userRole}<br>
<a href="update">수정</a>
</body>
</html>
updateform.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>Insert title here</title>
</head>
<body>
<h1>회원 정보 수정</h1>
<form action="update" method="post">
아이디 : <input type="text" name="userId" value="${user.userId}" readonly><br>
이름 : <input type="text" name="userName" value="${user.userName}"><br>
비밀번호 : <input type="password" name="userPassword" value="${user.userPassword}"><br>
역할 : <input type="text" name="userRole" value="${user.userRole}"><br>
<input type="submit" value=" 수정 ">
<input type="reset" value=" 취소 ">
</form>
</body>
</html>
deleteform.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>Insert title here</title>
</head>
<body>
<h1>비밀번호를 입력해주세요.${message}</h1>
<form action="delete" method="post">
사용자 비밀번호 : <input type="password" name="userPassword">
<input type="submit" value=" 삭제 ">
</form>
</body>
</html>
14. 모델의 데이터를 출력할 수 있습니까? view.jsp


15. 뷰에서 모델 데이터 출력 시 컬렉션 또는 맵 형식 데이터를 출력할 수 있습니까?
list.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>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>사원 목록</h1>
<c:forEach var="user" items="${users}">
${user.userId}, ${user.userName}, ${user.userPassword}, ${user.userRole}<br>
</c:forEach>
</body>
</html>	
16. 리소스 처리를 할 수 있습니까? css, js, image 파일 등 web.xml
	<mvc:resources location="/images/**" mapping="/WEB-INF/resources/images/"/>
	<mvc:resources location="/js/**" mapping="/WEB-INF/resources/js/"/>
	<mvc:resources location="/css/**" mapping="/WEB-INF/resources/css/"/>
17. HTML 문서에서 경로 처리를 할 수 있습니까? <a href="<c:url value='/xxx'/>">링크</a> 18. 수정/삭제 흐름을 이해합니까? 네