본문 바로가기

SPRING

Spring4 MVC(기초 복습)

1. Pom.xml:

메이븐 프로젝트는 모든 dependencies를 pom.xml에 설정 - 자동적으로 관련 dependencies를 repository에서 다운

 

2.controller:

 

@Controoler: 클래스 위에 표시 - http 요청에 따라 클래스 위 또는 Method 위에 잡은 @RequestMapping 주소와 mapping

package com.websystique.springmvc.controller; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; @Controller @RequestMapping("/") public class HelloWorldController { @RequestMapping(method = RequestMethod.GET) public String sayHello(ModelMap model) { model.addAttribute("greeting", "Hello World from Spring 4 MVC"); return "welcome"; } @RequestMapping(value="/helloagain", method = RequestMethod.GET) public String sayHelloAgain(ModelMap model) { model.addAttribute("greeting", "Hello World Again, from Spring 4 MVC"); return "welcome"; } }

3:Spring configuration file:

 

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    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-4.0.xsd
    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
 
    <!-- 관련 pacakge에 있는 @Controller, @Service, @Repository, @Component, etc.. 
  인식 -->
 <context:component-scan base-package="com.hojin.controller" />
 <!-- annotation을 사용하겠다고 명시 -->
 <mvc:annotation-driven />
 <!-- 이 설정으로 컨트롤러 return 값에 자동으로 붙음 -->
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix">
            <value>/WEB-INF/views/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
    </bean>
 
</beans>
 

 

4:Update web configuration (web.xml) file

 

밑의 설정들에 의해, servlet이 controller 앞에 서 있어서 url pattern에 적절한 controller로 mapping 시켜줌

contextConfigLocation init-param 으로 원하는 spring configuraion file을 설정

 

<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app> <display-name>Archetype Created Web Application</display-name> <servlet> <servlet-name>dispatcher</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcher</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>

 

5: view: welcom.jsp

 

 

 

 

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>HelloWorld page</title>
</head>
<body>
    Greeting : ${greeting}
</body>
</html>