当前位置: 移动技术网 > IT编程>开发语言>Java > 详解如何全注解方式构建SpringMVC项目

详解如何全注解方式构建SpringMVC项目

2019年07月19日  | 移动技术网IT编程  | 我要评论

简述

springboot对spring的的使用做了全面的封装,使用springboot大大加快了开发进程,但是如果不了解spring的特性,使用springboot时会有不少问题

目前网上流传使用idea比eclipse效率更加高,在搭建项目时,也尝试使用idea,但是由于习惯问题,最终还是使用了eclipse,以后也别再折腾了,专注于开发本身更加重要

这是个简单的springmvc项目,目的在于帮助理解spring4的springmvc的搭建,采用注解方式。项目简单得不能再简单,采用tomcat+spring+springmvc+h2方式搭建。项目启动后,在访问栏输入访问地址http://www.lhsxpumps.com/_localhost:8080/testspringmvc/后直接访问,利用访问地址http://localhost:8080/testspringmvc/user/10001检测功能是否正常运行,输出结果是为一串json字串(java直接转换)

文章分为3部分,项目搭建,代码说明,以及在这过程中遇到的问题的小结

项目搭建

依次选择file、new、spring legacy project

在弹出的对话框中选择spring mvc项目,填写项目其他信息

最后生成的springmvc项目的pom文件中要做些修改,因为这时生成的项目使用的是spring3,而这次的目的是练习使用spring4 (这里做个标志,以后有时间回来看看怎样可以直接生成spring4的)

至此,项目已经生成,项目文件结构如下

代码说明

public class testmvcinitializer extends abstractannotationconfigdispatcherservletinitializer {
  @override
  protected class<?>[] getrootconfigclasses()
  {
    return new class<?>[] { rootconfig.class };
  }

  @override
  protected class<?>[] getservletconfigclasses() {
    return new class<?>[] { webconfig.class };
  }

  @override
  protected string[] getservletmappings() {
    return new string[] { "/" };
  }
}

spring4中通过继承abstractannotationconfigdispatcherservletinitializer类,重写其方法实现web项目的配置,其中getrootconfigclasses方法定义了的配置类将用于contextloaderlistener应用上下文的bean,getservletconfigclasses方法用于定义dispatcherservlet应用上下文中的bean,getservletmappings方法将dispatcherservlet映射到"/"

@configuration
@enablewebmvc
@componentscan("com.m24.controller")
public class webconfig extends webmvcconfigureradapter {
  @bean
  public viewresolver viewresolver() {
    internalresourceviewresolver resolver = new internalresourceviewresolver();
    resolver.setprefix("/web-inf/views/");
    resolver.setsuffix(".jsp");
    return resolver;
  }

  @override
  public void configuredefaultservlethandling(defaultservlethandlerconfigurer configurer) {
    configurer.enable();
  }

  @override
  public void addresourcehandlers(resourcehandlerregistry registry) {
    super.addresourcehandlers(registry);
  }
}

此处注意的是使用@enablewebmvc,是springmvc配置类

最后是业务相关配置类

@configuration
@import(dataconfig.class)
@componentscan(basepackages = {"com.m24"},
  excludefilters = @filter(type=filtertype.custom, value=rootconfig.webpackage.class))
public class rootconfig {
  public static class webpackage extends regexpatterntypefilter {
    public webpackage() {
      super(pattern.compile("com.m24.controller"));
    }
  }
}

由于该配置类中使用了h2数据库,所以还需要引入h2的配置类

@import(dataconfig.class)
@configuration
public class dataconfig {
  @bean
  public datasource datasource() {
    return new embeddeddatabasebuilder()
        .settype(embeddeddatabasetype.h2)
        .addscript("schema.sql")
        .build();
  }

  @bean
  public jdbcoperations jdbctemplate(datasource datasource) {
    return new jdbctemplate(datasource);
  }
}

问题小结

1、提供数据库插入语句时,正确的是

insert into user values(10001, 'mvc', '123456', 'm', 'vc', 'mvc@m24.com');

在开始时使用双引号,后台出现未识别列的的错误,经查找

2、使用@responsebody时,提示找不到合适的转换器,要引入依赖

 <dependency>
      <groupid>com.fasterxml.jackson.core</groupid>
      <artifactid>jackson-core</artifactid>
      <version>2.5.0</version>
    </dependency>
    <dependency>
      <groupid>com.fasterxml.jackson.core</groupid>
      <artifactid>jackson-annotations</artifactid>
      <version>2.5.0</version>
    </dependency>
    <dependency>
      <groupid>com.fasterxml.jackson.core</groupid>
      <artifactid>jackson-databind</artifactid>
      <version>2.5.0</version>
    </dependency>

3、由于使用注解方式,没有web.xml文件,项目报错,缺失web.xml文件,pom文件中添加

<plugin>
        <groupid>org.apache.maven.plugins</groupid>
        <artifactid>maven-war-plugin</artifactid>
        <version>2.6</version>
        <configuration>
          <failonmissingwebxml>false</failonmissingwebxml>
        </configuration>
</plugin>

4、定义java版本

<!-- define the project compile level -->
      <plugin>
        <groupid>org.apache.maven.plugins</groupid>
        <artifactid>maven-compiler-plugin</artifactid>
        <version>2.3.2</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>

5、指定项目名

<finalname>testspringmvc</finalname>

代码地址:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网