您的位置:首页 > 编程语言 > Java开发

学习淘淘商城第十课(SSM框架整合之springmvc整合及父子容器的关系)

2018-02-04 00:49 573 查看
上节课我们一起学习了整合service层,这节我们一起学习下怎样整合web层即springmvc。

我们在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:dubbo="http://code.alibabatech.com/schema/dubbo"
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://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd"> <!-- 配置注解驱动 -->
<mvc:annotation-driven/>
<!-- 视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!-- 配置包扫描器,扫描@Controller注解的类 -->
<context:component-scan base-package="com.taotao.controller"/>
</beans>


可以看到我们springmvc.xml文件中配置扫描包是com.taotao.controller,因此我们需要创建这么一个目录,如下图所示。



下面我们需要配置编码和前端控制器,我们需要在taotao-manager-web工程下的web.xml文件中配置。



web.xml文件配置内容如下,其中前端控制器配置中1这句话的意思是tomcat启动之后便加载DispatcherServlet,如果不配置的话,需要等请求访问的时候才会加载DispatcherServlet。另外可以看到,我们并没有配置父容器(ContextLoaderListener),这是因为我们在taotao-manager工程中已经配置过了,而且配置了Dao和Service。因此我们表现层不需要再配置一遍父容器了。

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" id="WebApp_ID" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <display-name>taotao-manager</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>
<!-- 前端控制器 -->
<servlet>
<servlet-name>taotao-manager-web</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>
</servlet>
<servlet-mapping>
<servlet-name>taotao-manager-web</servlet-name>
ur
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>


说到这里,我们一般会对父子容器(同一个工程下)比较感兴趣,想知道是怎么回事,请看下面这张图。Spring父容器一般配置的是Service层和Dao层,而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便可以直接使用它们。

子容器其实也可以完全当父容器使用,之所以搞出父子容器,是为了让父容器有更好的扩展性,子容器只需要消费父容器就可以了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: