使用maven初始化springmvc项目

spring起步

最近在学习使用maven和springmvc,中间遇到了各种问题。定这篇文章就是为了让自己记住。看似清晰的道理只有被清晰的表达出来后才会清晰。

  1. 使用maven初始化springmvc项目一开始需要强入哪些依赖。
  2. maven黙认的jdk版本是1.5,如何改变。
  3. 在eclipse中初始化maven项目改变web.xml版本的时候总是出错怎么解决。
  4. spring中引入的各个包是什么用的,各有什么功能。
  5. spring如何初始化配置。

首先用eclipse创建一个maven项目。maven会自动生成pom.xml文件和生成相应的目录结构,如下图所示。然后修改pom文件,引入主流的相关依赖及配置。

maven生成的目录结构

相关依赖及配置包括以下几个:

  1. servlet-api
  2. jstl
  3. 自动测试
  4. spring相关
  5. mybatis
  6. json序列化包
  7. 数据库连接池
  8. 日志
  9. 数据库驱动
  10. 文件上传
  11. maven自动编译插件
  12. 项目编码格式

pom文件写好之后,还需要修改web.xml文件。maven黙认的web.xml版本是2.4的。这个版本很低,并不会自动支持jstl相关标签,所以需要修改为3.1版本。3.1版本的xml头如下:


<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
                      http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
    version="3.1" metadata-complete="true">
  <display-name>XXX</display-name>
</web-app>

但是直接这个修改,eclipse这个天杀的ide就是报错。我之前也在网上找过各种解决方案。最简单的方法就是先删除项目,但是不在硬盘上删除。删除项目之后,再删除这个项目目录下.setting文件夹、.classpath文件、.project文件。再用eclipse导入这个maven项目,这个错误就解决了。

eclipse报错

将下来在web.xml文件中配置spring mvc的dispatcher。让spring mvc接管所有的请求。

    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/configs/spring/dispatcher-servlet.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

配置spring mvc dispatcher就是跟配置普通的servlet一样。这里需要注意的是:如果不配置servlet中的contextConfigLocation,DispatcherServlet会在WEB-INF目录下寻找自己的配置文件。DispatcherServlet的配置文件默认文件名为配置的servlet名加下-servlet.xml。如果我上面的配置中把init-param标签删除掉的话,DispatcherServlet会在WEB-INF目录下寻找dispatcher-servlet.xml文件。

再看下一般dispatcher-servlet.xml是怎么写的。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">
    
    <context:annotation-config />
   
    <context:component-scan base-package="work.parabola.bind.controller">
        <context:include-filter type="annotation"
            expression="org.springframework.stereotype.Controller" />
    </context:component-scan>

    <mvc:annotation-driven />
    <mvc:resources mapping="/resources/**" location="/resources/" />

    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass"
            value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/WEB-INF/jsps/" />
        <property name="suffix" value=".jsp" />
    </bean>
</beans>

<context:annotation-config />
这个标签开启spring注解功能。

下面这一行表示开始包扫描,扫描 base-package包中的控制器。也就是在类上标注为@Controller的类。

<mvc:annotation-driven /> 扩充了注解驱动,可以将请求参数绑定到控制器参数

<mvc:resources mapping="/resources/**" location="/resources/" /> 静态文件处理,请求resources文件夹下的静态文件不需要经过控制器处理。

最后配置视图。spring mvc支持多种视图,目前我也还是刚开始学习。目前先使用这么一种,以后的东西以后再说。

dispatcher配置完之后继续配置spring的其他东西。在web.xml中配置spring容器,也就是spring应用上下文。

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/configs/spring/applicationContext*.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

首先配置spring应用上下文配置文件的路径,然后配置一个监听器就行了。接下来看看applicationContext.xml文件的配置。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">
    <context:annotation-config />
    <context:component-scan base-package="work.parabola.bind">
        <context:exclude-filter type="annotation"
            expression="org.springframework.stereotype.Controller" />
    </context:component-scan>
</beans>

初始的spring配置文件很简单,只是启用了注解和包扫描,将之前通过dispatcher扫描的控制器组排除。这样就是一个spring mvc初始项目的配置。接下来就可能使用这个基础项目进行学习了。

我的这个基出项目配置已经放到 https://git.coding.net/whatcanhumando/bind.git,方便别人和自己下载或者查看。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容