MVC设计模式/SpringMVC/SSM框架整合配置文件讲解

1.MVC设计模式理解

关键字:解耦合

对比原始开发:

*原始开发过程:请求—Controller(创建model对象调用模型层进行数据处理)—Model(创建View对象)—View(进行视图渲染)—响应

*mvc设计模式:请求—Controller(调用模型层进行数据处理)—Model(返回处理结果);Controller(调用view视图层进行视图渲染)—View(返回渲染后页面);Controller—响应;
核心是Controller进行中央调度;

2.SpringMVC架构理解

核心:DispatcherServlet / 前端控制器模式
MVC设计模式/SpringMVC/SSM框架整合配置文件讲解_第1张图片
简述:请求 到达 控制层(servlet)后符合条件被拦截,调用处理器映射器进行请求处理分发,并返回请求处理对象chain链(加入拦截器);servlet通过处理器适配器执行controller对象并返回model数据和视图;视图为逻辑视图时,servlet通过调用视图解析器进行视图解析出物理视图;servlet调用view层通过EL表达式等进行数据渲染成结果页面;servlet将页面响应给客户端;

注意逻辑视图与物理视图的区分;

3.SSM框架整合相关配置文件

1.spring容器

applicationContext-dao.xml

 
    <context:property-placeholder location="classpath:jdbc.properties"/>
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="url" value="${jdbc.url}">property>
        <property name="driverClassName" value="${jdbc.driver}">property>
        <property name="username" value="${jdbc.username}">property>
        <property name="password" value="${jdbc.password}">property>
    bean>

    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource">property>
        <property name="typeAliasesPackage" value="cn.fox.domain">property>
        <property name="configLocation" value="classpath:sqlMapConfig.xml">property>      
    bean>

    
     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="cn.fox.mapper">property>
     bean>

注意:Mapper接口代理开发的五项原则;

applicationContext-service.xml


    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource">property>
    bean>


    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="save*" propagation="REQUIRED"/>
            <tx:method name="update*" propagation="REQUIRED"/>
            <tx:method name="delete*" propagation="REQUIRED"/>
            <tx:method name="*" propagation="REQUIRED"/>
        tx:attributes>
    tx:advice>

    <aop:config>
        <aop:advisor advice-ref="txAdvice" pointcut="execution(* cn.fox.service.*.*(..))"/>
    aop:config>

注意:切点表达式的写法

可以在springmvc.xml中一起扫描所有的注解组件,但最好分开


    <context:component-scan base-package="com.xx.springmvc.service.impl"/>


    <context:component-scan base-package="com.xx.springmvc.web.controller"/>

2.springMVC.XML


    <context:component-scan base-package="cn.fox" />    

    <mvc:annotation-driven/>

    <mvc:default-servlet-handler />

    <bean class="cn.fox.exception.ExceptionResolver">bean>

    <bean id="multipartResolver"
        class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="maxUploadSize" value="10240000">property>
    bean>

    <mvc:interceptors>      
        <mvc:interceptor>
            <mvc:mapping path="/**" />
            <bean class="cn.fox.interceptor.LoginInterceptor">bean>
        mvc:interceptor>
    mvc:interceptors>


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

注意:2 注解驱动的作用较多

3.sqlMapConfig.xml

虽无相关内容,但需配置

4.web.xml


    <filter>
    <filter-name>characterEncodingfilter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
        <init-param>
            <param-name>encodingparam-name>
            <param-value>UTF-8param-value>
        init-param>
  filter>

  <filter-mapping>
    <filter-name>characterEncodingfilter-name>
    
    <url-pattern>/*url-pattern>
  filter-mapping>


  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
  listener>
    <context-param>
        <param-name>contextConfigLocationparam-name>
        <param-value>classpath:applicationContext-*.xmlparam-value>
    context-param>


        <servlet>
        <servlet-name>springmvcservlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
        <init-param>
            <param-name>contextConfigLocationparam-name>
            <param-value>classpath:springmvc.xmlparam-value>
        init-param>
    servlet>
    <servlet-mapping>
        <servlet-name>springmvcservlet-name>
        <url-pattern>/url-pattern>
        
    servlet-mapping>

你可能感兴趣的:(MVC设计模式/SpringMVC/SSM框架整合配置文件讲解)