기록해! 정리해!

Spring MVC 구조 - ViewResolver 적용하기 본문

Spring

Spring MVC 구조 - ViewResolver 적용하기

zsuling 2022. 9. 26. 14:36

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
	http://www.springframework.org/schema/beans/spring-beans.xsd">

<!-- HandlerMapping 등록하기  -->
<bean id=""  class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping" >
  <property name="mappings">
    <props>
      <prop key="/login.do">login</prop>
      <prop key="/getBoardList.do">getBoardList</prop>
      <prop key="/getBoard.do">getBoard</prop>
      <prop key="/insertBoard.do">insertBoard</prop>
      <prop key="/updateBoard.do">updateBoard</prop>
      <prop key="/deleteBoard.do">deleteBoard</prop>
      <prop key="/logout.do">logout</prop>
    </props>  
   
  </property>
</bean>

<!--  컨트롤 등록하기  -->
<bean id="deleteBoard"  class="com.springbook.view.controller.DeleteBoardController" />
<bean id="login"  class="com.springbook.view.controller.LoginController" />
<bean id="getBoardList"  class="com.springbook.view.controller.GetBoardListController" />
<bean id="getBoard"  class="com.springbook.view.controller.GetBoardController" />
<bean id="insertBoard"  class="com.springbook.view.controller.InsertBoardController" />
<bean id="updateBoard"  class="com.springbook.view.controller.UpdateBoardController" />
<bean id="logout"  class="com.springbook.view.controller.LogoutController" />

<!-- ViewResolver 등록 -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
	<property name="prefix" value="/WEB-INF/board/"></property>
	<property name="suffix" value=".jsp"></property>
</bean>

</beans>

[ ViewResolver 적용 하지 않는 경우는 redirect:getBoardList.do 를 붙인다. ]

package com.springbook.view.controller;

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.impl.BoardDaoImpl;

public class DeleteBoardController implements  Controller {
	public DeleteBoardController(){
		System.out.println("===>DeleteBoardController");
	}
	@Override
	public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) {

		   System.out.println("글 삭제 처리"); 
		   
		   int seq= Integer.parseInt(request.getParameter("seq")) ;
		   BoardDao dao = new BoardDaoImpl();
		   dao.delete(seq);
		  
		   ModelAndView  mav = new ModelAndView();
		   mav.setViewName("redirect:getBoardList.do");
		   
		return mav;
	}

}

[ ViewResolver 적용 하는 경우 : 확장자를 지운다. ]

package com.springbook.view.controller;

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 GetBoardController implements Controller {
	public GetBoardController(){
		System.out.println("===>GetBoardController");
	}
	@Override
	public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) {
		 System.out.println("글 상세 조회 처리"); 
		  int seq= Integer.parseInt(request.getParameter("seq")) ;
		  
		  BoardDao   dao = new BoardDaoImpl();
		  dao.cnt(seq);
		  BoardVo m = dao.getBoard(seq);
		 
		  // HttpSession session = request.getSession();	  
		  // session.setAttribute("m", m);
		  
		  ModelAndView mav=new ModelAndView();
		  mav.addObject("m", m);
		  mav.setViewName("getBoard");
		  
		return mav;
	}

}​

 

'Spring' 카테고리의 다른 글

어노테이션 기반 MVC 개발 - 컨트롤러 통합하기  (0) 2022.09.26
어노테이션 기반 MVC 개발  (0) 2022.09.26
Spring MVC 구조  (0) 2022.09.26
EL / JSTL 사용하기  (0) 2022.09.23
MVC 프레임워크 구현하기  (0) 2022.09.23
Comments