SpringMVC中实现Handler处理器有哪三种具体编写方式?
- 内容介绍
- 相关推荐
本文共计501个文字,预计阅读时间需要3分钟。
SpringMVC中处理器配置涉及映射器、适配器、视图解析器。手动编写处理器时,需遵循以下三种写法之一:
1. 继承HandlerAdapter
2.实现HandlerAdapter接口
3.使用适配器(Adapter)模式
无论哪种方式,处理器执行流程都是通过处理器映射器(DispatcherServlet)将请求映射到对应的处理器,然后处理器通过映射器调用适配器执行处理,最后通过视图解析器展示结果。
一、SpringMVC中的处理器
配置完SpringMVC的处理器映射器,处理适配器,视图解析器后,需要手动写处理器。关于处理器的写法有三种,无论怎么写,执行流程都是①处理映射器通过@Controller注解找到处理器,继而②通过@RequestMapping注解找到用户输入的url。下面分别介绍这三种方式。
package com.gql.springmvc; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; /** * 类说明: * 处理器的三种写法 * @guoqianliang1998. */ @Controller public class UserController { //1.SpringMVC开发方式 @RequestMapping("/hello") public ModelAndView hello(){ ModelAndView mv = new ModelAndView(); mv.addObject("msg","hello world!"); mv.setViewName("index.jsp"); return mv; } //2.原生Servlet开发方式 @RequestMapping("xx") public void xx(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException{ request.setAttribute("msg", "周冬雨"); request.getRequestDispatcher("/index.jsp").forward(request, response); } //3.开发中常用 @RequestMapping("yy") public String yy(Model model){ model.addAttribute("msg", "双笙"); return "forward:/index.jsp";//forward写不写都是转发,redirect代表重定向. } }
1.SpringMVC开发方式
@RequestMapping("/hello") public ModelAndView hello(){ ModelAndView mv = new ModelAndView(); mv.addObject("msg","hello world!"); mv.setViewName("index.jsp"); return mv; }
2.Servlet原生开发方式
@RequestMapping("xx") public void xx(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException{ request.setAttribute("msg", "周冬雨"); request.getRequestDispatcher("/index.jsp").forward(request, response); }
3.开发中常用的方式
在return的字符串中,forward写不写都是代表转发,redirect则代表重定向。
@RequestMapping("yy") public String yy(Model model){ model.addAttribute("msg", "双笙"); return "forward:/index.jsp"; }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易盾网络。
本文共计501个文字,预计阅读时间需要3分钟。
SpringMVC中处理器配置涉及映射器、适配器、视图解析器。手动编写处理器时,需遵循以下三种写法之一:
1. 继承HandlerAdapter
2.实现HandlerAdapter接口
3.使用适配器(Adapter)模式
无论哪种方式,处理器执行流程都是通过处理器映射器(DispatcherServlet)将请求映射到对应的处理器,然后处理器通过映射器调用适配器执行处理,最后通过视图解析器展示结果。
一、SpringMVC中的处理器
配置完SpringMVC的处理器映射器,处理适配器,视图解析器后,需要手动写处理器。关于处理器的写法有三种,无论怎么写,执行流程都是①处理映射器通过@Controller注解找到处理器,继而②通过@RequestMapping注解找到用户输入的url。下面分别介绍这三种方式。
package com.gql.springmvc; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; /** * 类说明: * 处理器的三种写法 * @guoqianliang1998. */ @Controller public class UserController { //1.SpringMVC开发方式 @RequestMapping("/hello") public ModelAndView hello(){ ModelAndView mv = new ModelAndView(); mv.addObject("msg","hello world!"); mv.setViewName("index.jsp"); return mv; } //2.原生Servlet开发方式 @RequestMapping("xx") public void xx(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException{ request.setAttribute("msg", "周冬雨"); request.getRequestDispatcher("/index.jsp").forward(request, response); } //3.开发中常用 @RequestMapping("yy") public String yy(Model model){ model.addAttribute("msg", "双笙"); return "forward:/index.jsp";//forward写不写都是转发,redirect代表重定向. } }
1.SpringMVC开发方式
@RequestMapping("/hello") public ModelAndView hello(){ ModelAndView mv = new ModelAndView(); mv.addObject("msg","hello world!"); mv.setViewName("index.jsp"); return mv; }
2.Servlet原生开发方式
@RequestMapping("xx") public void xx(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException{ request.setAttribute("msg", "周冬雨"); request.getRequestDispatcher("/index.jsp").forward(request, response); }
3.开发中常用的方式
在return的字符串中,forward写不写都是代表转发,redirect则代表重定向。
@RequestMapping("yy") public String yy(Model model){ model.addAttribute("msg", "双笙"); return "forward:/index.jsp"; }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易盾网络。

