기록해! 정리해!

Spring MVC 구조 본문

Spring

Spring MVC 구조

zsuling 2022. 9. 26. 14:35

[ web.xml ]


<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns="http://Java.sun.com/xml/ns/javaee" 
xmlns:web="http://java.sun.com/xml/ns/javaee" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
https://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">

  <servlet>
    <servlet-name>DispatcherServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>DispatcherServlet</servlet-name>
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>
  
</web-app>​
 
 

[ presentation-layer.xml ]


<?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">


</beans>​

[ web.xml ]

 

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns="http://Java.sun.com/xml/ns/javaee" 
xmlns:web="http://java.sun.com/xml/ns/javaee" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
https://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">

  <filter>
    <filter-name>characterEncoding</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
      <param-name>encoding</param-name> 
      <param-value>UTF-8</param-value>
    </init-param>    
  </filter>
  
  <filter-mapping>
    <filter-name>characterEncoding</filter-name>
    <url-pattern>*.do</url-pattern>  
  </filter-mapping>
  
  <servlet>
    <servlet-name>DispatcherServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>/WEB-INF/config/presentation-layer.xml</param-value>
    </init-param>
  
  </servlet>
  <servlet-mapping>
    <servlet-name>DispatcherServlet</servlet-name>
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>
  
</web-app>

컨트롤 변경하기

 

package com.springbook.view.controller;

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

import org.springframework.web.servlet.ModelAndView;
// springframework 에서 사용하는 Controller
import org.springframework.web.servlet.mvc.Controller;

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

// 1.  Controller  변경
public class LoginController implements Controller  {
	public LoginController(){
		System.out.println("===>LoginController");
	}
	@Override   // 2. 리턴형을 ModelAndView 로 변경 
	public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) {
		System.out.println("로그인 처리"); 
		 
		  String id=request.getParameter("id");
		  String password=request.getParameter("password");
		  

		   UserVo vo = new UserVo();
		   vo.setId(id);
		   vo.setPassword(password);
		   
		   UserDao3 service = new UserDao3();
		   UserVo user = service.getUser(vo);

		   // 3. ModelAndView 로 변경하기 
		   ModelAndView  mav = new ModelAndView();
		   if(user != null){
			   mav.setViewName("getBoardList.do") ;
		   }else{
			   mav.setViewName("login.jsp") ;
		   }
		   return  mav ;
		   		
	}

}
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);
		  
		  // HttpSession session = request.getSession();
		  // session.setAttribute("li", li);
		 
		  ModelAndView mav=new ModelAndView();
		  mav.addObject("li", li);
		  mav.setViewName("getBoardList.jsp");
		  		  
		return mav;
	}

}
package com.springbook.view.controller;

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

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

public class LogoutController implements Controller {
	public LogoutController(){
		System.out.println("===>LogoutController");
	}
	@Override
	public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) {
		 System.out.println("로그아웃 처리");
		 HttpSession session = request.getSession();
		 session.invalidate();
		 
		 ModelAndView  mav = new ModelAndView();
		 mav.setViewName("login.jsp");
		 
		return mav;
	}

}

[ presentation-layer.xml ]

<?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" />

</beans>​

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jstl/core_rt"  prefix="c"    %>
    
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<div align="center">
<br><br>
<h1> 글목록 </h1>
<h3> 테스트님 환영합니다. <a href=logout.do>Log-out</a> </h3>

<!-- 검색 시작 -->
<table border=1 width=700>
<tr><td align="right">
<form action=getBoardList.do >
<select name="searchCondition">
<option value="title"> 제목 </option>
<option value="content"> 내용 </option>
</select>
<input  type=text  name="searchKeyword" >
<input  type=submit value="검색하기" >
</form>
</td></tr>
</table>
<table border=1 width=700>
<tr> 
 <th> 번호 </th> <th> 제목 </th> <th> 작성자 </th>
 <th> 등록일 </th> <th> 조회수 </th>
</tr>
<c:forEach  items="${li}"  var="m">

<tr> 
 <td> ${m.seq} </td>
 <td> <a href=getBoard.do?seq=${m.seq}>
        ${m.title}
      </a> 
 </td>
 <td>  ${m.writer} </td>
 <td>  ${m.regDate} </td> <td> ${m.cnt} </td>
</tr>
</c:forEach>

</table>
<br>
<a href=insertBoard.jsp> 새글등록 </a>

</div>
</body>
</html>​

[ SampleController ]


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;

public class SampleController implements  Controller{

	@Override
	public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		// TODO Auto-generated method stub
		
		return null;
	}

}​

'Spring' 카테고리의 다른 글

어노테이션 기반 MVC 개발  (0) 2022.09.26
Spring MVC 구조 - ViewResolver 적용하기  (0) 2022.09.26
EL / JSTL 사용하기  (0) 2022.09.23
MVC 프레임워크 구현하기  (0) 2022.09.23
Model 2 : DispatcherServlet 만들기  (0) 2022.09.23
Comments