@RequestMapping("/login") public String login(HttpServletRequest request){ String name = request.getParameter("name"); String pwd = request.getParameter("pwd"); return "success"; }
前端請求的表單javascript
<form action="${pageContext.request.contextPath}/getParam" method="post"> 用戶名:<input type="text" name="name" > 密碼:<input type="text" name="pwd" > <input type="submit" value="submit"> </form>
Controller代碼html
@RequestMapping(value="/getParam",method=RequestMethod.POST) public String getParam1(String name,String pwd){ System.out.println("用戶名"+name); System.out.println("密碼"+pwd); return "success"; }
package com.neuedu.bean; /* * 項目名稱:SpringMVC-02 * @author:wzc * @date 建立時間:2017年8月22日 上午9:23:33 * @Description:定義學生類 * @parameter * */ public class Student { private String name; private String email; private int sex; private Address address; public Student() { super(); } public Student(String name, String email, int sex) { super(); this.name = name; this.email = email; this.sex = sex; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public int getSex() { return sex; } public void setSex(int sex) { this.sex = sex; } public Address getAddress() { return address; } public void setAddress(Address address) { this.address = address; } @Override public String toString() { return "Student [name=" + name + ", email=" + email + ", sex=" + sex + ", address=" + address + "]"; } }
Controller代碼段前端
@RequestMapping("/getStudent") public String getStudent(Student student){ String name = student.getName(); String email = student.getEmail(); System.out.println("name:"+name); System.out.println("email:"+email); String studnetIn = student.toString(); System.out.println(studnetIn); return "success"; }
<a href="${pageContext.request.contextPath }/testUrl?id=5">走一個,測試URL</a><br>
@RequestMapping(value="/testUrl",method=RequestMethod.GET) public String testURL( @RequestParam(value="id") Integer id){ // service.getOrderById(id) System.out.println("---------"+id); return "success"; }
<a href="${pageContext.request.contextPath}/order/1">測試get</a><br>
@RequestMapping(value="/order/{id}") public String getorderbyId1(@PathVariable(value="id") Integer id){ // service.getOrderById(id) System.out.println("---------"+id); return SUCCESS; }
value:這個字段要與請求參數的name屬性值一致!
required:布爾值,默認是true,當指定爲false的時候,說明這個參數不是必須的,能夠不帶!
defaultValue:在咱們不傳值的時候,默認使用defaultValue的值,傳遞參數的時候,使用咱們傳遞的參數值!java
HttpServletRequest
HttpServletResponse
HttpSessionjquery
public String login(HttpServletRequest request,HttpServletResponse response ,HttpSession session){ }
如:request。setAttribute(「user」,new User("張三",1));spring
ModelAndView ,Map ,Model,ModelMapjson
–ModelAndView: 處理方法返回值類型爲 ModelAndView時, 方法體便可經過該對象添加模型數據
–Map 及 Model、ModelMap: 入參爲 org.springframework.ui.Model、org.springframework.ui. ModelMap 或 java.uti.Map 時,session
處理 方法返回時,Map 中的數據會自動添加到模型中。 app
@RequestMapping("/testMoudle") public String testMoudle(Map<String,Object> map){ map.put("user", "張三"); return "success"; } @RequestMapping("/testMoudle1") public ModelAndView testMoudle1(){ ModelAndView mAndView=new ModelAndView(); mAndView.addObject("user", "李四"); mAndView.setViewName("success"); return mAndView; } @RequestMapping("/testMoudle2") public String testMoudle(ModelMap modelMap){ modelMap.addAttribute("user", "王五"); return "success"; } @RequestMapping("/testMoudle3") public String testMoudle2(Model model){ model.addAttribute("user", "趙七"); return "success"; }
不管咱們的返回值是String類型仍是ModelAndView類型,SpringMVC框架執行目標Handler方法以後都會將返回值解析爲ModelAView;
咱們放入到Map或者Model、ModelMap中的數據都會放入ModelAndView對象中,做爲MOdel使用!框架
在前端頁面中均可以經過做用域將參數取出來
1.加入json的3個jar包
jackson-annotations-2.1.5.jar
jackson-core-2.1.5.jar
jackson-databind-2.1.5.jar
2. 編寫目標方法,使其返回 JSON 對應的對象或集合
3. 在方法上添加 @ResponseBody 註解
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!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=UTF-8"> <title>Insert title here</title> <script type="text/javascript" src="${pageContext.request.contextPath }/js/jquery-1.9.1.min.js"></script> <script type="text/javascript"> $(function(){ $("#btn").click(function(){ var url="${pageContext.request.contextPath}/testJson"; var data={}; function callback(date){ for(var i= 0;i < date.length; i++){ alert(date[i].name); } } $.post(url,data,callback); }); }); </script> </head> <body> <button name="OK" id="btn">測試JSON</button> </body> </html>
@RequestMapping(value="/testJson",method=RequestMethod.POST) @ResponseBody public List<Student> testJSON(){ List<Student> list=new ArrayList<>(); list.add(new Student("張三 ", 0)); list.add(new Student("李四 ", 1)); list.add(new Student("王五 ", 0)); return list; }