什么都别讲, 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">
<!-- <!– jdbc配置 –>-->
<!-- <context:property-placeholder location="classpath:config/jdbc.properties" />-->
<!-- <!– 自动将Service层注入–>-->
<!-- <context:component-scan base-package="com.songci.mytest_one.service" />-->
<!-- <!– dbcp数据源 –>-->
<!-- <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init"-->
<!-- destroy-method="close">-->
<!-- <!–这里如果写成${jdbc.driver},就会出现加载jdbc驱动失败的问题,暂时不清楚什么原因–>-->
<!-- <property name="driverClassName" value="${jdbc.driver}" />-->
<!-- <property name="url" value="${jdbc.url}" />-->
<!-- <property name="username" value="${jdbc.username}" />-->
<!-- <property name="password" value="${jdbc.password}" />-->
<!-- <!– 连接池最大使用连接数 –>-->
<!-- <property name="maxActive" value="20"/>-->
<!-- <!– 初始化连接大小 –>-->
<!-- <property name="initialSize" value="1"/>-->
<!-- <!– 获取连接最大等待时间 –>-->
<!-- <property name="maxWait" value="60000"/>-->
<!-- <!– 连接池最大空闲 –>-->
<!-- <property name="maxPoolPreparedStatementPerConnectionSize" value="20"/>-->
<!-- <!– 连接池最小空闲 –>-->
<!-- <property name="minIdle" value="3"/>-->
<!-- <!– 自动清除无用连接 –>-->
<!-- <property name="removeAbandoned" value="true"/>-->
<!-- <!– 清除无用连接的等待时间 –>-->
<!-- <property name="removeAbandonedTimeout" value="180"/>-->
<!-- <!– 连接属性 –>-->
<!-- <property name="connectionProperties" value="clientEncoding=UTF-8"/>-->
<!-- </bean>-->
<!-- <!– mybatis的配置文件 –>-->
<!-- <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>-->
<!-- <!– spring与mybatis整合配置,扫描所有dao 和所有mapper文件 –>-->
<!-- <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>-->
<!-- <!– 写操作 –>-->
<!-- <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"/>-->
<!-- <!– 读操作 –>-->
<!-- <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.*.*(..))"/>-->
<!-- <!–把事务控制在Service层–>-->
<!-- <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>
回顾一下 多模块配置的目录
下面是我的代码地址
git@github.com:idea-your-father/ssm_parent.git
https://github.com/idea-your-father/ssm_parent