SpringMVC原理解析
2020-04-23 09:26
976 查看
1.原理图
2.代码实例解析(建议自己做一遍)
2.1 web.xml
确定导入了SpringMVC的包之后,我们先来配置web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0"> <!--1.注册DispatcherServlet--> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!--关联一个springmvc的配置文件:spring-mvc.xml--> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-mvc.xml</param-value> </init-param> <!--启动级别-1--> <load-on-startup>1</load-on-startup> </servlet> <!--/ 匹配所有的请求;(不包括.jsp)--> <!--/* 匹配所有的请求;(包括.jsp)--> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
2.2 spring-mvc.xml
配置spring-mvc.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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> //配置映射器,它的作用是帮助Dispatcher找到对应的bean,并且再把信息传递给Dispatcher,对应图上3,4,5 <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/> //配置适配器,它的作用是找到对应的实现了Controller接口的类,对应图上6,7 <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/> //添加视图解析器 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> //前缀,对应要访问的文件的文件目录前缀 <property name="prefix" value="/WEB-INF/jsp/"/> //后缀,对应访问文件的格式 <property name="suffix" value=".jsp"/> //这样有了前缀和后缀以后,直接可以输入文件的名字,如我们本例中“hello”就可以直接访问了 //这里不理解没关系,在下边对应的Controller层还会继续解释这一点 </bean> //这里的id=/hello,表示我们输入的URL的后缀 <bean id="/hello" class="com.wang.controller.HelloServlet"/> </beans>
2.3Controller
编写对应的Controller层,也就是要实现Controller接口的类
import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.Controller; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; //实现Controller接口 public class HelloServlet implements Controller { //其中必须要重写的方法 public ModelAndView handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception { //创建MondelAndView对象 ModelAndView modelAndView = new ModelAndView(); //向其中添加对象,包含如下信息 modelAndView.addObject("msg","Hello,SpringMVC!You can do it!!!"); //这里的hello与spring-mvc.xml配置中,视图解析器相对应 //也就是访问的是/WEB-INF/jsp/hello.jsp文件 modelAndView.setViewName("hello"); return modelAndView; } }
2.4编写跳转的视图文件
在web/WEB-INF/jsp目录下创建hello.jsp文件
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> ${msg} </body> </html>
3.配置Tomcat启动
访问http://localhost:8080/hello页面,我们将看到如下显示
Hello,SpringMVC!You can do it!!!
- 点赞
- 收藏
- 分享
- 文章举报
相关文章推荐
- [SpringMVC]SpringMVC学习笔记一: springmvc原理及实例解析.
- 【SpringMVC架构】SpringMVC入门实例,解析工作原理(二)
- springMVC源码分析--@SessionAttribute用法及原理解析SessionAttributesHandler和SessionAttributeStore
- SpringMVC原理解析
- springmvc 原理解析
- springMVC源码分析--@SessionAttribute用法及原理解析SessionAttributesHandler和SessionAttributeStore
- 【SpringMVC架构】SpringMVC入门实例,解析工作原理(二)
- springMVC源码分析--@SessionAttribute用法及原理解析SessionAttributesHandler和SessionAttributeStore
- (1) SpringMVC原理详细解析()
- SpringMVC注解@RequestParam方法原理解析
- 【SpringMVC架构】SpringMVC入门实例,解析工作原理(二)
- springmvc框架原理解析
- 【SpringMVC架构】SpringMVC入门实例,解析工作原理(二)
- SpringMVC原理解析
- [SpringMVC]SpringMVC学习笔记一: springmvc原理及实例解析.
- SpringMVC源码之参数解析绑定原理
- SpringMVC源码深度解析之SpringServletContainerInitializer原理分析
- SpringMVC源码之参数解析绑定原理
- springmvc原理解析
- 【SpringMVC架构】SpringMVC入门实例,解析工作原理(二)