스프링 MVC 프로젝트 - memberController

이지윤·2022년 4월 11일
0

MVC 프로젝트 예제

MemberController.java

  • 학생목록 조회, 학생정보 조회 기능
  • 학생정보 등록, 학생정보 수정 기능
  • <MemberController.java>
package org.tukorea.web.controller;

import java.util.List;

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.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.tukorea.web.domain.StudentVO;
import org.tukorea.web.service.MemberService;

@Controller
@RequestMapping(value = "/member")
// 클래스 레벨로 기본 경로 /member지정
// http://localhost:8080/web/member
public class MemberController {
	private static final Logger logger = LoggerFactory.getLogger(MemberController.class);

	@Autowired(required = true)
    // Memberservice 객체를 주입
	private MemberService memberService;

	}

회원정보 서비스 인터페이스

  • <MemberService.java>
package org.tukorea.web.service;

import java.util.List;

import org.tukorea.web.domain.StudentVO;

public interface MemberService {
	
	public StudentVO readMember(String id) throws Exception;
	public void addMember(StudentVO student) throws Exception; 
	public List<StudentVO> readMemberList() throws Exception;
	public void updateMember(StudentVO student) throws Exception; 
	public void deleteMember(String id) throws Exception;
	
}

회원정보 서비스 모의구현 클래스

  • <MockMemberService.java>
package org.tukorea.web.service;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;

import org.tukorea.web.domain.StudentVO;

import org.springframework.stereotype.Service;

@Service
public class MockMemberService implements MemberService {
	private Map<String, StudentVO> storage = new HashMap<String, StudentVO>();
	
	@PostConstruct 
    // 빈 생성 후 초기화
	public void init() {
		StudentVO vo = new StudentVO();
		vo.setId("kangho");
		vo.setPasswd("pwd"); 
		vo.setUsername("kangho"); 
		vo.setSnum("20181010"); 
		vo.setDepart("computer"); 
		vo.setMobile("010-2222-3333"); 
		vo.setEmail("kang@tukorea.ac.kr"); 
		try {
			storage.put(vo.getId(), vo);
		}catch(Exception e) {
			System.out.println(e);
		}
	}

	@Override
	public StudentVO readMember(String id) throws Exception {
		// TODO Auto-generated method stub
		if(storage.get(id)==null) throw new Exception(id);
			return storage.get(id);
	}

	@Override
	public void addMember(StudentVO student) throws Exception {
		// TODO Auto-generated method stub
		storage.put(student.getId(), student);
	}

	@Override
	public List<StudentVO> readMemberList() throws Exception {
		// TODO Auto-generated method stub
		List<StudentVO> list = new LinkedList<StudentVO>();
		for(StudentVO customer : storage.values()) {
			list.add(customer);
		}
		return list;
	}

	@Override
	public void updateMember(StudentVO student) throws Exception {
		// TODO Auto-generated method stub
		if(storage.get(student.getId())!=null) {
			storage.remove(student.getId());
		}
		storage.put(student.getId(), student);
	}

	@Override
	public void deleteMember(String id) throws Exception {
		// TODO Auto-generated method stub
		storage.remove(id);	
	}
}

학생 목록 조회

