springMVC maven多模块项目搭建

在这里插入图片描述

什么都别讲, maven多模块工程,先新建一个 默认 的 父工程,删除 src目录,再建个 web 模块

在这里插入图片描述
web.xml 写对了你就成功了一半

现在 resources目录下 建立一个 config目录

然后我贴 web.xml 配置

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:config/applicationContext.xml</param-value>
  </context-param>
  <context-param>
    <param-name>log4jConfigLocation</param-name>
    <param-value>classpath:config/log4j.properties</param-value>
  </context-param>
  <context-param>
    <param-name>log4jRefreshInterval</param-name>
    <param-value>6000</param-value>
  </context-param>
  <filter>
    <filter-name>SpringEncodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
      <param-name>encoding</param-name>
      <param-value>UTF-8</param-value>
    </init-param>
    <init-param>
      <param-name>forceEncoding</param-name>
      <param-value>true</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>SpringEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  <listener>
    <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
  </listener>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <servlet>
    <servlet-name>springMVC-SERVLET</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>
        classpath:config/spring-mvc.xml
      </param-value>
    </init-param>
    <load-on-startup>2</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>springMVC-SERVLET</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>




  <filter>
    <filter-name>sitemesh</filter-name>
    <filter-class>org.sitemesh.config.ConfigurableSiteMeshFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>sitemesh</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>

第一个注意点: 配置 context-parm 好让扫描 到 bean.xml
即 spring 的配置文件的位置

在这里插入图片描述第二个注意点: 配置 spring mvC 默认 的 servlet ,让这个 servlet 拦截 根路径的所有请求
在这里插入图片描述parm-value 指定spring MVC 配置文件 spring-mvc.xml 的文件位置

第3个要点:
resources 目录下 的 config 下 带个 spring 的配置文件,这里web.xml 起名叫 application.xml ,所以 也要和 web.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:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-4.0.xsd
           http://www.springframework.org/schema/aop
           http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
           http://www.springframework.org/schema/tx
           http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">

<!--    &lt;!&ndash; jdbc配置 &ndash;&gt;-->
<!--    <context:property-placeholder location="classpath:config/jdbc.properties" />-->

<!--    &lt;!&ndash; 自动将Service层注入&ndash;&gt;-->
<!--    <context:component-scan base-package="com.songci.mytest_one.service" />-->


<!--    &lt;!&ndash; dbcp数据源 &ndash;&gt;-->
<!--    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init"-->
<!--          destroy-method="close">-->
<!--        &lt;!&ndash;这里如果写成${jdbc.driver},就会出现加载jdbc驱动失败的问题,暂时不清楚什么原因&ndash;&gt;-->
<!--        <property name="driverClassName" value="${jdbc.driver}" />-->
<!--        <property name="url" value="${jdbc.url}" />-->
<!--        <property name="username" value="${jdbc.username}" />-->
<!--        <property name="password" value="${jdbc.password}" />-->

<!--        &lt;!&ndash; 连接池最大使用连接数 &ndash;&gt;-->
<!--        <property name="maxActive" value="20"/>-->
<!--        &lt;!&ndash; 初始化连接大小 &ndash;&gt;-->
<!--        <property name="initialSize" value="1"/>-->
<!--        &lt;!&ndash; 获取连接最大等待时间 &ndash;&gt;-->
<!--        <property name="maxWait" value="60000"/>-->
<!--        &lt;!&ndash; 连接池最大空闲 &ndash;&gt;-->
<!--        <property name="maxPoolPreparedStatementPerConnectionSize" value="20"/>-->
<!--        &lt;!&ndash; 连接池最小空闲 &ndash;&gt;-->
<!--        <property name="minIdle" value="3"/>-->
<!--        &lt;!&ndash; 自动清除无用连接 &ndash;&gt;-->
<!--        <property name="removeAbandoned" value="true"/>-->
<!--        &lt;!&ndash; 清除无用连接的等待时间 &ndash;&gt;-->
<!--        <property name="removeAbandonedTimeout" value="180"/>-->
<!--        &lt;!&ndash; 连接属性 &ndash;&gt;-->
<!--        <property name="connectionProperties" value="clientEncoding=UTF-8"/>-->
<!--    </bean>-->

<!--    &lt;!&ndash; mybatis的配置文件 &ndash;&gt;-->
<!--    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">-->
<!--        <property name="dataSource" ref="dataSource"/>-->
<!--        <property name="configLocation" value="classpath:config/mybatis-config.xml"/>-->
<!--        <property name="mapperLocations" value="classpath*:mapper/*Mapper.xml"/>-->
<!--    </bean>-->

