【SpringMVC入门篇】SpringMVC 入门使用 (一)
概述
Spring MVC 也叫 Spring Web MVC ,属于展示层框架。SpringMVC 是 Spring 框架的一部分。
Spring Web MVC 框架提供了 MVC (模型 - 视图 - 控制器) 架构和用于开发灵活和松散耦合的 Web 应用程序的组件。 MVC 模式导致应用程序的不同方面(输入逻辑,业务逻辑和 UI 逻辑)分离,同时提供这些元素之间的松散耦合。
- 模型 (Model):封装了应用程序数据,通常它们将由 POJO 类组成。
- 视图 (View):负责渲染模型数据,一般来说它生成客户端浏览器可以解释 HTML 输出。
- 控制器 (Controller):负责处理用户请求并构建适当的模型,并将其传递给视图进行渲染。
DispatcherServlet(分发) 组件类
Spring Web MVC 框架是围绕 DispatcherServlet 设计的,它处理所有的 HTTP 请求和响应。 Spring Web MVC DispatcherServlet 的请求处理工作流如下图所示:
以下是对应于到 DispatcherServlet 的传入 HTTP 请求的事件顺序:
- 在接收到 HTTP 请求后,DispatcherServlet 会查询 HandlerMapping 以调用相应的 Controller。
- Controller 接受请求并根据使用的 GET 或 POST 方法调用相应的服务方法。 服务方法将基于定义的业务逻辑设置模型数据,并将视图名称返回给 DispatcherServlet。
- DispatcherServlet 将从 ViewResolver 获取请求的定义视图。
所有上述组件,即: HandlerMapping,Controller 和 ViewResolver 是 WebApplicationContext 的一部分,它是普通 ApplicationContext 的扩展,带有 Web 应用程序所需的一些额外功能。
域对象传值
Request域对象
给Request域传值有多种方式,具体如下:
// 1、Servlet原生Api方式
@RequestMapping("/testRequestByServletApi")
public String testRequestByServletApi(HttpServletRequest request) {
request.setAttribute("testRequest", "hello, Servlet api");
return "success";
}
// 2、ModelAndView
@RequestMapping("/testModelAndView")
public ModelAndView testModelAndView() {
ModelAndView view = new ModelAndView();
view.addObject("testRequest", "hello, ModelAndView");
view.setViewName("success");
return view;
}
// 3、Model
@RequestMapping("/testModel")
public String testModel(Model model) {
model.addAttribute("testRequest", "hello, Model");
return "success";
}
// 4、Map
@RequestMapping("/testMap")
public String testModel(Map<String, Object> map) {
map.put("testRequest", "hello, Model");
return "success";
}
// 5、ModelMap
@RequestMapping("/testModelMap")
public String testModel(ModelMap modelMap) {
modelMap.addAttribute("testRequest", "hello, ModelMap");
return "success";
}
Session域对象
@RequestMapping("/testSession")
public String testModel(HttpSession session) {
session.setAttribute("testRequest", "hello, session");
return "success";
}
视图解析器
在Servlet中有两个概念需要区分一下:转发和重定向。
- 转发 :是一次http request请求,发生在服务端,可以访问WEB-INF中资源
- 重定向 :是两次请求,第一次服务端返回302,浏览器自动触发第二次,不可以访问WEB-INF中的资源
原生的Servlet接口
通过原生的Servelt接口HttpServletRequest进行转发,HttpServletResponse进行重定向
@RequestMapping("/hello")
public void hello(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//通过request进行转发
//request.getRequestDispatcher("/WEB-INF/jsp/test.jsp").forward(request, response);
//通过response进行重定向
response.setStatus(HttpServletResponse.SC_FOUND);
response.sendRedirect(request.getContextPath()+"/myhello.jsp");
}
通过视图解析器
当我们引入Thymeleaf,默认使用的就是Thymeleaf视图解析器,但是如果有前缀forward
和redirect
则不是使用Thymeleaf视图解析器,具体如下:
InternalResourceView
(用于转发):用于转发RedirectView
(用于重定向):用于重定向
@Controller
public class ViewController {
@RequestMapping("/testThymeleafView")
public String testThymeleafView() {
return "success"; //使用Thymeleaf视图解析器
}
@RequestMapping("/testForward")
public String testForward() {
return "forward:/testThymeleafView"; //使用的InternalResourceView
}
@RequestMapping("/testRedirect")
public String testRedirect() {
return "redirect:/testThymeleafView"; //使用的RedirectView解析器
}