KIC 백엔드 수업

5월 30일 (화) - []

Crecok 2023. 5. 30. 17:48
반응형

 

이전 수업 개념

 

 

▶ Spring MVC

- Spring DI의 기능과 Spring AOP의 기능이 내장되어있다.

 

 Spring MVC 구현 방법

- Java 프로젝트를 Maven 프로젝트로 변경 후 Spring Framework 적용 + 기타 설정

- STS (Spring Tool Suite) → Spring Boot

 

 Spring Boot - 유니콘

 전자정부 프레임워크 - Legacy Spring MVC

 


 

아래 흐름에 따라 프로젝트 구성해보기

 

입력 주소 경로 보여지는 페이지
zipcode.daum model2.ZipcodeAction Zipcode.jsp
zipcode_ok.daum model2.ZipcodeOkAction Zipcode_ok.jsp

 

- Maven 리포지토리에서 Mariadb, Lombok 라이브러리 가져오기

 

ZipcodeTO

package model1;

import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class ZipcodeTO {
	private String zipcode;
	private String sido;
	private String gugun;
	private String dong;
	private String ri;
	private String bunji;
}

 

ZipcodeOkAction

 


 

게시판 → Spring MVC

 

Model2 (URL → Spring MVC)

board01 프로젝트

 


 

어노테이션

 

 

list1.do → listview1.jsp

list2.do → listview2.jsp

 

▶ 다이나믹 프로젝트 생성 : web04

 메이븐 프로젝트로 변경 : groupId : com.exam / artifactId : web

 

▶ pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.exam</groupId>
	<artifactId>web</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>war</packaging>
	
	<name>web Maven Webapp</name>
	<url>http://maven.apache.org</url>

	<properties>

		<!-- Generic properties -->
		<java.version>11</java.version>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

		<!-- Web -->
		<jsp.version>2.2</jsp.version>
		<jstl.version>1.2</jstl.version>
		<servlet.version>2.5</servlet.version>

		<!-- Spring -->
		<spring-framework.version>5.2.8.RELEASE</spring-framework.version>

		<!-- Hibernate / JPA -->
		<hibernate.version>5.6.9.Final</hibernate.version>

		<!-- Logging -->
		<logback.version>1.2.11</logback.version>
		<slf4j.version>1.7.36</slf4j.version>

		<!-- Test -->
		<junit.version>4.11</junit.version>

	</properties>
	<dependencies>

		<!-- Spring MVC -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>${spring-framework.version}</version>
		</dependency>

		<!-- Other Web dependencies -->
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>jstl</artifactId>
			<version>${jstl.version}</version>
		</dependency>
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>servlet-api</artifactId>
			<version>${servlet.version}</version>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>javax.servlet.jsp</groupId>
			<artifactId>jsp-api</artifactId>
			<version>${jsp.version}</version>
			<scope>provided</scope>
		</dependency>

		<!-- Spring and Transactions -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-tx</artifactId>
			<version>${spring-framework.version}</version>
		</dependency>

		<!-- Logging with SLF4J & LogBack -->
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-api</artifactId>
			<version>${slf4j.version}</version>
			<scope>compile</scope>
		</dependency>
		<dependency>
			<groupId>ch.qos.logback</groupId>
			<artifactId>logback-classic</artifactId>
			<version>${logback.version}</version>
			<scope>runtime</scope>
		</dependency>

		<!-- Hibernate -->
		<dependency>
			<groupId>org.hibernate</groupId>
			<artifactId>hibernate-entitymanager</artifactId>
			<version>${hibernate.version}</version>
		</dependency>


		<!-- Test Artifacts -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-test</artifactId>
			<version>${spring-framework.version}</version>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>${junit.version}</version>
			<scope>test</scope>
		</dependency>

	</dependencies>


	<build>
		<plugins>
			<plugin>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.8.1</version>
				<configuration>
					<release>11</release>
				</configuration>
			</plugin>
			<plugin>
				<artifactId>maven-war-plugin</artifactId>
				<version>3.2.3</version>
			</plugin>
		</plugins>
	</build>
</project>

 

 

