[Spring] 간단 한 Spring MVC 입문 예

머리말
테스트 특성 은 간단 한 Spring MVC 의 예 를 만들어 기록 해 야 한다. 예 일 뿐 입문 자의 예 일 뿐이다.
버 전 설명
POM 파일 을 설명 하고 도입 할 JAR 을 지정 합 니 다.
<properties>
        <spring.version>4.0.9.RELEASE</spring.version>
    </properties>

    <dependencies>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
        </dependency>
        
        <dependency>
            <groupId>org.springframework.webflow</groupId>
            <artifactId>spring-webflow</artifactId>
            <version>2.3.4.RELEASE</version>
        </dependency>

    </dependencies>

웹. xml 에 할당 기 설정
<!-- Spring MVC, DispatchcerServlet -->
    <servlet>
        <servlet-name>SpringDispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring-mvc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>SpringDispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

Spring MVC 설정 지정
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:task="http://www.springframework.org/schema/task"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
           http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
           http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd">

    <!--   BEAN      -->
    <context:component-scan base-package="controller,service" />

    <!--               -->
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsps/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>

</beans>

Controller 예 를 들 어 자주 사용 하 는 두 가지 방식 을 포함 합 니 다. 뛰 기, 되 돌려 주기 문자열 (예 를 들 어 JSON)
package controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import service.UserService;

@Controller
@RequestMapping("user")
public class UserController {
    
    @Autowired
    private UserService userService;
    
    @RequestMapping("update")
    public String update() {
        this.userService.update();
        return "hello"; //   hello.jsp  
    }
    
    @RequestMapping("isUser")
    @ResponseBody
    public String isUser() {
        this.userService.update();
        return "yes"; //      
    }

}

간단 한 서비스
package service;

import org.springframework.stereotype.Service;

@Service
public class UserService {
    
    public void update() {
        System.out.println("update user.");
    }
    
}

좋은 웹페이지 즐겨찾기