SpringMVC框架實現(xiàn)Handler處理器的三種寫法
一、SpringMVC中的處理器
配置完SpringMVC的處理器映射器,處理適配器,視圖解析器后,需要手動寫處理器。關于處理器的寫法有三種,無論怎么寫,執(zhí)行流程都是①處理映射器通過@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開發(fā)方式 @RequestMapping("/hello") public ModelAndView hello(){ ModelAndView mv = new ModelAndView(); mv.addObject("msg","hello world!"); mv.setViewName("index.jsp"); return mv; } //2.原生Servlet開發(fā)方式 @RequestMapping("xx") public void xx(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException{ request.setAttribute("msg", "周冬雨"); request.getRequestDispatcher("/index.jsp").forward(request, response); } //3.開發(fā)中常用 @RequestMapping("yy") public String yy(Model model){ model.addAttribute("msg", "雙笙"); return "forward:/index.jsp";//forward寫不寫都是轉發(fā),redirect代表重定向. } }
1.SpringMVC開發(fā)方式
@RequestMapping("/hello") public ModelAndView hello(){ ModelAndView mv = new ModelAndView(); mv.addObject("msg","hello world!"); mv.setViewName("index.jsp"); return mv; }
2.Servlet原生開發(fā)方式
@RequestMapping("xx") public void xx(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException{ request.setAttribute("msg", "周冬雨"); request.getRequestDispatcher("/index.jsp").forward(request, response); }
3.開發(fā)中常用的方式
在return的字符串中,forward寫不寫都是代表轉發(fā),redirect則代表重定向。
@RequestMapping("yy") public String yy(Model model){ model.addAttribute("msg", "雙笙"); return "forward:/index.jsp"; }
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
郵件收發(fā)原理你了解嗎? 郵件發(fā)送基本過程與概念詳解(一)
你真的了解郵件收發(fā)原理嗎?這篇文章主要為大家詳細介紹了郵件發(fā)送基本過程與概念,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-10-10java中functional interface的分類和使用詳解
這篇文章主要介紹了java中functional interface的分類和使用,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-04-04