SpringMVC后台接受前台传值的方法

1.HttpRequestServlet 接收前台传值

  1. @RequestMapping("/go5")  
  2.     public String hello5(HttpServletRequest request){  
  3.         String name=request.getParameter("uname");  
  4.         String id=request.getParameter("uid");  
  5.         System.out.println(id+"----"+name);  
  6.         return "/WEB-INF/jsp/index.jsp";  
  7.     }  
@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.直接接收前台传值
  1. //接收单个参数  
  2.  @RequestMapping("/hello")  
  3.  public String hello(String name){  
  4.      System.out.println(name);  
  5.      return "/WEB-INF/jsp/index.jsp";  
  6.  }  
 //接收单个参数
	 @RequestMapping("/hello")
	 public String hello(String name){
		 System.out.println(name);
		 return "/WEB-INF/jsp/index.jsp";
	 }

  1. //接收多个参数  
  2.      @RequestMapping("/go2")  
  3.     public String hello3(String name,int id){  
  4.          System.out.println(name);  
  5.          System.out.println(id);  
  6.          return "/WEB-INF/jsp/index.jsp";  
  7.      }  
//接收多个参数
	 @RequestMapping("/go2")
	public String hello3(String name,int id){
		 System.out.println(name);
		 System.out.println(id);
		 return "/WEB-INF/jsp/index.jsp";
	 }

3.以对象形式接收前台传递的数据

  1. //以对象形式接受参数  
  2.      @RequestMapping("/go4")  
  3.      public String hello5( Student stu){  
  4.          System.out.println(stu.getName());  
  5.          System.out.println(stu.getId());  
  6.          return "/WEB-INF/jsp/index.jsp";  
  7.      }  
//以对象形式接受参数
	 @RequestMapping("/go4")
	 public String hello5( Student stu){
		 System.out.println(stu.getName());
		 System.out.println(stu.getId());
		 return "/WEB-INF/jsp/index.jsp";
	 }
  1. public class Student {//类中必须要有无参数构造函数不然会有java.lang.NoSuchMethodException: org.entity.Student.<init>()错误  
  2.      private int id;  
  3.      private String name;  
  4.     public Student(int id, String name) {  
  5.         super();  
  6.         this.id = id;  
  7.         this.name = name;  
  8.     }  
  9.       
  10.     public Student() {  
  11.         super();  
  12.     }  
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();
	}
  1. }  
}

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

4.restful风格
  1. //restful风格  
  2.      @RequestMapping("/detete/{uid}/{uname}")  
  3.      public String hello2(@PathVariable("uid")int id,@PathVariable("uname")String name){  
  4.          System.out.println(id+"  ---->"+name);  
  5.          return "/WEB-INF/jsp/index.jsp";  
  6.      }  
//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
---知识积累
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值