緊跟上一篇博文,我們聊一下SpringMVC強大的功能。
RestFul風格
1.什麼是RestFul風格
Restful就是一個資源定位及資源操作的風格。不是標準也不是協議,只是一種風格。
基於這個風格設計的軟件可以更簡潔,更有層次,更易於實現緩存等機制。
2.傳統操作資源的方式與使用RestFul操作資源的方式有什麼區別
3.代碼測試
@Controller
public class HelloController {
//映射訪問路徑
@RequestMapping("/item/{p1}/{p2}")
public String index(@PathVariable int p1, @PathVariable int p2, Model model){
int result = p1+p2;
//Spring MVC會自動實例化一個Model對象用於向視圖中傳值
model.addAttribute("msg", "結果:"+result);
//返回視圖位置
return "Hello";
}
}
結果展示:
當輸入的參數不是int類型的結果:
總結:
1.通過路徑變量的類型可以約束訪問參數,如果類型不一樣,則訪問不到對應的請求方法
2.原本的?傳參的方式不能使用了
3.獲得參數更加方便,框架會自動進行類型轉換。
4.使用method屬性指定請求類型:約束請求的類型
代碼測試:將映射請求路徑改爲POST請求
//映射訪問路徑,必須是POST請求
@RequestMapping(value = "/hello",method = {RequestMethod.POST})
public String index(Model model){
model.addAttribute("msg", "hello!");
return "Hello";
}
運行結果:
所有的地址欄請求默認都會是 HTTP GET 類型的。
只需將POST請求改爲GET請求就可以運行成功。
5.組合註解
相當於@RequestMapping(method =RequestMethod.GET) 的一個快捷方式。
- @GetMapping
- @PostMapping
- @PutMapping
- @DeleteMapping
- @PatchMapping
數據跳轉方式:3種方式
1.ModelAndView
設置ModelAndView對象 , 根據view的名稱 , 和視圖解析器跳到指定的頁面 .
public class ControllerTest1 implements Controller {
public ModelAndView handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
//返回一個模型視圖對象
ModelAndView mv = new ModelAndView();
mv.addObject("msg","ControllerTest1");
mv.setViewName("Hello");
return mv;
}
}
2.ServletAPI
通過設置ServletAPI , 不需要視圖解析器
@Controller
public class ResultGo {
@RequestMapping("/result/t1")
public void test1(HttpServletRequest req, HttpServletResponse rsp) throws IOException {
rsp.getWriter().println("Hello,Spring BY servlet API");
}
@RequestMapping("/result/t2")
public void test2(HttpServletRequest req, HttpServletResponse rsp) throws IOException {
rsp.sendRedirect("/index.jsp"); //重定向
}
@RequestMapping("/result/t3")
public void test3(HttpServletRequest req, HttpServletResponse rsp) throws Exception {
//轉發
req.setAttribute("msg","Hello,Spring BY servlet API");
req.getRequestDispatcher("/WEB-INF/jsp/test.jsp").forward(req,rsp);
}
}
3.SpringMVC
通過SpringMVC來實現轉發和重定向
@Controller
public class ResultSpringMVC {
@RequestMapping("/item1")
public String test1(){
//轉發一
return "/index.jsp";
}
@RequestMapping("/item2")
public String test2(){
//轉發二
return "forward:/index.jsp";
}
@RequestMapping("/item3")
public String test3(){
//重定向
return "redirect:/index.jsp";
}
}
當執行/item1路徑時會出現錯誤:是因爲會走視圖解析器視圖名發生改變
當執行/item2或者3時不會發生錯誤。
數據處理
1.處理前端傳來的數據
(1)提交的域名稱和處理方法的參數名一致
提交數據 : http://localhost:8080/hello?name=wendi
@RequestMapping("/hello")
public String hello(String name){
System.out.println(name);
return "hello";
}
後臺輸出 : wendi
(2)提交的域名稱和處理方法的參數名不一致
提交數據 : http://localhost:8080/hello?username=wendi
//@RequestParam("username") : username提交的域的名稱 .
@RequestMapping("/hello")
public String hello(@RequestParam("username") String name){
System.out.println(name);
return "hello";
}
後臺輸出 : wendi
(3)提交的是一個對象:要求提交的表單域和對象的屬性名一致
實體類:
public class User {
private int id;
private String name;
private int age;
//構造
//get/set
//tostring()
}
提交數據 : http://localhost:8080/mvc04/user?name=wendi&id=1&age=15
@RequestMapping("/user")
public String user(User user){
System.out.println(user);
return "hello";
}
後臺輸出 : User { id=1, name=‘wendi’, age=15 }
如果使用對象的話,前端傳遞的參數名和對象名必須一致,否則就是null
2.數據顯示到前端
(1)第一種 : 通過ModelAndView
public class ControllerTest1 implements Controller {
public ModelAndView handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
//返回一個模型視圖對象
ModelAndView mv = new ModelAndView();
mv.addObject("msg","ControllerTest1");
mv.setViewName("test");
return mv;
}
}
(2)第二種 : 通過ModelMap
@RequestMapping("/hello1")
public String hello(@RequestParam("username") String name, ModelMap model){
//相當於req.setAttribute("name",name);
model.addAttribute("name",name);
return "hello";
}
(3)第三種 : 通過Model
@RequestMapping("/hello2")
public String hello(@RequestParam("username") String name, Model model){
//相當於req.setAttribute("name",name);
model.addAttribute("msg",name);
return "test";
}
三種的區別:
- ModelAndView 可以在儲存數據的同時,可以進行設置返回的邏輯視圖,進行控制展示層的跳轉。
- ModelMap 繼承了 LinkedMap ,除了實現了自身的一些方法,同樣的繼承 LinkedMap 的方法和特性;
- Model 只有寥寥幾個方法只適合用於儲存數據,簡化了新手對於Model對象的操作和理解