728x90
λ°μν
[spring] μ€νλ§ νλ‘μ νΈ λ§λ€κΈ°
β Dynamic Web Project λ§λ€κΈ° μ€μ λ°©λ²
β λ©μ΄λΈ νλ‘μ νΈλ‘ λ³κ²½νλ λ°©λ²
β pom.xml μ€μ : Spring Library μμ‘΄μ± μΆκ° μ½λ ↓
β λ€μ΄λ‘λ ν κ² <dependencies> ~ </dependencies> μμ μ½λ λ£κΈ°
<!-- 4.3.3.RELEASE λ²μ μ μ°κΈ° μν΄ -->
<properties>
<org.springframework-version>4.3.3.RELEASE</org.springframework-version>
</properties>
<!-- λ€μ΄λ‘λ ν κ² dependencies μμ μ½λ λ£κΈ° -->
<dependencies>
<!-- 1. spring container(core) -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<!-- 2. Spring Web -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<!-- 3. Spring MVC -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${org.springframework-version}</version>
</dependency>
</dependencies>
β
μ½λ μμ± ν, μ μ₯ νλ©΄ μκΈ°λ λ³ν : Maven Dependencies μμ±
β web.xml : DispatcherServlet μ μ
<!-- DispatChServlet Mapping : Front Controller λ±λ‘ -->
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
β web.xml : νκΈμ²λ¦¬
→ μμ μ½λ λ°λ‘ λ°μ μΆκ°λ‘ μμ±
<!-- νκΈμ²λ¦¬ -->
<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>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
β WEB-INF ν΄λ μμ spring-servlet.xml νμΌ μμ± ν, μλ μ½λ μ λ ₯
β /WEB-INF/spring-servlet.xml λ§λ ν μλ μ½λ μ λ ₯ ↓
<?xml version="1.0" encoding="UTF-8"?>
<!-- μλ μ½λ ctrl+c , v -->
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"
xmlns:mvc="http://www.springframework.org/schema/mvc">
<!-- annotation μ€μ μ νκ² λ€. -->
<context:annotation-config />
<!-- com.javaex.controller ν¨ν€μ§ λ°μ μλ ν΄λμ€ μ€μ @Controllerλ₯Ό λ¬κ³ μλ ν΄λμ€μ κ°μ²΄λ₯Ό
μμ± νκ² λ€. (new νκ² λ€) -->
<context:component-scan
base-package="com.javaex.controller" />
</beans>
β src/main/java ν΄λ μμ com.javaex.controller ν¨ν€μ§ μμ±
β’ class μμ± ν, Hello.java νμΌ λ§λ€κΈ°
β’ μλ μ½λλ‘ controller μμ±νκΈ°
β’ @Controller / @RequestMapping μ½λ λ³΅λΆ ν, ctrl + shift + o λ‘ import μμ±νκΈ°
package com.javaex.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class Hello {
@RequestMapping( "/hello")
public String hello(){
System.out.println("/hellospring/hello");
return "/WEB-INF/views/index.jsp";
}
}
@Controller / @RequestMapping μ½λ λ³΅λΆ ν, ctrl + shift + o λ‘ import μμ±ν νλ©΄ λ³ν
β index.jsp νμΌ λ§λ€κΈ° : view μμ±νκΈ°
β’ WEB-INF ν΄λ μμ views ν΄λ μμ± ν, index.jsp νμΌ λ§λ€κΈ°
β’ 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>
<!-- νΈμΆμ£Όμ : localhost:8088/hellospring/hello -->
<h1>hello spring ν¬λ‘ μ€νλ§ !@#$%^*()</h1>
</body>
</html>
μ€ννλ©΄μΌλ‘ νμΈνκΈ°
β localhost:8088/hellospring/hello μ£Όμλ‘ νμΈν΄λ³΄κΈ°
β’ localhost:λ²νΈ/νμΌμμΉ
728x90
λ°μν