13.5 实现Web层
前面部分已经实现了本应用的所有中间层,系统的所有业务逻辑组件也都部署在Spring容器中了。接下来应该为应用实现Web层了。通常而言,系统的控制器和JSP在起设计。因为当JSP页面发出请求后,该请求被控制器接收,然后控制器负责调用业务逻辑组件来处理请求。从这个意义上来说,控制器是JSP页面和业务逻辑组件之间的纽带。
13.5.1 控制器的处理顺序
对于使用Spring MVC的应用而言,控制器实际上由两个部分组成:系统的核心控制器DispatcherServlet和业务控制器Controller:
web.xml
| 12
 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
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 
 | <?xml version="1.0" encoding="UTF-8"?><web-app
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns="http://xmlns.jcp.org/xml/ns/javaee"
 xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
 id="WebApp_ID"
 version="3.1">
 
 <listener>
 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 </listener>
 
 <context-param>
 <param-name>contextConfigLocation</param-name>
 <param-value>/WEB-INF/applicationContext*.xml</param-value>
 </context-param>
 
 <servlet>
 <servlet-name>springmvc</servlet-name>
 <servlet-class>org.springframework.web.servlet.DispatcherServlet
 </servlet-class>
 <init-param>
 <param-name>contextConfigLocation</param-name>
 <param-value>/WEB-INF/springmvc-config.xml</param-value>
 </init-param>
 <load-on-startup>1</load-on-startup>
 </servlet>
 
 <servlet-mapping>
 <servlet-name>springmvc</servlet-name>
 <url-pattern>/</url-pattern>
 </servlet-mapping>
 
 <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>
 
 <jsp-config>
 <jsp-property-group>
 
 <url-pattern>*.jsp</url-pattern>
 
 <el-ignored>false</el-ignored>
 
 <scripting-invalid>true</scripting-invalid>
 
 <include-prelude>/WEB-INF/jsp/taglib.jsp</include-prelude>
 </jsp-property-group>
 </jsp-config>
 <error-page>
 <error-code>404</error-code>
 <location>/404.html</location>
 </error-page>
 <welcome-file-list>
 <welcome-file>index.jsp</welcome-file>
 </welcome-file-list>
 </web-app>
 
 | 
springmvc-config.xml
| 12
 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
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 
 | <?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:p="http://www.springframework.org/schema/p"
 xmlns:context="http://www.springframework.org/schema/context"
 xsi:schemaLocation="
 http://www.springframework.org/schema/beans
 http://www.springframework.org/schema/beans/spring-beans.xsd
 http://www.springframework.org/schema/mvc
 http://www.springframework.org/schema/mvc/spring-mvc.xsd
 http://www.springframework.org/schema/context
 http://www.springframework.org/schema/context/spring-context.xsd">
 
 
 <context:component-scan
 base-package="org.fkit.hrm.controller" />
 
 
 <mvc:annotation-driven />
 
 
 <mvc:default-servlet-handler />
 
 
 <mvc:interceptors>
 <mvc:interceptor>
 
 <mvc:mapping path="/*" />
 
 <bean class="org.fkit.hrm.interceptor.AuthorizedInterceptor" />
 </mvc:interceptor>
 </mvc:interceptors>
 
 
 <bean id="viewResolver"
 class="org.springframework.web.servlet.view.InternalResourceViewResolver"
 p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" />
 
 
 <bean id="multipartResolver"
 class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
 
 <property name="maxUploadSize">
 <value>10485760</value>
 </property>
 
 <property name="defaultEncoding">
 <value>UTF-8</value>
 </property>
 </bean>
 </beans>
 
 | 
在springmvc-config.xml文件中配置了一个拦截器,用于判断用户是否登录,如果其没有登录,则用户不能访问网站,跳回登录页面重新登录。
AuthorizedInterceptor.java
| 12
 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
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 
 | package org.fkit.hrm.interceptor;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 import org.fkit.hrm.domain.User;
 import org.fkit.hrm.util.common.HrmConstants;
 import org.springframework.web.servlet.HandlerInterceptor;
 import org.springframework.web.servlet.ModelAndView;
 
 
 
 
 public class AuthorizedInterceptor implements HandlerInterceptor
 {
 
 private static final String[] IGNORE_URI ={"/loginForm", "/login", "/404.html"};
 
 
 
 
 @Override
 public void afterCompletion(HttpServletRequest request,
 HttpServletResponse response, Object handler, Exception exception)
 throws Exception
 {
 
 }
 
 
 
 
 
 @Override
 public void postHandle(HttpServletRequest request,
 HttpServletResponse response, Object handler, ModelAndView mv)
 throws Exception
 {
 
 }
 
 
 
 
 
 
 @Override
 public boolean preHandle(HttpServletRequest request,
 HttpServletResponse response, Object handler) throws Exception
 {
 
 boolean flag = false;
 
 String servletPath = request.getServletPath();
 
 for (String s : IGNORE_URI)
 {
 if (servletPath.contains(s))
 {
 flag = true;
 break;
 }
 }
 
 if (!flag)
 {
 
 User user = (User) request.getSession()
 .getAttribute(HrmConstants.USER_SESSION);
 
 if (user == null)
 {
 
 request.setAttribute("message", "请先登录再访问网站!");
 request.getRequestDispatcher(HrmConstants.LOGIN)
 .forward(request, response);
 return flag;
 } else
 {
 flag = true;
 }
 }
 return flag;
 }
 }
 
 | 
下面通过用例来介绍控制层的实现。