기록해! 정리해!

어노테이션 기반 MVC 개발 본문

Spring

어노테이션 기반 MVC 개발

zsuling 2022. 9. 26. 14:38

p.355

<?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"
	xsi:schemaLocation="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.3.xsd">

<context:component-scan base-package="com.springbook.view" />


</beans>

[ 원래 소스 ]

package com.springbook.view.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

import com.springbook.biz.board.BoardDao;
import com.springbook.biz.board.BoardVo;
import com.springbook.biz.board.impl.BoardDaoImpl;

public class GetBoardListController implements  Controller {
	public GetBoardListController(){
		System.out.println("===>GetBoardListController");
	}
	@Override
	public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) {
		 System.out.println("글 목록 검색 처리"); 
		 String searchCondition=request.getParameter("searchCondition");
		 String searchKeyword=request.getParameter("searchKeyword");

		  BoardVo vo = new BoardVo();
		  vo.setSearchCondition(searchCondition);
		  vo.setSearchKeyword(searchKeyword);
		  
		  BoardDao   dao = new BoardDaoImpl();
		  List<BoardVo> li = dao.getBoardList(vo);
		  
		  // JSP 구문을 사용하려면 session 을 사용해야 한다.
		  // HttpSession session = request.getSession();
		  // session.setAttribute("li", li);
		 
		  
		  // ModelAndView 를 사용해서 mav.addObject() 넘기면
		  //  EL / JSTL 로 받아오야 한다. 
		  ModelAndView mav=new ModelAndView();
		  mav.addObject("li", li);
		  mav.setViewName("getBoardList");
		  		  
		return mav;
	}

}

[ Command 화 적용 소스 ]

package com.springbook.view.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.springbook.biz.board.BoardVo;
import com.springbook.biz.board.impl.BoardDaoImpl;

@Controller
public class GetBoardListController {
	
	public GetBoardListController(){
		System.out.println("===>GetBoardListController");
	}
	
	@RequestMapping(value="/getBoardList.do")
	public ModelAndView getBoardList(BoardVo vo , BoardDaoImpl dao, ModelAndView mav) {
		 System.out.println("글 목록 검색 처리"); 
		  		  		 
		  mav.addObject("li", dao.getBoardList(vo));
		  mav.setViewName("getBoardList.jsp");
		  		  
		return mav;
	}

}

Command 화 적용하기 전체 소스

package com.springbook.view.controller;

import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.springbook.biz.board.impl.BoardDaoImpl;

@Controller
public class DeleteBoardController  {
	public DeleteBoardController(){
		System.out.println("===>DeleteBoardController");
	}
	
	@RequestMapping(value="/deleteBoard.do")
	public String deleteBoard(HttpServletRequest request, BoardDaoImpl dao) {

		 System.out.println("글 삭제 처리"); 
		 int seq= Integer.parseInt(request.getParameter("seq")) ;
		 dao.delete(seq);
		  		 
		return "getBoardList.do";
	}

}
--------------------------------------------------------------------------------------------------------------------------

​

package com.springbook.view.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.springbook.biz.board.BoardVo;
import com.springbook.biz.board.impl.BoardDaoImpl;


@Controller
public class InsertBoardController  {
	public InsertBoardController(){
		System.out.println("===>InsertBoardController");
	}
	
	@RequestMapping(value="/insertBoard.do")
	public String insertBoard(BoardVo vo, BoardDaoImpl dao) {
		 		  
		 dao.insert(vo);		  	
		 
		return "getBoardList.do";
		   
		
	}

}
---------------------------------------------------------------------------------------------------------------------------

​

package com.springbook.view.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.springbook.biz.board.BoardVo;
import com.springbook.biz.board.impl.BoardDaoImpl;

@Controller
public class UpdateBoardController {
	public UpdateBoardController(){
		System.out.println("===>UpdateBoardController");
	}
	
	@RequestMapping(value="/updateBoard.do")
	public String handleRequest(BoardVo vo, BoardDaoImpl dao) {	
		
		   dao.update(vo);	
		   
		return "getBoardList.do";
	}

}
------------------------------------------------------------------------------------------------------------------------------

​

package com.springbook.view.controller;

import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.springbook.biz.board.impl.BoardDaoImpl;

@Controller
public class GetBoardController  {
	public GetBoardController(){
		System.out.println("===>GetBoardController");
	}
	
	
	@RequestMapping(value="/getBoard.do")
	public ModelAndView getBoard(HttpServletRequest request,BoardDaoImpl dao, ModelAndView mav) {
		 System.out.println("글 상세 조회 처리"); 
		  
		  int seq= Integer.parseInt(request.getParameter("seq")) ;
		  dao.cnt(seq);  		  
		  mav.addObject("m", dao.getBoard(seq));
		  mav.setViewName("getBoard.jsp");
		  
		return mav;
	}

}
------------------------------------------------------------------------------------------------------------------------------

​

package com.springbook.view.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.springbook.biz.board.BoardVo;
import com.springbook.biz.board.impl.BoardDaoImpl;

@Controller
public class GetBoardListController {
	
	public GetBoardListController(){
		System.out.println("===>GetBoardListController");
	}
	
	@RequestMapping(value="/getBoardList.do")
	public ModelAndView getBoardList(BoardVo vo , BoardDaoImpl dao, ModelAndView mav) {
		 System.out.println("글 목록 검색 처리"); 
		  		  		 
		  mav.addObject("li", dao.getBoardList(vo));
		  mav.setViewName("getBoardList.jsp");
		  		  
		return mav;
	}

}
----------------------------------------------------------------------------------------------------------------------------

​

package com.springbook.view.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.springbook.biz.user.UserVo;
import com.springbook.biz.user.impl.UserDao3;

@Controller
public class LoginController  {
	public LoginController(){
		System.out.println("===>LoginController");
	}
	
	@RequestMapping(value="/login.do")
	public String login(UserVo vo, UserDao3 service ) {
		System.out.println("로그인 처리"); 		 		     
		  
		   if(service.getUser(vo) != null){
			  return "getBoardList.do" ;
		   }else{
			  return"login.jsp" ;
		   }		 
	}

}

--------------------------------------------------------------------------------------------------------------------------

​

package com.springbook.view.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class LogoutController {
	public LogoutController(){
		System.out.println("===>LogoutController");
	}
	
	@RequestMapping(value="/logout.do")
	public String logout( HttpSession session) {
		
		 System.out.println("로그아웃 처리");		 
		 session.invalidate();		 
				 
		return "login.jsp";
	}

}

 

 

Comments