zoukankan      html  css  js  c++  java
  • 编写简单的spring mvc程序,在tomcat上部署


    编写简单的spring mvc程序,在tomcat上部署

    1 用java 配置spring mvc ,可以省去web.xml
    package hello;
    import org.springframework.web.servlet.support.
    AbstractAnnotationConfigDispatcherServletInitializer;

    public class SpittrWebAppInitializer
    extends AbstractAnnotationConfigDispatcherServletInitializer {
    @Override
    protected String[] getServletMappings() {
        return new String[] { "/" };
    }

    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class<?>[] { RootConfig.class };
    }

    @Override
    protected Class<?>[] getServletConfigClasses() {
        return new Class<?>[] { WebConfig.class };
    }
    }

    2 RootConfig.java
    package hello;
    import org.springframework.context.annotation.ComponentScan;
    import org.springframework.context.annotation.ComponentScan.Filter;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.annotation.FilterType;
    import org.springframework.web.servlet.config.annotation.EnableWebMvc;

    @Configuration
    @ComponentScan(basePackages={"spitter"},
    excludeFilters={
    @Filter(type=FilterType.ANNOTATION, value=EnableWebMvc.class)
    })
    public class RootConfig {
    }

    3 WebConfig.java
    package hello;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.ComponentScan;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.web.servlet.ViewResolver;
    import org.springframework.web.servlet.config.annotation.
    DefaultServletHandlerConfigurer;
    import org.springframework.web.servlet.config.annotation.EnableWebMvc;
    import org.springframework.web.servlet.config.annotation.
    WebMvcConfigurerAdapter;
    import org.springframework.web.servlet.view.
    InternalResourceViewResolver;

    @Configuration
    @EnableWebMvc
    @ComponentScan("hello")
    public class WebConfig extends WebMvcConfigurerAdapter {
        /*
    @Bean
    public ViewResolver viewResolver() {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/WEB-INF/views/");
        resolver.setSuffix(".jsp");
        resolver.setExposeContextBeansAsAttributes(true);
        return resolver;
    }

    @Override
    public void configureDefaultServletHandling(
        DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }
    */
    }

    4 controler
    package hello;

    import java.util.concurrent.atomic.AtomicLong;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.bind.annotation.RestController;

    @RestController
    public class GreetingController {

        private static final String template = "Hello, %s!";
        private final AtomicLong counter = new AtomicLong();

        @RequestMapping("/greeting")
        public Greeting greeting(@RequestParam(value="name", defaultValue="World") String name) {
            return new Greeting(counter.incrementAndGet(),
                                String.format(template, name));
        }
    }


    package hello;

    public class Greeting {

        private final long id;
        private final String content;

        public Greeting(long id, String content) {
            this.id = id;
            this.content = content;
        }

        public long getId() {
            return id;
        }

        public String getContent() {
            return content;
        }
    }


    5 启动程序
    package hello;

    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;

    @SpringBootApplication
    public class Application {

        public static void main(String[] args) {
            SpringApplication.run(Application.class, args);
        }
    }


    6 gradle
    buildscript {
        repositories {
            mavenCentral()
        }
        dependencies {
            classpath("org.springframework.boot:spring-boot-gradle-plugin:1.5.9.RELEASE")
        }
    }

    apply plugin: 'java'
    apply plugin: 'eclipse'
    apply plugin: 'idea'
    apply plugin: 'org.springframework.boot'
    apply plugin: 'war'

    jar {
        baseName = 'gs-rest-service'
        version =  '0.1.0'
    }

    repositories {
        mavenCentral()
    }

    sourceCompatibility = 1.8
    targetCompatibility = 1.8

    dependencies {
        compile("org.springframework.boot:spring-boot-starter-web")
        testCompile('org.springframework.boot:spring-boot-starter-test')
        testCompile('com.jayway.jsonpath:json-path')
    }


  • 相关阅读:
    2018-2019-120165226_20165310_20165315 实验四 外设驱动程序设计
    20165310 实验三-并发程序
    Exp2_固件程序设计 20165226_20165310_20165315
    实验楼缓冲区溢出实验报告
    2018-2019-2 《网络对抗技术》Exp4 恶意代码分析20165211
    2018-2019-2 《网络对抗技术》Exp3 免杀原理与实践 20165211
    2018-2019-2 《网络对抗技术》Exp2 后门原理与应用 20165211
    2018-2019-2 《网络对抗技术》Exp1 PC平台逆向破解 Week3 20165211
    2018-2019-2 《网络对抗技术》Exp0 Kali安装 Week1 20165211
    2018-2019-1 20165211 实验四 外设驱动程序设计
  • 原文地址:https://www.cnblogs.com/mingzhang/p/7997904.html
Copyright © 2011-2022 走看看