Spring Mvc 4 HelloWord


プロジェクトの構築
jarパッケージの導入
  • spring-aop-4.2.0.RELEASE.jar
  • spring-beans-4.2.0.RELEASE.jar
  • spring-context-4.2.0.RELEASE.jar
  • spring-core-4.2.0.RELEASE.jar
  • spring-expression-4.2.0.RELEASE.jar
  • spring-web-4.2.0.RELEASE.jar
  • spring-webmvc-4.2.0.RELEASE.jar
  • commons-logging-1.2.jar

  • 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" id="WebApp_ID" version="3.1">
        <!-- Spring  Servlet   -->
        <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:springmvc.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>
    </web-app>

    springmvcを作成する.xml
    springmvc.xmlは上のclasspath:springmvcと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" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd">
        <!--          -->
        <context:component-scan base-package="com.sanchan.server.controller"></context:component-scan>
        <!--          : Controller               -->
        <bean  class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/WEB-INF/views/"></property>
            <property name="suffix" value=".jsp"></property>
        </bean>
    </beans>

    #作成Controller srcでcomを作成する.sanchan.server.controller.HelloWorld
    package com.sanchan.server.controller;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    //     Spring    ,      
    @Controller
    public class HelloWorld {
        /** * 1、 @RequestMapping      URL * 2、                     ,  org.springframework.web.servlet.view. * InternalResourceViewResolver     ,      : * prefix【 springmvc.xml    prefix 】+  return    +suffix【 springmvc.xml    suffix 】 * @return */
        @RequestMapping("/helloworld")
        public String hello() {
            System.out.print("hello world");
            return "success";
        }
    }

    WEB-INFの下に/views/successを作成する.jsp
    これで簡単なspring mvcが完成しました