[Spring] Spring MVC 예제
- BACKEND/Spring
- 2017. 6. 15. 14:46
o Dependency 추가
- Spring webmvc 링크
http://mvnrepository.com/artifact/org.springframework/spring-webmvc/4.3.9.RELEASE
- JSTL 링크
http://mvnrepository.com/artifact/jstl/jstl/1.2
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_3_1.xsd" version="3.1">
<display-name>spring_mvc_basic</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<!-- 모든 요청을 받는 서블릿을 등록 : FrontController -->
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<!-- Encoding 처리해주는 Filter 등록 -->
<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>/*</url-pattern>
</filter-mapping>
</web-app>
spring-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.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<context:component-scan base-package="domain.controller, domain.service"/>
</beans>
GreetingController.java
package domain.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import domain.service.GreetingService;
@Controller // <bean class="domain.controller.GreetingController">
@RequestMapping("/greeting/") //모든 Controller메소드의 URL 앞에 붙일 URL 경로.
public class GreetingController {
@Autowired
private GreetingService service;
@RequestMapping("hello.do") //.do는 생략 가능
public String hello(){
System.out.println("hello()실행");
return "/WEB-INF/view/greeting/response.jsp";
}
@RequestMapping("greeting1.do")
public ModelAndView greeting(){
System.out.println("greeting()");
String msg = "안녕하세요. 무명씨님.";
return new ModelAndView("/WEB-INF/view/greeting/response.jsp", "result", msg);
}
@RequestMapping("greeting2.do")
public ModelAndView greeting(String name){ //name 요청파라미터 이름
System.out.println("greeting(String)");
String msg = service.getGreeting(name);
return new ModelAndView("/WEB-INF/view/greeting/response.jsp", "result", msg);
}
@RequestMapping("greeting3.do") //Controller 로직을 처리할 메소드임을 선언 - 속성의 value - 요청 URL
public ModelAndView greeting(String name, int age){
System.out.println("greeting(String,int)");
String msg = service.getGreeting(name, age);
//ModelAndView -> 응답 URL -> View, View에 전달할 값 : name-value 쌍
return new ModelAndView("/WEB-INF/view/greeting/response.jsp", "result", msg);
}
}
GreetingServiceImpl.java
package domain.service.impl;
import org.springframework.stereotype.Service;
import domain.service.GreetingService;
@Service
public class GreetingServiceImpl implements GreetingService {
@Override
public String getGreeting(){
return "안녕하세요... 무명씨";
}
@Override
public String getGreeting(String name){
return String.format("%s 님 안녕하세요", name);
}
@Override
public String getGreeting(String name, int age){
return String.format("%d 세 %s님 안녕하세요", age, name);
}
}
GreetingService
package domain.service;
public interface GreetingService {
String getGreeting();
String getGreeting(String name);
String getGreeting(String name, int age);
}
index.jsp
<%@ page contentType="text/html;charset=UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
//String name = URLEncoder.encode("김영수, "UTF-8"); - 익스플로어에서 요청파라미터를 못받아오는 처리를 해주는 코드
%>
<h2>index.jsp</h2>
<a href="/spring_mvc_basic/greeting/hello.do">hello호출</a><br>
<a href="/spring_mvc_basic/greeting/greeting1.do">greeting1호출</a><br>
<a href="/spring_mvc_basic/greeting/greeting2.do?name=김영수">greeting2호출</a><br>
<a href="/spring_mvc_basic/greeting/greeting3.do?name=박영희&age=27">greeting3호출</a><br> <!-- 요청파라미터를 못받아오는 경우는 400오류를 리턴한다. -->
<hr>
<a href="<c:url value='/greeting/hello.do'/>">hello호출</a><br>
<a href="<c:url value='/greeting/greeting2.do'><c:param name='name' value='홍길동'/></c:url>">greeting2호출</a><br>
</body>
</html>
response.jsp
<%@ page contentType="text/html;charset=UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<a href="/spring_mvc_basic/index.jsp">index페이지</a>
<h2>Greeting Response.jsp</h2>
<h3>greeting에서의 응답 메세지 : ${requestScope.result}</h3>
</body>
</html>
'BACKEND > Spring' 카테고리의 다른 글
[Spring] Handler(Controller 로직 처리자) 개념 (0) | 2017.06.16 |
---|---|
[Spring] Spring MVC 개념 (0) | 2017.06.16 |
[Spring] Transaction (0) | 2017.06.15 |
[Spring] AOP (0) | 2017.06.14 |
[Spring] Spring에서 Mybatis사용하기 (0) | 2017.06.13 |