springMVC的核心是中央调度器(前端控制器)。
前端控制器配置在web.xml文件里。用来接收用户发起的请求。接收到用户的请求之后,调用映射器,查询这个请求的处理器执行链;之后调用适配器,根据处理器执行链,调用处理器Controller,返回MAV;之后调用视图解析器,生成相应的视图处理类,即view对象,最后进行视图渲染,并返回给用户结果。
开发步骤:
1.新建Dynamic Web Project项目
2.导入所需要的jar包
3.重点,修改web.xml .注册中央调度器 对象 DispatcherServlet (也叫做前端控制器)。
4.定义发起请求的视图 jsp
5.定义处理器对象,处理用户的请求,定义类实现 Controller 接口。
6.定义显示处理器结果的视图jsp,可以使用EL表达式获取内存中的数据。
7.定义springMVC的配置文件,springMVC是spring的一部分,也是容器。注册自定义的处理器对象。
8.在springMVC的配置文件中注册视图解析器对象。
<?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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> <display-name>01my-springmvc</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <!-- springmvc的核心是 Dispatcher 调度器,继承了 HttpServlet --> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- 当程序启动时,创建DispatcherServlet 对象,加载springMVC需要的配置文件 默认的配置文件 是 /WEB-INF/spring-servlet.xml 默认的文件名是 <servlet-name>加上 -servlet.xml 指定springMVC的配置文件 使用父类 FrameworkServlet 的 contextConfigLocation --> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc.xml</param-value> </init-param> <!-- DispatcherServlet 在 tomcat应用启动的时候创建实例 load-on-startup:指定对象在服务器启动时候创建实例的值 1.是大于等于0的数,数值越小,创建对象的时间越早。 2.不给值,或者给个负值,当首次访问这个Servlet的时候,才创建对象的实例。 3.当程序中有多个对象的load-on-startup 值相同时,由服务器决定创建对象的顺序。 https://blog.csdn.net/qq_25646191/article/details/78775752 --> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <!-- url-pattern: 把某些浏览器的请求交给 DispatcherServlet 处理,交给DispatcherServlet 处理的请求是经过springmvc框架处理的请求。 springmvc中有两种方式 1.扩展名,例如 *.do , *.mvc , *.action 等,不能使用 *.jsp 2.使用斜杠 "/" 表示所有请求 --> <url-pattern>*.do</url-pattern> </servlet-mapping> </web-app>
<?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:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd" > <!-- 注册处理器对象 --> <!-- id : 指定请求的uri,资源的名称,以 / 开头,表示这个bean是处理用户请求的,用来和其它bean分开 --> <bean id="/hello.do" class="controller.MyController"/> </beans>
package controller; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.Controller; public class MyController implements Controller { @Override public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception { ModelAndView mv = new ModelAndView(); String name = request.getParameter("name"); //把参数处理之后,在页面显示 //框架处理之后, 效果相当于 request.setAttribute("name", "hello"+name); mv.addObject("name", "hello"+name); //request.getRequestDispatcher("/show.jsp").forward(request, response); mv.setViewName("/show.jsp"); return mv; } }
原文:https://www.cnblogs.com/llq1214/p/11427030.html