您的位置:首页 > 其它

商城项目实战08:SSM框架整合之表现层整合

2017-06-01 19:59 591 查看
上文我们一起学习了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
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

从上可以看到我们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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40

其中前端控制器配置中
<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便可以直接使用它们。子容器其实也可以完全当父容器使用,之所以搞出父子容器,是为了让父容器有更好的扩展性,子容器只需要消费父容器就可以了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: