Spring 3 MVC hello worldの例 - 注釈

Spring 3 MVC Hello Worldの例–注釈

このチュートリアルでは、以前のMaven + Spring MVC XML exampleを取得し、@JavaConfigの構成をサポートするように書き直し、XMLファイルを追加せずに、Tomcat7やJetty9などのサーブレット3.0以降のコンテナーにデプロイします。

使用される技術:

  1. 春3.2.13.RELEASE

  2. メーベン3

  3. JDK 1.6

  4. Tomcat 7またはJetty 9

  5. Eclipse 4.4

  6. ブーストラップ3

Spring 4 MVC Annotation
このSpring 4 MVC hello world example – Annotationを試してください。

1. プロジェクト構造

プロジェクトsource codeをダウンロードし、プロジェクトのフォルダー構造を確認します。

spring3-mvc-hello-world-annotation

P.S No more XML files like web.xml or any other Spring XML configuration files.

2. メーベン

2.1 A pom.xml template to quick start a Spring MVC project. このプロジェクトをコンパイルするには、servlet-apiの依存関係を追加する必要があります。

pom.xml


  4.0.0
  com.example
  spring3-mvc-maven-annotation-hello-world
  war
  1.0-SNAPSHOT
  spring mvc

  
    1.6
    3.2.13.RELEASE
    1.2
    3.1.0
  

  

    
        org.springframework
        spring-webmvc
        ${spring.version}
    

    
        javax.servlet
        jstl
        ${jstl.version}
    

    
    
        javax.servlet
        javax.servlet-api
        ${servletapi.version}
        provided
    

  

  
    

    
        org.apache.maven.plugins
        maven-compiler-plugin
        3.3
        
            ${jdk.version}
            ${jdk.version}
        
    

    
    
        org.eclipse.jetty
        jetty-maven-plugin
        9.2.11.v20150529
        
            10
            
                /spring3
            
        
    

    
    
        org.apache.maven.plugins
        maven-eclipse-plugin
        2.9
        
            true
            true
            2.0
            spring3
        
    

    
  

  

2.2 To compile this project and make it supports Eclipse IDE.

ターミナル

$ mvn eclipse:eclipse

3. スプリングコントローラー

HelloController.java

package com.example.web.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class HelloController {

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String printWelcome(ModelMap model) {

        model.addAttribute("message", "Spring 3 MVC Hello World");
        return "hello";

    }

    @RequestMapping(value = "/hello/{name:.+}", method = RequestMethod.GET)
    public ModelAndView hello(@PathVariable("name") String name) {

        ModelAndView model = new ModelAndView();
        model.setViewName("hello");
        model.addObject("msg", name);

        return model;

    }

}

4. JSPビュー

値を表示し、ブートストラップcssおよびjsを含むJSPページ。

html4strict

<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>



Maven + Spring MVC + @JavaConfig









${title}

Hello ${name} Welcome Welcome!

Learn more

Heading

ABC

View details

Heading

ABC

View details

Heading

ABC

View details


© Mkyong.com 2015

5. Spring @JavaConfig

SpringWebConfig.java

package com.example.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;

@EnableWebMvc //mvc:annotation-driven
@Configuration
@ComponentScan({ "com.example.web" })
public class SpringWebConfig extends WebMvcConfigurerAdapter {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
    }

    @Bean
    public InternalResourceViewResolver viewResolver() {
        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
        viewResolver.setViewClass(JstlView.class);
        viewResolver.setPrefix("/WEB-INF/views/jsp/");
        viewResolver.setSuffix(".jsp");
        return viewResolver;
    }

}

同等のXML。



    

    
        
            /WEB-INF/views/jsp/
        
        
            .jsp
        
    

    

    

6. サーブレット3.0+コンテナ

AbstractAnnotationConfigDispatcherServletInitializerを拡張してServletInitializerクラスを作成すると、サーブレット3.0以降のコンテナがこのクラスを取得して自動的に実行します。 これはweb.xmlの代わりです。

MyWebInitializer.java

package com.example.servlet3;

import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

import com.example.config.SpringWebConfig;

public class MyWebInitializer extends
        AbstractAnnotationConfigDispatcherServletInitializer {

    @Override
    protected Class[] getServletConfigClasses() {
        return new Class[] { SpringWebConfig.class };
    }

    @Override
    protected String[] getServletMappings() {
        return new String[] { "/" };
    }

    @Override
    protected Class[] getRootConfigClasses() {
        return null;
    }

}

7. Demo

プロジェクトをダウンロードして、組み込みのJettyコンテナーで実行します。

ターミナル

$ mvn jetty:run

spring3-mvc-maven-xml-demo

spring3-mvc-maven-xml-demo2

ソースコードをダウンロード

ダウンロード–spring3-mvc-maven-annotation-hello-world(47 KB)