zoukankan      html  css  js  c++  java
  • (转)淘淘商城系列——SSM框架整合之表现层整合

    http://blog.csdn.net/yerenyuan_pku/article/details/72721120

    上文我们一起学习了Service层的整合,本文将教大家如何整合表现层。 
    我们在taotao-manager-web工程的src/main/resource目录下新建一个spring文件夹,在该目录下新建一个springmvc.xml文件,如下图所示。 
     
    springmvc.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:p="http://www.springframework.org/schema/p"
        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-4.2.xsd
            http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd">
    
        <context:component-scan base-package="com.taotao.controller" />
        <mvc:annotation-driven />
        <bean>
            class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/WEB-INF/jsp/" />
            <property name="suffix" value=".jsp" />
        </bean>
    
    </beans>
    • 1
    • 2

    从上可以看到我们springmvc.xml文件中配置的扫描包是com.taotao.controller,因此我们需要创建这么一个目录,并且从视图解析器的配置中可以看出,我们还要在WEB-INF目录下新建一个jsp目录,如下图所示。 
     
    下面我们需要在taotao-manager-web工程下的web.xml文件中配置一下编码和前端控制器,web.xml文件的配置内容如下:

    <?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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
        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>taotao-manager-web</display-name>
        <welcome-file-list>
            <welcome-file>index.jsp</welcome-file>
        </welcome-file-list>
    
        <!-- 解决post乱码 -->
        <filter>
            <filter-name>CharacterEncodingFilter</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>CharacterEncodingFilter</filter-name>
            <url-pattern>/*</url-pattern>
        </filter-mapping>
    
        <!-- springmvc的前端控制器 -->
        <servlet>
            <servlet-name>taotao-manager</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <!-- contextConfigLocation不是必须的, 如果不配置contextConfigLocation, springmvc的配置文件默认在:WEB-INF/servlet的name+"-servlet.xml" -->
            <init-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>classpath:spring/springmvc.xml</param-value>
            </init-param>
            <load-on-startup>1</load-on-startup>
        </servlet>
        <servlet-mapping>
            <servlet-name>taotao-manager</servlet-name>
            <url-pattern>/</url-pattern>
        </servlet-mapping>
    </web-app>
    • 1

    其中前端控制器配置中<load-on-startup>1</load-on-startup>这句话的意思是tomcat启动之后便加载DispatcherServlet,如果不配置的话,需要等请求访问的时候才会加载DispatcherServlet。另外可以看到,我们并没有配置父容器(ContextLoaderListener),这是因为我们在taotao-manager工程中已经配置过了,而且配置了Dao和Service。因此我们表现层不需要再配置一遍父容器了。 
    说到这里,我们一般会对父子容器(同一个工程下)比较感兴趣,想知道是怎么回事,请看下面这张图。Spring父容器一般配置的是Dao层和Service层,而Spring子容器一般配置的是Controller层,父子容器的访问关系是,子容器可以访问父容器中的对象,但是父容器无法访问子容器中的对象,比如Controller可以把Dao和Service注入进来,但是Dao和Service无法把Controller注进来。 

    我们在service配置扫描包的时候配置的扫描范围是”com.taotao.service”,如果我们配置成com.taotao,那么就会把com.taotao.controller也扫描到父容器中,这样父子容器中就都有Controller层了,但是在父容器中扫进来Controller是没有用的,我们在表现层访问的时候,访问的还是子容器的Controller。同理,如果把子容器的扫描范围扩大,变为com.taotao,那么它便会把Dao和Service也扫描到子容器当中,这样当我们访问表现层的时候,访问的便是子容器中的Dao和Service,子容器中的Dao和Service是没有事务的,但是父容器中的Dao和Service是有事务的,这样就会导致虽然我们在父容器中配置了事务,但由于子容器扫描范围太大,而导致访问子容器中的Dao和Service没有事务的问题。 

    由于子容器可以直接访问父容器中的对象,因此我们在子容器中不用配置Dao和Service便可以直接使用它们。子容器其实也可以完全当父容器使用,之所以搞出父子容器,是为了让父容器有更好的扩展性,子容器只需要消费父容器就可以了。

     
     
  • 相关阅读:
    Asp.Net+Oracle+BootStrap+Jquery
    UML类图几种关系的总结
    PHP对象在内存堆栈中的分配
    php sprintf 详解
    微信错误代码45047:客服消息只能发送20条/个用户
    php利用array_search与array_column实现二维数组查找
    mvc 详解
    php中++i 与 i++ 的区分详解
    Git 别名多个命令 超实用
    php 对象继承
  • 原文地址:https://www.cnblogs.com/telwanggs/p/6934045.html
Copyright © 2011-2022 走看看