springMVC接收數據和響應返回

1. 表單數據

後臺直接用形參來接收表單的name。一一對應html

@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類對象來接收!java

可是要添加對應maven依賴web

<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
<dependency>
  <groupId>javax.servlet</groupId>
  <artifactId>javax.servlet-api</artifactId>
  <version>4.0.1</version>
  <scope>provided</scope>
</dependency>
@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=222spring

@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來註釋):api

@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依賴包app

<dependency>
      <groupId>commons-fileupload</groupId>
      <artifactId>commons-fileupload</artifactId>
      <version>1.3.2</version>
    </dependency>
      <dependency>
          <groupId>commons-net</groupId>
          <artifactId>commons-net</artifactId>
          <version>1.4.1</version>
      </dependency>

B.而後在bean的配置文件(applicationContext.xml或者dispatcher-servlet.xml...配置文件均可以)中,添加<bean>配置以下:maven

<!--上傳文件-->
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"></bean>

C. 前端:必須給form聲明爲:enctype="multipart/form-data"  而後帶有一個file類型的input!注意屬性name和變量對應關係!ide

<form enctype="multipart/form-data" action="addUser.do" method="post">
    <input type="file" name="file">
    <input type="submit" value="上傳" />
</form>

D.後臺使用MultipartFile類來接收!並來解析post

也可使用@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.限定上傳文件大小

<!--上傳文件-->
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="maxUploadSize" value="1000000"></property>
    </bean>

3. 發送頭部數據

相關文章
相關標籤/搜索