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

来源:互联网 发布:下载语音助手软件 编辑:程序博客网 时间:2024/05/21 14:53

1.HttpRequestServlet 接收前台传值

[java] view plain copy
print?
  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.     }  

2.直接接收前台传值
[java] view plain copy
print?
  1. //接收单个参数  
  2.  @RequestMapping("/hello")  
  3.  public String hello(String name){  
  4.      System.out.println(name);  
  5.      return "/WEB-INF/jsp/index.jsp";  
  6.  }  

[java] view plain copy
print?
  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.      }  

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

[java] view plain copy
print?
  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.      }  
[java] view plain copy
print?
  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.     }  
[java] view plain copy
print?
  1. }  

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

4.restful风格
[java] view plain copy
print?
  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.      }  

url:http://localhost:7080/myweb/go5.do?uid=18&uname=ii
---知识积累