@RequestMapping(value = { "/list" }, method = RequestMethod.GET)
	public String listMember(Model model) throws Exception {
		List<StudentVO> students = memberService.readMemberList();

		logger.info("/member.listt URL called. then listMember method executed.");
		model.addAttribute("students", students);
		return "member/member_list";

<member_list.jsp>

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@ taglib uri = "http://java.sun.com/jsp/jstl/core" prefix ="c"%> 
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Student List</title>
<link rel = "stylesheet" href = "../resources/member.css" type = "text/css"></link>
</head>
<body>
	<div align = "center">
		<header>학생 목록 </header>
		<c:url value = "/member/member_register" var = "url"/><a href = "${url}">학생 등록</a><br><br>
		<table>
		<tr>
			<th>ID</th>
			<th>PWD</th>
			<th>USERNAME</th>
			<th>SNUM</th>
			<th>DEPART</th>
			<th>MOBILE</th>
			<th>EMAIL</th>
		</tr>
		<c:forEach var = "member" items = "${students}">
		<tr>
		<td> <c:out value = "${member.id}"/></td>
		<td> <c:out value = "${member.passwd}"/></td>
		<td> <c:out value = "${member.username}"/></td>
		<td> <c:out value = "${member.snum}"/></td>
		<td> <c:out value = "${member.depart}"/></td>
		<td> <c:out value = "${member.mobile}"/></td>
		<td> <c:out value = "${member.email}"/></td>
		<td>
		<c:url value = "/member/read?id=${member.id}" var = "url"/><a href = "${url}">정보 보기</a>
		<c:url value = "/member/modify?id=${member.id}" var = "url"/><a href = "${url}">정보 수정 </a>
		</td>
		</tr>
		</c:forEach>
		</table>
	</div>
</body>
</html>

학생 정보 조회

@RequestMapping(value = "/read", method = RequestMethod.GET)
	public String readMember(@RequestParam("id") String id, Model model) throws Exception {
		StudentVO student = memberService.readMember(id);
		logger.info(" /read?id=kang URL called. then readMemebr method executed.");
		model.addAttribute("student", student);
		return "member/member_read";
	}


<member_read.jsp>

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

<!DOCTYPE html>
<html>
<head>
	<meta charset="UTF-8">
	<title>학생 정보</title>
	<link rel="stylesheet" href="../resources/member.css" type="text/css"></link>
</head>
<body>
	<div align=center>
		<header>학생 정보</header><br>
		<table>
		    <tr><th> LoginID </th><td><c:out value="${student.id}"/></td></tr>
			<tr><th> LoginPWD </th><td><c:out value="${student.passwd}"/></td></tr>
			<tr><th> YourName </th><td><c:out value="${student.username}"/></td></tr>
			<tr><th> StudentNumber </th><td><c:out value="${student.snum}"/></td></tr>
			<tr><th> Department </th><td><c:out value="${student.depart}"/></td></tr>
			<tr><th> Mobile </th><td><c:out value="${student.mobile}"/></td></tr>
			<tr><th> EmailNumber </th><td><c:out value="${student.email}"/></td></tr>
		</table>
		<c:url value="/member/list" var="url"/><a href="${url}">학생 목록 보기</a>
	</div>
</body>
</html>

학생정보 등록

@RequestMapping(value = {"/member_register"}, method = RequestMethod.GET)
	public String createMemberGet() throws Exception {
		logger.info(" /register URL GET method called. then forward member_register.jsp.");
		return "member/member_register"; 
		}
	
	@RequestMapping(value = {"/register"}, method = RequestMethod.POST)
	public String createMemberPost(@ModelAttribute("student") StudentVO vo) throws Exception
	{
		memberService.addMember(vo);
		logger.info(vo.toString());
		logger.info(" /register URL POST method called. then createMember method executed."); 
		return "redirect:/member/list";
		}

<member_register.jsp >

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
	<meta charset="UTF-8">
	<title>학생 등록</title>
	<link rel="stylesheet" href="../resources/member.css" type="text/css"></link>
</head>
<body>
	<div align=center>
		<header>학생 정보 등록</header>
		<form name=form1 action="http://localhost:8080/web/member/register/" method="post">
		<table>	
			<tr><th>LoginID</th><td><input type="text" name="id" autofocus placeholder="공백없이 입력하세요"></td></tr>
			<tr><th>LoginPWD</th><td><input type="text" name="passwd" placeholder="공백없이 입력하세요"></td></tr>
			<tr><th>YourName</th><td><input type="text" name="username"  placeholder="공백없이 입력하세요"></td></tr>
			<tr><th>StudentNumber</th><td><input type="text" name="snum" placeholder="공백없이 입력하세요"></td></tr>
			<tr><th>Department</th><td><input type="text" name="depart" placeholder="예)컴퓨터공학부"></td></tr>
			<tr><th>MobileNumber</th><td><input type="text" name="mobile" placeholder="***-****-****"></td></tr>
			<tr><th>EmailAddress</th><td><input type="text" name="email" placeholder="****@****.***"></td></tr>
		</table>
		<dl>
			<dd><input type="submit" name="submit" value="보내기"></dd>
			<dd><input type="reset" name="reset" value="다시 작성"></dd>
		</dl>
		</form>
	</div>
</body>
</html>

학생정보 수정

@RequestMapping(value = "/modify", method = RequestMethod.GET) 
	public String modifyMemberGet(@RequestParam("id") String id, Model model) throws Exception {
		StudentVO student = memberService.readMember(id);
		logger.info(" /modify?id=kang URL GET method called. then forward member_modify.jsp."); model.addAttribute("student", student);
		return "member/member_modify";
	}
	@RequestMapping(value = "/modify", method = RequestMethod.POST)
	public String modifyMemberPost(@ModelAttribute("student") StudentVO vo) throws Exception {
		memberService.updateMember(vo);
		logger.info(vo.toString());
		logger.info(" /modify?id=kang URL POST method called. then modifyMemberPost executed.");
		return "redirect:/member/list";
	}


<member_modify.jsp>

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

<!DOCTYPE html>
<html>
<head>
	<meta charset="UTF-8">
	<title>정보 수정</title>
	<link rel="stylesheet" href="../resources/member.css" type="text/css"></link>
</head>
<body>
	<div align=center>
		<header>학생 정보 수정</header>
		<form name=form1 action="http://localhost:8080/web/member/modify/" method="post">
		<table>
			<tr><th>LoginID</th><td><input type="text" name="id" readonly value="${student.id}"></td></tr>
			<tr><th>LoginPWD</th><td><input type="text" name="passwd" value="${student.passwd}"></td></tr>
			<tr><th>YourName</th><td><input type="text" name="username" value="${student.username}"></td></tr>
			<tr><th>StudentNumber</th><td><input type="text" name="snum" value="${student.snum}"></td></tr>
			<tr><th>Department</th><td><input type="text" name="depart" value="${student.depart}"></td></tr>
			<tr><th>MobileNumber</th><td><input type="text" name="mobile" value="${student.mobile}"></td></tr>
			<tr><th>EmailAddress</th><td><input type="text" name="email" value="${student.email}"></td></tr>
		</table><br>
			<input type="submit" name="submit" value="최종 수정">
			<input type="reset" name="reset" value="다시작성">
		</form>
	</div>
</body>
</html>

profile
초보자

0개의 댓글