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

