全注解方式构建SpringMVC项目

摘要:
简述SpringBoot对Spring的的使用做了全面的封装,使用SpringBoot大大加快了开发进程,但是如果不了解Spring的特性,使用SpringBoot时会有不少问题目前网上流传使用IDEA比Eclipse效率更加高,在搭建项目时,也尝试使用IDEA,但是由于习惯问题,最终还是使用了Eclipse,以后也别再折腾了,专注于开发本身更加重要这是个简单的SpringMVC项目,目的在于帮助理解Spring4的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

全注解方式构建SpringMVC项目第1张

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

全注解方式构建SpringMVC项目第2张

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

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

全注解方式构建SpringMVC项目第3张

代码说明
public class TestMVCInitializer extendsAbstractAnnotationConfigDispatcherServletInitializer {
    @Override
    protected Class<?>[] getRootConfigClasses()
    {
        return new Class<?>[] { RootConfig.class};
    }

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

    @Override
    protectedString[] getServletMappings() {
        return new String[] { "/"};
    }
}

Spring4中通过继承AbstractAnnotationConfigDispatcherServletInitializer类,重写其方法实现web项目的配置,其中getRootConfigClasses方法定义了的配置类将用于ContextLoaderListener应用上下文的bean,getServletConfigClasses方法用于定义DispatcherServlet应用上下文中的bean,getServletMappings方法将DispatcherServlet映射到"/"

@Configuration
@EnableWebMvc
@ComponentScan("com.m24.controller")
public class WebConfig extendsWebMvcConfigurerAdapter {
    @Bean
    publicViewResolver viewResolver() {
        InternalResourceViewResolver resolver = newInternalResourceViewResolver();
        resolver.setPrefix("/WEB-INF/views/");
        resolver.setSuffix(".jsp");
        returnresolver;
    }

    @Override
    public voidconfigureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }

    @Override
    public voidaddResourceHandlers(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 classRootConfig {
    public static class WebPackage extendsRegexPatternTypeFilter {
        publicWebPackage() {
            super(Pattern.compile("com.m24.controller"));
        }
    }
}

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

@Import(DataConfig.class)
@Configuration
public classDataConfig {
    @Bean
    publicDataSource dataSource() {
        return newEmbeddedDatabaseBuilder()
                .setType(EmbeddedDatabaseType.H2)
                .addScript("schema.sql")
                .build();
    }

    @Bean
    publicJdbcOperations jdbcTemplate(DataSource dataSource) {
        return newJdbcTemplate(dataSource);
    }
}
问题小结

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

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

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

全注解方式构建SpringMVC项目第4张

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>

代码地址:https://github.com/m2492565210/testspringmvc

免责声明:文章转载自《全注解方式构建SpringMVC项目》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇[Hardware] 机械硬盘和固态硬盘功耗对比sql union和union all的用法及效率下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

MyBatis与Spring MVC结合时,使用DAO注入出现:Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required

错误源自使用了这个例子:http://www.yihaomen.com/article/java/336.htm,如果运行时会出现如下错误: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Property 'sqlSessi...

SpringMvc 项目配置

spring-mvc.xml <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instan...

SpringMVC 的 切面

官网路径:https://docs.spring.io/spring/docs/current/spring-framework-reference/core.html#beans 一:术语介绍 通知(Advice) 指切面(定义为aspect的类)中的工作; spring切面可以应用的五种通知: 前置通知(Before):在目标方法被调用之前调用通知功能...

SpringMVC介绍及参数绑定

本节内容: SpringMVC介绍 入门程序 SpringMVC架构 SpringMVC整合MyBatis 参数绑定 SpringMVC和Struts2的区别 一、SpringMVC介绍 1. 什么是SpringMVC Spring web mvc和Struts2都属于表现层的框架,它是Spring框架的一部分,我们可以从Spring的整体结构中看得出...

关于springMVC

一 mvc设计模式 MVC 全名是 Model View Controller,是 模型(model)-视图(view)-控制器(controller) 的缩写, 是⼀种⽤于设计创建 Web 应⽤程序表现层的模式。 MVC 中每个部分各司其职:Model(模型):模型包含业务模型和数据模型,数据模型⽤于封装数据,业务模型⽤于处理业务。View(视图): 通...

Spring和Spring MVC包扫描

在Spring整体框架的核心概念中,容器是核心思想,就是用来管理Bean的整个生命周期的,而在一个项目中,容器不一定只有一个,Spring中可以包括多个容器,而且容器有上下层关系,目前最常见的一种场景就是在一个项目中引入Spring和SpringMVC这两个框架,那么它其实就是两个容器,Spring是父容器,SpringMVC是其子容器,并且在Spring...