标签:row obj style info nal 前缀 article dispatch strong
1.Spring MVC 的工作流程
1.web请求被 前端控制器(DispatcherServlet)拦截
2.DispatcherServlet调用 映射处理器(HandelerMapping)查找页面 处理器(Handler),HandlerMapping向DispatcherServlet返回Handler。HandlerMapping把web请求映射为HandlerExecutionChain对象,它包含了一个Handler处理器对象和多个 拦截器(HandlerInterceptor)对象
3.DispatcherServlert调用 处理适配器(HandlerAdapter)去执行Handler
4.HandlerAdapter会根据适配器的结果去执行Handler,Handler执行完成后适配器返回ModelAndView,HandlerAdapter向DispatcherServlert返回ModelAndView.
5.DispatcherServlet调用 视图解析器(ViewResolver)进行视图解析,它根据逻辑视图名解析成JSP,ViewResolver向DispatcherServlet返回View.
6.DiapatcherServlet进行视图渲染
7.DispatcherServlet向用户返回响应结果
2.第一个SpringMVC程序
1.准备jar包(有些包为后续作准备)
2.项目结构(不要在意项目名和包名,咳咳,注意创建的是web项目)
3.web.xml文件
1 <?xml version="1.0" encoding="UTF-8"?> 2 <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"> 3 <display-name>SpringMVC</display-name> 4 <!-- 配置前端控制器 --> 5 <servlet> 6 <servlet-name>article6</servlet-name> 7 <!-- 配置SpringMVC前端控制器 --> 8 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 9 <!-- 加载Spring的配置文件 --> 10 <init-param> 11 <param-name>contextConfigLocation</param-name> 12 <param-value>classpath:springmvc-servlet.xml</param-value> 13 </init-param> 14 15 </servlet> 16 <!-- 由DispatcherServlet拦截前缀为url的路径 --> 17 <servlet-mapping> 18 <servlet-name>article6</servlet-name> 19 <url-pattern>/url/*</url-pattern> 20 </servlet-mapping> 21 22 </web-app>
4.springmvc-servlet.xml(地址前缀记得加上url/才能拦截处理)
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:mvc="http://www.springframework.org/schema/mvc" 5 xmlns:p="http://www.springframework.org/schema/p" 6 xmlns:context="http://www.springframework.org/schema/context" 7 xsi:schemaLocation=" 8 http://www.springframework.org/schema/beans 9 http://www.springframework.org/schema/beans/spring-beans.xsd 10 http://www.springframework.org/schema/context 11 http://www.springframework.org/schema/context/spring-context.xsd 12 http://www.springframework.org/schema/mvc 13 http://www.springframework.org/schema/mvc/spring-mvc.xsd"> 14 15 <!-- 1默认方式 BeanNameUrlHandlerMapping 访问路径为http://localhost:8080/mmm/student.html--> 16 <!-- <bean id="handlerMapping" class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"></bean> --> 17 <!-- <bean id="studentController" name="/student.html" class="com.article6.example1.StudentController"></bean> --> 18 19 <!-- 2使用类名 studentController作为访问路径 第一个字母小写 访问路径为http://localhost:8080/mmm/studentController--> 20 <!-- <bean id="handlerMapping" class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"></bean> 21 <bean id="studentController" class="com.article6.example1.StudentController"></bean> --> 22 23 <!-- 3 SimpleUrlHandlerMapping 分离URL和bean --> 24 <bean id="handlerMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> 25 <property name="mappings"> 26 <props> 27 <!-- 定义路径 测试3用 访问路径为http://localhost:8080/mmm/student.html--> 28 <!-- <prop key="/student.html">studentController</prop> --> 29 30 <!-- 多功能控制器 http://localhost:8080/mmm/url/student/addStudent--> 31 <prop key="/student/*">studentController1</prop> 32 </props> 33 </property> 34 </bean> 35 <!-- 测试多功能控制器--> 36 <bean id="studentController1" class="com.article6.example1.StudentController1"></bean> 37 38 <!-- 测试3用 --> 39 <!-- <bean id="studentController" class="com.article6.example1.StudentController"></bean> --> 40 41 <!-- 完成视图的对应 --> 42 <!-- 对转向页面的路径解析。prefix:前缀, suffix:后缀 --> 43 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" > 44 <property name="prefix" value="/WEB-INF/jsp/"/> 45 <property name="suffix" value=".jsp"/> 46 </bean> 47 48 </beans>
5.StudentController.java(测试三种映射处理器(对应配置文件中三种方式) HandlerMapping用于管理URL和controller之间的映射关系)
1 package com.article6.example1; 2 3 import javax.servlet.http.HttpServletRequest; 4 import javax.servlet.http.HttpServletResponse; 5 6 import org.springframework.web.servlet.ModelAndView; 7 import org.springframework.web.servlet.mvc.Controller; 8 9 public class StudentController implements Controller{ 10 11 //默认 把bean的name的属性作URL使用 12 // @Override 13 // public ModelAndView handleRequest(HttpServletRequest arg0, HttpServletResponse arg1) throws Exception { 14 // ModelAndView view=new ModelAndView(); 15 // view.addObject("msg","第一个SpringMVC程序");//传递的值 16 // view.setViewName("/example1/StudentResult");//视图名,最后.xml文件中视图解析器会拼接此地址 17 // return view; 18 // } 19 20 //类名映射控制器 21 // @Override 22 // public ModelAndView handleRequest(HttpServletRequest arg0, HttpServletResponse arg1) throws Exception { 23 // ModelAndView view=new ModelAndView(); 24 // view.addObject("msg","类名映射控制器"); 25 // view.setViewName("/example1/StudentResult"); 26 // return view; 27 // } 28 29 //SimpleUrlHandlerMapping简单的URL映射控制器,可以把URL和bean的映射关系分开 30 @Override 31 public ModelAndView handleRequest(HttpServletRequest arg0, HttpServletResponse arg1) throws Exception { 32 ModelAndView view=new ModelAndView(); 33 view.addObject("msg","SimpleUrlHandlerMapping映射控制器"); 34 view.setViewName("/example1/StudentResult"); 35 return view; 36 } 37 }
6.StudentController1.java(多功能控制器 测试类 通过方法名访问 解决针对每一个URL请求编写对应的控制器显得太繁琐 )
1 package com.article6.example1; 2 3 import javax.servlet.http.HttpServletRequest; 4 import javax.servlet.http.HttpServletResponse; 5 6 import org.springframework.web.servlet.ModelAndView; 7 import org.springframework.web.servlet.mvc.multiaction.MultiActionController; 8 9 /** 多功能控制器 10 * @author wt 11 * @data:2018年4月12日 下午7:34:53 12 */ 13 public class StudentController1 extends MultiActionController{ 14 15 public ModelAndView addStudent(HttpServletRequest arg0, HttpServletResponse arg1){ 16 ModelAndView mav=new ModelAndView(); 17 mav.addObject("msg","addStudent()"); 18 mav.setViewName("/example1/StudentResult"); 19 return mav; 20 } 21 public ModelAndView updateStudent(HttpServletRequest arg0, HttpServletResponse arg1){ 22 ModelAndView mav=new ModelAndView(); 23 mav.addObject("msg","updateStudent()"); 24 mav.setViewName("/example1/StudentResult"); 25 return mav; 26 } 27 public ModelAndView delStudent(HttpServletRequest arg0, HttpServletResponse arg1){ 28 ModelAndView mav=new ModelAndView(); 29 mav.addObject("msg","delStudent()"); 30 mav.setViewName("/example1/StudentResult"); 31 return mav; 32 } 33 public ModelAndView queryStudent(HttpServletRequest arg0, HttpServletResponse arg1){ 34 ModelAndView mav=new ModelAndView(); 35 mav.addObject("msg","queryStudent()"); 36 mav.setViewName("/example1/StudentResult"); 37 return mav; 38 } 39 }
多功能控制器的结果
7.StudentResult.jsp(最终访问的页面,获取msg 因为是通过转发,所以地址栏地址显示第一次访问地址)
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 7 <title>Insert title here</title> 8 </head> 9 <body> 10 <%=request.getAttribute("msg")%> 11 </body> 12 </html>
这里显得很繁琐,最终我们可以使用注解驱动SpringMVC就简单多了。。。下期再做介绍。
标签:row obj style info nal 前缀 article dispatch strong
原文地址:https://www.cnblogs.com/wtzl/p/8810862.html