1.springmvc是spring框架的一个模块,springmvc和spring无需通过中间整合层进行整合。springmvc是一个基于mvc的web框架。mvc的思想大家已经很熟悉了,简称“Model-View-Controller”。
下面先简单介绍下我对spring-mvc的理解。
上面这张图大概说明了springmvc的运行过程,看起来可能有点云里雾里的,总结起来就是下面这些:
- 客户端发起请求到前端控制器(DispatcherServlet).
- 前端控制器请求HandlerMappering 查找Handler,可以根据xml配置、注解进行查找。
- DispatcherServlet将请求提交到Controller;
- Controller调用业务逻辑处理后,返回ModelAndView;
- DispatcherServlet查询一个或多个ViewResoler视图解析器,找到ModelAndView指定的视图;
- 视图负责将结果显示到客户端。
这里稍微解释下常用的几个组件名称和作用。
- 前端控制器(DispatcherServlet):用于接收请求,响应结果
- 处理器映射器(HandlerMapping):根据请求的url查找Handler(三大核心组件之一)
- 处理器适配器(HandlerAdapter):按照特定的规则去执行Handler(三大核心组件之一)
- 处理器(Handler):编写Handler时按照HandlerAdapter的要求去做,这样适配器才可以去正确执行Handler
- 视图解析器(View resolver):进行视图解析(三大核心组件之一)
- 视图(View):包括jsp、pdf等
在了解了上面的基础原理后下面来讲下几个主要的配置文件。项目开发前所需要的jar包提前要导入项目工程里面去。
有几个主要的配置文件,先了解下每个配置文件的作用。
1. web.xml:当服务启动时首先会去加载web.xml这个资源文件,里面包括了对前端控制器、乱码问题等配置。
2.applicatonContext.xml : 一般配置数据源,事物,注解 等。
在这里我使用的是applicatonContext-*.xml的形式将DAO层、Service层、Transaction层分开配置,这样便于管理
分别为applicatonContext-dao.xml、applicatonContext-service.xml、applicatonContext-transaction.xml
分开配置时,需要在web.xml中配置上下文位置
3.springmvc.xml: 里面配置的是控制层的 ,如视图解析器静态资源, mvc 文件上传,拦截器等。
4.SqlMapConfig.xml: 该配置文件为MyBatis的配置文件,里面无需配置,一切交给spring管理,但是xml文件基础配置要有。
以下是配置文件代码:
web.xml
[html] view plain copy
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns="http://java.sun.com/xml/ns/javaee"
- xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
- id="WebApp_ID" version="2.5">
- <display-name>ssm_boot_crm</display-name>
- <welcome-file-list>
- <welcome-file>index.html</welcome-file>
- <welcome-file>index.htm</welcome-file>
- <welcome-file>index.jsp</welcome-file>
- <welcome-file>default.html</welcome-file>
- <welcome-file>default.htm</welcome-file>
- <welcome-file>default.jsp</welcome-file>
- </welcome-file-list>
- <!-- 上下文的位置 -->
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>classpath:applicationContext-*.xml</param-value>
- </context-param>
- <!-- Spring的监听器 -->
- <listener>
- <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
- </listener>
- <!-- POST提交过滤器 UTF-8 -->
- <filter>
- <filter-name>encoding</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>
- </filter>
- <filter-mapping>
- <filter-name>encoding</filter-name>
- <url-pattern>*.action</url-pattern>
- </filter-mapping>
- <!-- 前端控制器 -->
- <servlet>
- <servlet-name>crm</servlet-name>
- <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
- <init-param>
- <param-name>contextConfigLocation</param-name>
- <!-- 此处不配置 默认找 /WEB-INF/[servlet-name]-servlet.xml -->
- <param-value>classpath:springmvc.xml</param-value>
- </init-param>
- <load-on-startup>1</load-on-startup>
- </servlet>
- <servlet-mapping>
- <servlet-name>crm</servlet-name>
- <!-- 1:*.do *.action 拦截以.do结尾的请求 (不拦截 jsp png jpg .js .css)
- 2:/ 拦截所有请求 (不拦截.jsp) 建议使用此种 方式 (拦截 .js.css .png) (放行静态资源)
- 3:/* 拦截所有请求(包括.jsp) 此种方式 不建议使用 -->
- <url-pattern>*.action</url-pattern>
- </servlet-mapping>
- </web-app>
applicationContext-dao.xml
[html] view plain copy
- <?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:mvc="http://www.springframework.org/schema/mvc"
- 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:task="http://www.springframework.org/schema/task" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
- 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
- http://www.springframework.org/schema/task
- http://www.springframework.org/schema/task/spring-task-4.0.xsd
- http://code.alibabatech.com/schema/dubbo
- http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
- <!-- srping框架 配置文件 用于管理数据库连接池 -->
- <!-- 配置 读取properties文件 db.properties -->
- <context:property-placeholder location="classpath:db.properties" />
- <!-- 配置 数据源 用于连接数据库 -->
- <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
- <!-- 数据库驱动 -->
- <property name="driverClassName" value="${jdbc.driver}" />
- <!-- 连接地址 -->
- <property name="url" value="${jdbc.url}" />
- <!-- 用户名 -->
- <property name="username" value="${jdbc.username}" />
- <!-- 密码 -->
- <property name="password" value="${jdbc.password}" />
- </bean>
- <!-- 配置 Mybatis的工厂 -->
- <bean class="org.mybatis.spring.SqlSessionFactoryBean">
- <!-- 绑定数据源 -->
- <property name="dataSource" ref="dataSource" />
- <!-- 配置Mybatis的核心 配置文件所在位置 -->
- <property name="configLocation" value="classpath:SqlMapConfig.xml" />
- <!-- 配置pojo别名 -->
- <property name="typeAliasesPackage" value="com.company.ssm.crm.pojo" />
- </bean>
- <!-- 配置 1:原始Dao开发 接口实现类 Mapper.xml 三个
- 2:接口开发 接口 不写实现类 Mapper.xml 二个 (UserDao、ProductDao
- 、BrandDao。。。。。。。)
- 3:接口开发、并支持扫描 cn.itcast.core.dao(UserDao。。。。。) 写在此包下即可被扫描到
- -->
- <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
- <property name="basePackage" value="com.company.ssm.crm.dao" />
- </bean>
- </beans>
applicationContext-service.xml
[html] view plain copy
- <?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:mvc="http://www.springframework.org/schema/mvc"
- 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:task="http://www.springframework.org/schema/task" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
- 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
- http://www.springframework.org/schema/task
- http://www.springframework.org/schema/task/spring-task-4.0.xsd
- http://code.alibabatech.com/schema/dubbo
- http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
- <!-- 配置扫描包 扫描 @Service spring代理管理业务层 -->
- <context:component-scan base-package="com.company.ssm.crm.service" />
- </beans>
applicationContext-transaction.xml
[html] view plain copy
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
- xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- 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
- http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
- <!-- spring 事务管理器 -->
- <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="save*" propagation="REQUIRED" />
- <tx:method name="insert*" propagation="REQUIRED" />
- <tx:method name="add*" propagation="REQUIRED" />
- <tx:method name="create*" propagation="REQUIRED" />
- <tx:method name="delete*" propagation="REQUIRED" />
- <tx:method name="update*" propagation="REQUIRED" />
- <tx:method name="find*" propagation="SUPPORTS" read-only="true" />
- <tx:method name="select*" propagation="SUPPORTS" read-only="true" />
- <tx:method name="get*" propagation="SUPPORTS" read-only="true" />
- </tx:attributes>
- </tx:advice>
- <!-- AOP 切面 -->
- <aop:config>
- <aop:advisor advice-ref="txAdvice"
- pointcut="execution(* cn.itcast.core.service.*.*(..))" />
- </aop:config>
- </beans>
springmvc.xml
[html] view plain copy
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
- 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:task="http://www.springframework.org/schema/task" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
- 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
- http://www.springframework.org/schema/task
- http://www.springframework.org/schema/task/spring-task-4.0.xsd
- http://code.alibabatech.com/schema/dubbo
- http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
- <!-- 加载属性文件 -->
- <context:property-placeholder location="classpath:resource.properties" />
- <!-- 配置扫描 器 -->
- <context:component-scan base-package="com.company.ssm.crm.controller" />
- <!-- 配置处理器映射器 适配器 -->
- <mvc:annotation-driven />
- <!-- 配置视图解释器 jsp -->
- <bean id="jspViewResolver"
- class="org.springframework.web.servlet.view.InternalResourceViewResolver">
- <property name="prefix" value="/WEB-INF/jsp/" />
- <property name="suffix" value=".jsp" />
- </bean>
- </beans>
SqlMapConfig.xml
[html] view plain copy
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
- 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:task="http://www.springframework.org/schema/task" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
- 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
- http://www.springframework.org/schema/task
- http://www.springframework.org/schema/task/spring-task-4.0.xsd
- http://code.alibabatech.com/schema/dubbo
- http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
- <!-- 加载属性文件 -->
- <context:property-placeholder location="classpath:resource.properties" />
- <!-- 配置扫描 器 -->
- <context:component-scan base-package="com.company.ssm.crm.controller" />
- <!-- 配置处理器映射器 适配器 -->
- <mvc:annotation-driven />
- <!-- 配置视图解释器 jsp -->
- <bean id="jspViewResolver"
- class="org.springframework.web.servlet.view.InternalResourceViewResolver">
- <property name="prefix" value="/WEB-INF/jsp/" />
- <property name="suffix" value=".jsp" />
- </bean>
- </beans>
db.properties
[html] view plain copy
- jdbc.driver=com.mysql.jdbc.Driver
- jdbc.url=jdbc:mysql://localhost:3306/crm?characterEncoding=utf-8
- jdbc.username=root
- jdbc.password=root
以上就是主要的配置文件的配置,特别注意的是在web.xml中默认加载的资源文件再WEB_INF目录下,如果的你xml不在的话就要写清楚的文件路径,例如写在src目录下面就要写classpath,之前开发过程中忽略了这一点,所以启动一直报错找不到资源文件。
并结合自己的SSM搭建,小结SSM环境搭建,以便记忆,交流和学习,有什么不妥的地方,欢迎指正、交流
原文地址:https://www.cnblogs.com/iOS-mt/p/8462349.html