springMVC接收数据和响应返回

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

1. 表单数据

后台直接用形参来接收表单的name。一一对应

@Controller
public class TestController {
    @RequestMapping(value="addUser.do",method= RequestMethod.POST)
    public String postTest2(String age,String userName){
        System.out.print("\n\n userName = "+userName);
        System.out.print("\n\n age = "+age);
        return "redirect:/html/TEST2.html";
    }

}

或者带有注解(这样可以当做转义变量!):

 @RequestMapping(value="addUser.do",method= RequestMethod.POST)
    public String postTest2(@RequestParam("userName") String userName22, @RequestParam("age") Integer age33){
        System.out.print("\n\n userName22 = "+userName22);
        System.out.print("\n\n age33 = "+age33);
        return "redirect:/html/TEST2.html";
    }

又或者使用HttpServletRequest类对象来接收!

但是要添加对应maven依赖



  javax.servlet
  javax.servlet-api
  4.0.1
  provided
 @RequestMapping(value="addUser.do",method= RequestMethod.POST)
    public String postTest2(@RequestParam("userName") String userName22, @RequestParam("age") Integer age33,HttpServletRequest request){
        System.out.print("\n\n userName22 = "+userName22);
        System.out.print("\n\n age33 = "+age33);
        String username=request.getParameter("userName");
        String password=request.getParameter("age");
        System.out.println("\n\nusername is:"+username);
        System.out.println("\n\npassword is:"+password);
        return "redirect:/html/TEST2.html";
    }
@RequestMapping(value="addUser.do",method= RequestMethod.POST)
    public String postTest2(HttpServletRequest request){
        String username=request.getParameter("userName");
        String password=request.getParameter("age");
        System.out.println("\n\nusername is:"+username);
        System.out.println("\n\npassword is:"+password);
        return "redirect:/html/TEST2.html";
    }

或者这样:接收到时用&拼接的字符串...:str is:userName=11&age=222

@RequestMapping(value="addUser.do",method= RequestMethod.POST)
    public String postTest2(@RequestBody String str){
        System.out.println("\n\nstr is:"+str);
        return "redirect:/html/TEST2.html";
    }

或者一个对象bean类来接收(但不能用@RequestBody来注释):

@RequestMapping(value="addUser.do",method= RequestMethod.POST)
    public String postTest2(Stu stu){
        System.out.println("\n\nstu is:"+stu);
        System.out.println("\n\nstu is:"+stu.getUserName());
        System.out.println("\n\nstu is:"+stu.getAge());
        return "redirect:/html/TEST2.html";
    }

2. 文件上传(multipart)

A. 首先添加maven依赖包


      commons-fileupload
      commons-fileupload
      1.3.2
    
      
          commons-net
          commons-net
          1.4.1
      

B.然后在bean的配置文件(applicationContext.xml或者dispatcher-servlet.xml...配置文件都可以)中,添加配置如下:


    

C. 前端:必须给form声明为:enctype="multipart/form-data"  然后带有一个file类型的input!注意属性name和变量对应关系!

D.后台使用MultipartFile类来接收!并来解析

也可以使用@RequestPart注解,并使用byte[]来接收二进制,或者用Part类对象接收。三种形式来接收都可以!

@RequestMapping(value="addUser.do",method= RequestMethod.POST)
    public String postTest2(@RequestParam("file") MultipartFile file){
        System.out.println("\n\nage file = :"+file.getOriginalFilename());
        return "redirect:/html/TEST2.html";
    }

PS.当然可以支持多个文件同时上传并设置上传最大容量的阈值l

E.限定上传文件大小

 
    
        
    

3. 发送头部数据

转载于:https://my.oschina.net/u/3697586/blog/1827741

你可能感兴趣的:(springMVC接收数据和响应返回)