SpringMVC後臺接受前臺傳值的方法

菜雞03號發表於2016-08-29

1.HttpRequestServlet 接收前臺傳值

@RequestMapping("/go5")
	public String hello5(HttpServletRequest request){
		String name=request.getParameter("uname");
		String id=request.getParameter("uid");
		System.out.println(id+"----"+name);
		return "/WEB-INF/jsp/index.jsp";
	}

2.直接接收前臺傳值
 //接收單個引數
	 @RequestMapping("/hello")
	 public String hello(String name){
		 System.out.println(name);
		 return "/WEB-INF/jsp/index.jsp";
	 }

//接收多個引數
	 @RequestMapping("/go2")
	public String hello3(String name,int id){
		 System.out.println(name);
		 System.out.println(id);
		 return "/WEB-INF/jsp/index.jsp";
	 }

3.以物件形式接收前臺傳遞的資料

//以物件形式接受引數
	 @RequestMapping("/go4")
	 public String hello5( Student stu){
		 System.out.println(stu.getName());
		 System.out.println(stu.getId());
		 return "/WEB-INF/jsp/index.jsp";
	 }
public class Student {//類中必須要有無引數建構函式不然會有java.lang.NoSuchMethodException: org.entity.Student.<init>()錯誤
     private int id;
     private String name;
	public Student(int id, String name) {
		super();
		this.id = id;
		this.name = name;
	}
	
	public Student() {
		super();
	}
}

url:http://localhost:7080/myweb/go5.do?uid=18&uname=ii

4.restful風格
//restful風格
	 @RequestMapping("/detete/{uid}/{uname}")
	 public String hello2(@PathVariable("uid")int id,@PathVariable("uname")String name){
		 System.out.println(id+"  ---->"+name);
		 return "/WEB-INF/jsp/index.jsp";
	 }

url:http://localhost:7080/myweb/go5.do?uid=18&uname=ii




相關文章