▶ web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" version="4.0">
  <display-name>web01</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.jsp</welcome-file>
    <welcome-file>default.htm</welcome-file>
  </welcome-file-list>
  
  <!--  encoding -->
  <filter>
  	<filter-name>encodingFilter</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>encodingFilter</filter-name>
  	<url-pattern>*.do</url-pattern>
  </filter-mapping>
  <servlet>
	<servlet-name>appServlet</servlet-name>
	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
	<init-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>/WEB-INF/servlet-context.xml</param-value>
	</init-param>
	<load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
	<servlet-name>appServlet</servlet-name>
	<url-pattern>*.do</url-pattern>
  </servlet-mapping>
  
</web-app>

 

 

▶ servlet-context.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"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="
	http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
	http://www.springframework.org/schema/context 
	http://www.springframework.org/schema/context/spring-context-4.3.xsd">

	<!--
	<bean class="config.ConfigController1" />
	<bean class="config.ConfigController2" />
	-->
	
	<context:component-scan base-package="config" />
	
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/views/"/>
		<property name="suffix" value=".jsp"/>
	</bean>
	 
</beans>

 

 

 패키지 생성 : config

 클래스 생성 : ConfigController1.java

package config;

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

@Controller
public class ConfigController1 {

	@RequestMapping( "/list1.do" )
	public String handleRequest1() {
		System.out.println( "handleRequest1() 호출" );
		return "listview1";
	}
	
	@RequestMapping( "/list2.do" )
	public String handleRequest2() {
		System.out.println( "handleRequest2() 호출" );
		return "listview2";
	}
    
}

 

 

 ConfigController2.java

package config;

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

@Controller
public class ConfigController2 {
	
	@RequestMapping( "/list3.do" )
	public ModelAndView handleRequest3() {
		System.out.println( "handleRequest3() 호출" );
		
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName( "listview3" );
		return modelAndView;
	}
	
	@RequestMapping( "/list4.do" )
	public ModelAndView handleRequest4() {
		System.out.println( "handleRequest4() 호출" );
		
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName( "listview4" );
		return modelAndView;
	}
	
}

 

 

▶ listview1.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
listview1.jsp
</body>
</html>

 

 

 listview2.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
listview2.jsp
</body>
</html>

 

 

 listview3.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
listview3.jsp
</body>
</html>

 

 

 listview4.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
listview4.jsp
</body>
</html>

 

 

 index.jsp (실행)

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>

<a href="list1.do">list1.do</a><br />
<a href="list2.do">list2.do</a><br />
<a href="list3.do">list3.do</a><br />
<a href="list4.do">list4.do</a><br />

</body>
</html>

 


 

/board/list1.do → listview1.jsp

/board/list2.do → listview2.jsp

 

 

 

위 프로젝트에서 ConfigController3 클래스 추가

package config;

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

@Controller
@RequestMapping( "/board" )
public class ConfigController3 {
	
	@RequestMapping( "/list1.do" )
	public String handleRequest1() {
		return "listview1";
	}
	
	@RequestMapping( "/list2.do" )
	public String handleRequest2() {
		return "listview2";
	}
	
}

 

 

index.jsp 수정 (실행)

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>

<a href="list1.do">list1.do</a><br />
<a href="list2.do">list2.do</a><br />
<a href="list3.do">list3.do</a><br />
<a href="list4.do">list4.do</a><br />

<a href="board/list1.do">board/list1.do</a><br />
<a href="board/list2.do">board/list2.do</a><br />

</body>
</html>

 


 

 

get, post 호출 방식 분리

 

 

 

ConfigController4 클래스 추가

package config;

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

@Controller
public class ConfigController4 {

	@RequestMapping( "/form.do" )
	public ModelAndView handleRequest1() {
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName( "form" );
		return modelAndView;
	}
	
	@GetMapping( "/form_ok.do" )
	// @RequestMapping( value="/form_ok.do", method=RequestMethod.GET )
	public ModelAndView form_get_ok() {
		
		System.out.println( "form_get_ok() 호출" );
		
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName( "form_ok" );
		return modelAndView;
	}
	
	@PostMapping( "/form_ok.do" )
	// @RequestMapping( value="/form_ok.do", method=RequestMethod.POST )
	public ModelAndView form_post_ok() {
		
		System.out.println( "form_post_ok() 호출" );
		
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName( "form_ok" );
		return modelAndView;
	}
	
}

 

 

form.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
form.jsp<br /><br />

<form action="form_ok.do" method="get">
데이터<input type="text" name="data" />
<input type="submit" value="전송" />
</form>