<!--    &lt;!&ndash; spring与mybatis整合配置,扫描所有dao 和所有mapper文件 &ndash;&gt;-->
<!--    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">-->
<!--        <property name="basePackage" value="com.songci.mytest_one.dao"/>-->
<!--        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>-->
<!--    </bean>-->

<!--    <bean id="transactionManager"-->
<!--          class="org.springframework.jdbc.datasource.DataSourceTransactionManager">-->
<!--        <property name="dataSource" ref="dataSource"/>-->
<!--    </bean>-->
<!--    <tx:advice id="txAdvice" transaction-manager="transactionManager">-->
<!--        <tx:attributes>-->
<!--            &lt;!&ndash; 写操作 &ndash;&gt;-->
<!--            <tx:method name="insert*" propagation="REQUIRED" isolation="DEFAULT"/>-->
<!--            <tx:method name="update*" propagation="REQUIRED" isolation="DEFAULT"/>-->
<!--            <tx:method name="delete*" propagation="REQUIRED" isolation="DEFAULT"/>-->
<!--            <tx:method name="batch*" propagation="REQUIRED" isolation="DEFAULT"/>-->

<!--            &lt;!&ndash; 读操作 &ndash;&gt;-->
<!--            <tx:method name="load*" propagation="REQUIRED" isolation="DEFAULT" read-only="true"/>-->
<!--            <tx:method name="get*" propagation="REQUIRED" isolation="DEFAULT" read-only="true"/>-->
<!--            <tx:method name="find*" propagation="REQUIRED" isolation="DEFAULT" read-only="true"/>-->

<!--            <tx:method name="*" propagation="REQUIRED" isolation="DEFAULT"/>-->
<!--        </tx:attributes>-->
<!--    </tx:advice>-->

<!--    <aop:config>-->
<!--        <aop:pointcut id="pc" expression="execution(public * com.songci.mytest_one.service.*.*(..))"/>-->
<!--        &lt;!&ndash;把事务控制在Service层&ndash;&gt;-->
<!--        <aop:advisor advice-ref="txAdvice" pointcut-ref="pc"/>-->
<!--    </aop:config>-->
</beans>

有就行了,别管它

同级目录下 带个 spring-mvc 的配置文件

<?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"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
            http://www.springframework.org/schema/mvc
            http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-4.0.xsd
           http://www.springframework.org/schema/aop
           http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
           http://www.springframework.org/schema/tx
           http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">

    <!-- 扫描controller(controller层注入) -->
    <context:component-scan base-package="com.ssm.demo.controller"/>

    <!-- 启动注解支持 -->
    <mvc:annotation-driven />

    <!-- 静态资源 -->
    <!--<mvc:resources location="/WEB-INF/js/" mapping="/js/**"/>-->
    <!--<mvc:resources location="/WEB-INF/css/" mapping="/css/**"/>-->
    <!--<mvc:resources location="/WEB-INF/image/" mapping="/image/**"/>-->

    <!-- 视图解析器 -->
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/view/" />
        <property name="suffix" value=".jsp" />
        <property name="order" value="2"/>
    </bean>

    <!-- 避免IE在ajax请求时,返回json出现下载 -->
    <bean id="jacksonMessageConverter"
          class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
        <property name="supportedMediaTypes">
            <list>
                <value>text/html;charset=UTF-8</value>
                <value>application/json;charset=UTF-8</value>
            </list>
        </property>
    </bean>

    <!--Spring3.1开始的注解 HandlerMapping -->
    <!--3.1之后必须存在, 不解-->
    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>
    <!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 -->
    <!--Spring3.1开始的注解 HandlerAdapter -->
    <!--Spring3.1之前的注解 HandlerAdapter org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter -->
    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
        <property name="messageConverters">
            <list>
                <ref bean="jacksonMessageConverter"/>
                <!-- json转换器 -->
            </list>
        </property>
    </bean>
    <!--文件上传限制-->
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="defaultEncoding" value="UTF-8"/>
        <property name="maxUploadSize" value="5242880"/>
    </bean>

</beans>

回顾一下 多模块配置的目录

在这里插入图片描述下面是我的代码地址

[email protected]:idea-your-father/ssm_parent.git

https://github.com/idea-your-father/ssm_parent

发布了174 篇原创文章 · 获赞 7 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_43923045/article/details/105472987