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')
    }


  • 相关阅读:
    javascript核心基础(正则专题)
    JsonUtil(工具类)
    身份证验证要用到的代码
    来cnblog了
    强智科技教务处模拟登录
    Thinkphp学习日记:jQuery_ajax数据提交
    在win8中使用服务器管理器来管理windows server 2012
    在windows server 2012运行锐捷客户端
    CentOS 6.4 配置LAMP 环境 与安装 phpmyadmin
    Linux下的光盘挂载与卸载
  • 原文地址:https://www.cnblogs.com/mingzhang/p/7997904.html
Copyright © 2011-2022 走看看