SpringMVC工作流程
1、用户向服务器发送请求,被SpringMvc前端控制器DispatcherServlet捕获。
<servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:dispatcher-servlet.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>
2、DispatcherServlet对请求URL进行解析,得到URI,根据URI调用HandlerMapping获得Handler配置的所有相关对象
3、DispatcherServlet根据获得的Handler,选择一个合适的HandlerAdapter。提取request中的模型数据,填充handler入参,开始执行Handler(Controller)
4、Handler执行完成后,向DispatcherServlet返回一个ModelAndView对象
@RequestMapping(value = "/dashboard") public class DashboardCtrl extends AbsBaseCtrlAware { private static final Logger logger = Logger.getLogger("LoginCtrl"); @Resource(name = "userService") private UserService userService; @RequestMapping public ModelAndView into(HttpServletRequest request) { RealTimeUserInfo realTimeUserInfo = userService.getRealTimeUserInfoByUser(getCurrentUser()); ModelAndView ret = new ModelAndView("main/main"); Map<String, Date> priod = userService.getPriod(getCurrentUser()); if (priod != null && priod.containsKey("billStart") && priod.containsKey("billEnd")) { ret.addObject("spriod", StringUtil.formatDate(priod.get("billStart"), "yyyy-MM-dd")); ret.addObject("epriod", StringUtil.formatDate(priod.get("billEnd"), "yyyy-MM-dd")); } ret.addObject("userInfo", realTimeUserInfo); // 功能权限刷新 request.getSession().setAttribute("openFunction", OpenFunctionUtil.getSinglton()); request.getSession().setAttribute("nav", "dashboard"); return ret; } }
5、DispatcherServlet根据返回的ModelAndView,选择一个合适的ViewResolver,渲染视图,返回客户端
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/page/"/>
<property name="suffix" value=".jsp"/>
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
</bean>
6、拦截器会对全部/部分url进行拦截
<!-- 拦截器 --> <mvc:interceptors> <bean class="com.selfpackage.interceptor.AuthorizeInterceptor"/> <bean class="com.selfpackage.interceptor.Idstar30Interceptor"/> <bean class="com.selfpackage.interceptor.CasSsoInterceptor"/> <bean class="com.selfpackage.interceptor.LoginInterceptor"/> <bean class="com.selfpackage.interceptor.CsrfInterceptor"/> <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"/> </mvc:interceptors>