[스프링학원]Spring MVC HelloWorld, XML방식 및 자바설정(Java Config), 스프링동영상교육, 학원교육도좋지만 예제 많이 만들어 보세요~
http://ojc.asia/bbs/board.php?bo_table=LecSpring&wr_id=888
ojc.asia
https://www.youtube.com/watch?v=OXnif975EoQ&list=PLxU-iZCqT52B7oCYJltUT2-kBzckdr5vp&index=12

https://www.youtube.com/watch?v=KPiMj6pJQj0&list=PLxU-iZCqT52B7oCYJltUT2-kBzckdr5vp&index=15&t=18s

1. File -> New -> Spring Legacy Project 선택
Project name : hello3
Spring MVC Project 선택
top level package 를 a.b.hello3 로 입력 (hello3은 Context 이름, localhost:8080/hello3)
pom.xml에서 스프링의 버전을 4.3 이상으로… 4.3.0
2. webapp아래의 다운로드된 모든 파일 삭제, a.b.hello3 패키지의 HomeController.java 삭제
3. HelloController.java
package a.b.hello3;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/hello") //http://localhost:8080/hello3/hello
public class HelloController {
@GetMapping
//@RequestMapping(method=RequestMethod.Get)
public String hello(Model model) {
model.addAttribute("message", "Hello Spring MVC framework!");
return "hello"; //View 이름, ViewResolver가 해석함, 주로 접두어. 접미어 붙임
}
}
4. WEB-INF/web.xml (프로젝트에서 우측 마우스 >> j2ee tools >> generate deployment descriptor stub 선택)
<?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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<servlet>
<servlet-name>helloServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>helloServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
5. /WEB-INF/helloServlet-servlet.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">
<context:component-scan base-package="a.b.hello3"/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/jsp/</value>
</property>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
6. /WEB-INF/jsp/hello.jsp
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>Insert title here</title>
</head>
<body>
<h3>${message}</h3>
</body>
</html>
실행 : http://localhost:8080/hello3/hello
Spring MVC HelloWorld를 자바설정(Java Config)으로 변경
1. File -> New -> Spring Legacy Project 선택
Project name : spring4mvc
Spring MVC Project 선택
top level package 를 a.b.spring4mvc 로 입력
2. 기본적으로 컨트롤러, web.xml, 디스패처 서블릿의 설정파일을 자동 다운로드 한다. 프로젝트에서 우측 마우스 버튼 run as --> run on server 로 실행해보자.
(http://localhost:8080/spring4mvc/)
3. /WEB-INF/web.xml 파일과 /WEB-INF/spring/root-context.xml, /WEBINF/spring/appServlet/servlet-context.xml 파일을 삭제하자.
4. pom.xml에서 아래 플러그인을 추가하자.(web.xml이 없어도 에러나지 않도록)
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
servlet-api 버전 3.0 이상인지 확인하자.
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
5. 기본 패키지 아래에 config 패키지 만들고 아래의 두 자바 설정 파일을 만들자.
[Config.java]
/* DispatcherServlet 설정파일의 뷰리졸버를 정의 */
package config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.view.JstlView;
import org.springframework.web.servlet.view.UrlBasedViewResolver;
@Configuration //자바 설정 파일임을 나타냄
@ComponentScan("a.b.spring4mvc") //XML 설정의 component-scan base-package
@EnableWebMvc //Spring Web MVC의 Annotation 이 가능하도록
public class Config {
@Bean
public UrlBasedViewResolver setupViewResolver() { //뷰 리졸버
UrlBasedViewResolver viewResolver = new UrlBasedViewResolver();
viewResolver.setPrefix("/WEB-INF/views/");
viewResolver.setSuffix(".jsp");
viewResolver.setViewClass(JstlView.class);
return viewResolver;
}
}
[WebInit.java]
/* web.xml 파일의 설정을 정의한다. */
package config;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration.Dynamic;
import org.springframework.web.WebApplicationInitializer;
import
org.springframework.web.context.support.AnnotationConfigWebApplicationCont
ext;
import org.springframework.web.servlet.DispatcherServlet;
public class WebInit implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
ctx.register(Config.class);
ctx.setServletContext(servletContext);
Dynamic servlet = servletContext.addServlet("appServlet", new DispatcherServlet(ctx) );
servlet.addMapping("/");
servlet.setLoadOnStartup(1);
}
}
6. 브라우저에서 실행해 보자.
- http://localhost:8080/spring4mvc/
#스프링학원, #스프링교육, #Spring학원, #Spring교육, #SpringMVC, #스프링HelloWorld, #스프링강좌, #스프링강의
스프링학원, 스프링교육, Spring학원, Spring교육, SpringMVC, 스프링HelloWorld, 스프링강좌, 스프링강의