SpringMVC框架實現(xiàn)Handler處理器的三種寫法
一、SpringMVC中的處理器
配置完SpringMVC的處理器映射器,處理適配器,視圖解析器后,需要手動寫處理器。關(guān)于處理器的寫法有三種,無論怎么寫,執(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寫不寫都是轉(zhuǎn)發(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寫不寫都是代表轉(zhuǎn)發(fā),redirect則代表重定向。
@RequestMapping("yy")
public String yy(Model model){
model.addAttribute("msg", "雙笙");
return "forward:/index.jsp";
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Spring Boot 訪問安全之認證和鑒權(quán)詳解
這篇文章主要介紹了Spring Boot 訪問安全之認證和鑒權(quán),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-11-11
Apache?log4j2-RCE?漏洞復(fù)現(xiàn)及修復(fù)建議(CVE-2021-44228)
Apache?Log4j2是一款Java日志框架,大量應(yīng)用于業(yè)務(wù)系統(tǒng)開發(fā)。2021年11月24日,阿里云安全團隊向Apache官方報告了Apache?Log4j2遠程代碼執(zhí)行漏洞(CVE-2021-44228),本文給大家介紹Apache?log4j2-RCE?漏洞復(fù)現(xiàn)(CVE-2021-44228)的相關(guān)知識,感興趣的朋友一起看看吧2021-12-12
Java基礎(chǔ)開發(fā)之JDBC操作數(shù)據(jù)庫增刪改查,分頁查詢實例詳解
這篇文章主要介紹了Java基礎(chǔ)開發(fā)之JDBC操作數(shù)據(jù)庫增刪改查,分頁查詢實例詳解,需要的朋友可以參考下2020-02-02
Redis中String字符串和sdshdr結(jié)構(gòu)體超詳細講解
這篇文章主要介紹了Redis中String字符串和sdshdr結(jié)構(gòu)體,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2023-04-04