<form action="form_ok.do" method="post">
데이터<input type="text" name="data" />
<input type="submit" value="전송" />
</form>

</body>
</html>

 

 

form_ok.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
form_ok.jsp
</body>
</html>

 

 

index.jsp 수정 (실행)

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>

<a href="list1.do">list1.do</a><br />
<a href="list2.do">list2.do</a><br />
<a href="list3.do">list3.do</a><br />
<a href="list4.do">list4.do</a><br />

<a href="board/list1.do">board/list1.do</a><br />
<a href="board/list2.do">board/list2.do</a><br />

<a href="form.do">form.do</a><br />

</body>
</html>

 

 


 

form.jsp 수정

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
form.jsp<br /><br />

<form action="form_ok.do" method="get">
데이터<input type="text" name="data" />
<input type="submit" value="전송" />
</form>

<form action="form_ok.do" method="post">
데이터1 <input type="text" name="data1" />
데이터2 <input type="text" name="data2" />
<input type="submit" value="전송" />
</form>

</body>
</html>

 

 

ConfigController4 다시 수정

package config;

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

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

@Controller
public class ConfigController4 {


	@RequestMapping( "/form.do" )
	public ModelAndView handleRequest1() {
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName( "form" );
		return modelAndView;
	}
	
	/*
	@GetMapping( "/form_ok.do" )
	// @RequestMapping( value="/form_ok.do", method=RequestMethod.GET )
	public ModelAndView form_get_ok() {
		
		System.out.println( "form_get_ok() 호출" );
		
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName( "form_ok" );
		return modelAndView;
	}
	
	@PostMapping( "/form_ok.do" )
	// @RequestMapping( value="/form_ok.do", method=RequestMethod.POST )
	public ModelAndView form_post_ok() {
		
		System.out.println( "form_post_ok() 호출" );
		
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName( "form_ok" );
		return modelAndView;
	}
	*/
	
	@RequestMapping( value="/form_ok.do" )
	public ModelAndView form_ok( String data1, String data2 ) {
		
		System.out.println( "form_ok() 호출 : " + data1 );
		System.out.println( "form_ok() 호출 : " + data2 );
		
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName( "form_ok" );
		return modelAndView;
	}
	
}

 

 

index.jsp 실행

 

 


 

ConfigController4 수정

package config;

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

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class ConfigController4 {


	@RequestMapping( "/form.do" )
	public ModelAndView handleRequest1() {
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName( "form" );
		return modelAndView;
	}
	
	/*
	@GetMapping( "/form_ok.do" )
	// @RequestMapping( value="/form_ok.do", method=RequestMethod.GET )
	public ModelAndView form_get_ok() {
		
		System.out.println( "form_get_ok() 호출" );
		
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName( "form_ok" );
		return modelAndView;
	}
	
	@PostMapping( "/form_ok.do" )
	// @RequestMapping( value="/form_ok.do", method=RequestMethod.POST )
	public ModelAndView form_post_ok() {
		
		System.out.println( "form_post_ok() 호출" );
		
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName( "form_ok" );
		return modelAndView;
	}
	
	
	@RequestMapping( value="/form_ok.do" )
	public ModelAndView form_ok( String data1, String data2 ) {
		
		System.out.println( "form_ok() 호출 : " + data1 );
		System.out.println( "form_ok() 호출 : " + data2 );
		
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName( "form_ok" );
		return modelAndView;
	}
	
	
	@RequestMapping( value="/form_ok.do" )
	public ModelAndView form_ok( @RequestParam( "data1" ) String pdata1 ) {
		
		// System.out.println( "form_ok() 호출 : " + data1 );
		System.out.println( "form_ok() 호출 : " + pdata1 );
		
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName( "form_ok" );
		return modelAndView;
	}
	
	
	
	@RequestMapping( value="/form_ok.do" )
	public String form_ok( HttpServletRequest request ) {

		request.setAttribute( "data" , request.getParameter( "data" ) );
		return "form_ok";
	}
	*/
	
	@RequestMapping( value="/form_ok.do" )
	public ModelAndView form_ok( HttpServletRequest request ) {

		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName( "form_ok" );
		modelAndView.addObject( "data1", request.getParameter( "data1" ) );
		return modelAndView;
	}
}

 

 


 

우편번호 검색기 / 게시판 어노테이션을 이용해 만들기

 

 

반응형