版權聲明: 本文由 一隻博客 發表於 bloghome博客html
前端傳到Controller:java
方法1spring
經過HttpServletRequest 。寫法以下:多線程
@Controller public class MyTestController { @RequestMapping("/print") public String PrintInfo(HttpServletRequest request) { System.out.println("name:" +request.getParameter("name")); System.out.println("age:" + request.getParameter("age")); return "testpage"; } }
HttpServletRequest類是Servlet中的類型,表明了一個Servlet請求。不管Post仍是Get請求,都能經過這種方式獲取到。併發
好比上面的代碼,經過Get方法,以下地址app
http://127.0.0.1:8080/WebApp/print?name=zhangsan&age=30jsp
也能夠經過Post方法,使用Postman工具模擬一個post請求,均可以將值傳到Controller。ide
這招能夠得到Cookie以及Session數據。工具
還能夠經過註解@Autowired,將HttpServletRequest 自動的注入進來,沒必要擔憂多線程下的併發問題,由於這裏HttpServletRequest注入的是一個AOP proxy ,而不是一個普通bean 。每次請求過來,都會檢查線程本地屬性,來獲取真正的Request對象。這些都是Spring自動配置的默認場景。能夠參閱https://docs.spring.io/spring/docs/current/spring-framework-reference/html/beans.html#beans-factory-scopes-other-injection
可是不推薦使用這個方法,由於這種方法破壞了對一個注入對象的常規理解,形成混亂。
代碼以下:
@Controller public class MyTestController { @Autowired private HttpServletRequest request; @RequestMapping(value="/print") public String PrintInfo() { System.out.println("name:" +request.getParameter("name")); System.out.println("age:" + request.getParameter("age")); return "testpage"; } }
方法2
使用路徑變量。寫法以下:
@Controller public class MyTestController { @RequestMapping("/print/{name}/{age}") public String PrintInfo(@PathVariable String name, @PathVariable int age) { System.out.println("name:" + name); System.out.println("age:" + age); return "testpage"; } }
@RequestMapping中的{}中即爲路徑變量,該變量還須要在方法的參數值出現,而且標記@PathVariable。
經過URL匹配的方式既能夠實現傳值,這是REST風格的一種傳值方式。
上面的例子,只需輸入URL:
http://127.0.0.1:8080/WebApp/print/ZhangSan/30
controller接收到傳值,輸出:
name:ZhangSan
age:30
@RequestMapping("/print/{name}/{age}")是@RequestMapping(Value="/print/{name}/{age}")的縮寫形式,本質上是同樣的。
方法3
參數名匹配的方式:
@Controller public class MyTestController { @RequestMapping(value="/print") public String PrintInfo(String name, int age) { System.out.println("name:" +name); System.out.println("age:" + age); return "testpage"; } }
或者:
@Controller public class MyTestController { @RequestMapping(value="/print") public String PrintInfo(@RequestParam("name") String name,@RequestParam("age") int age) { System.out.println("name:" +name); System.out.println("age:" + age); return "testpage"; } }
當請求傳入的參數名字和controller
中代碼的名字同樣的時候,兩種方式均可以,區別在於使用了註解@RequestParam,能夠設置一個默認值來處理到null值。
@RequestParam(value="name", defaultValue="John")
可是若是請求中參數的名字和變量名不同的時候,就只能使用@RequestParam註解。例如請求的參數爲以下的時候:
http://localhost:8080/WebApp/print?user_name=somename&user_age=30
Controller代碼只能以下的寫法
@RequestMapping(value="/print") public String PrintInfo(@RequestParam("user_name") String name, @RequestParam("user_age")int age) { ... }
儘可能使用@RequestParam註解,由於這樣能夠清晰的知道該參數來自Request,可讀性高。
方法4
傳遞請求頭中的參數,須要用到@RequestHeader註解,該註解將Header中的值綁定到參數上,能夠獲取一個,多個或者全部的參數。例如
@Controller public class MyTestController { @RequestMapping(value="/print") public String PrintInfo(@RequestHeader Map<String, String> headers) { for (String elem: headers.keySet()) { System.out.println(elem + " : " + headers.get(elem)); } return "testpage"; } }
或者
@Controller public class MyTestController { @RequestMapping(value="/print") public String PrintInfo(@RequestHeader("User-Agent") String userAgent) { System.out.println("12"); System.out.println("name:" +userAgent); //System.out.println("age:" + age); return "testpage"; } }
方法5
使用到@RequestBody註解,獲得整個RequestBody的信息
@Controller public class MyTestController { @RequestMapping(value="/print") public String PrintInfo(@RequestBody String body) { System.out.println("body:" +body); return "testpage"; } }
@RequestBody能夠將Json數據直接映射程Java對象。例如:
方法6
採用@ModelAttribute註解,命名匹配,Post中的參數值和Model中的參數值一致的話,會自動綁定到該值。
@Controller public class MyTestController { @RequestMapping(value="/print") public String PrintInfo(@ModelAttribute User user) { System.out.println("6"); System.out.println("Name:" +user.getName()); System.out.println("Age:" +user.getAge()); return "testpage"; } } public class User { private String name; private int age; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
而後當Post的值中有name和age時,Controller中的user對象會自動附上值。
Controller傳遞到JSP
方法1
使用ModelAndView類,代碼以下:
@RequestMapping("/hello") public ModelAndView showMessage() { ModelAndView mv = new ModelAndView("helloworld"); mv.addObject("userList", GetUserList()); return mv; } public List<User> GetUserList() { List<User> lst=new ArrayList<User>(); User user1=new User(); user1.setName("zhangsan"); user1.setAge(20); lst.add(user1); User user2=new User(); user2.setName("lisi"); user2.setAge(30); lst.add(user2); return lst; }
JSP頁面中:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Spring 4 MVC -HelloWorld</title> </head> <body> <c:forEach items="${userList}" var="user"> ${user.name} ${user.age} <br /> </c:forEach> </body> </html>
ModelAndView 初始化的時候,設置了view的名字,同時也把對象存起來,直接傳給view。簡單實用。
方法2
使用Model或者ModelMap
(Model是一個接口,ModelMap實現了Model接口)
該方法和ModelAndView方法類似,只是Model和View分開來了,經過返回一個String來找到View,Model是注入到Controller的一個參數,經過對它添加屬性,在jsp端讀取值。代碼以下:
@Controller public class HelloWorldController { String message = "Welcome to Spring MVC!"; @RequestMapping("/hello") public String showMessage(Model model) { model.addAttribute("userList", GetUserList()); return "helloworld"; } public List<User> GetUserList() { List<User> lst=new ArrayList<User>(); User user1=new User(); user1.setName("zhangsan"); user1.setAge(10); lst.add(user1); User user2=new User(); user2.setName("lisi"); user2.setAge(33); lst.add(user2); return lst; } }
JSP頁面中:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Spring 4 MVC -HelloWorld</title> </head> <body> <c:forEach items="${userList}" var="user"> ${user.name} ${user.age} <br /> </c:forEach> </body> </html>