首页 技术 正文
技术 2022年11月15日
0 收藏 603 点赞 2,914 浏览 3799 个字

编写简单的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’)
}

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,088
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,564
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,413
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,186
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,822
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,905