好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

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

简述

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

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

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

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

项目搭建

依次选择file、new、spring legacy project

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

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

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

代码说明

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

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映射到"/"

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

@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配置类

最后是业务相关配置类

?

1

2

3

4

5

6

7

8

9

10

11

@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测试数据pile( "com.m24.controller" ));

     }

   }

}

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

?

1

@import (dataconfig. class )

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

@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测试数据');

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

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

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

<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文件中添加

?

1

2

3

4

5

6

7

8

<plugin>

         <groupid>org.apache.maven.plugins</groupid>

         <artifactid>maven-war-plugin</artifactid>

         <version> 2.6 </version>

         <configuration>

           <failonmissingwebxml> false </failonmissingwebxml>

         </configuration>

</plugin>

4、定义java版本

?

1

2

3

4

5

6

7

8

9

10

<!-- 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、指定项目名

?

1

<finalname>testspringmvc</finalname>

代码地址: https://github测试数据/m2492565210/testspringmvc

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

原文链接:http://HdhCmsTestcnblogs测试数据/m2492565210/p/8328070.html

查看更多关于详解如何全注解方式构建SpringMVC项目的详细内容...

  阅读:18次