2014-03-19 28 views
0

我想提交一個表單到控制器在使用java春天,在下面的代碼中我通過以下方式成功檢索文件元素,但沒有得到如何檢索其他元素(短名稱和全部名)值。 請幫我一把。如何檢索在控制器中的表單元素在java

<body> 
    <div style="text-align: center; margin-top: 60px;"> 
     <form action="upload" enctype="multipart/form-data"> 
      <input type="hidden" id="shortName" name="michael"> 
      <input type="hidden" id="fullName" name="michael jackson"> 
      Select file: 
      <input type="file" name="dataFile" id="fileAttachment"/><br/><br/> 
       <div style="text-align: center; margin-top: 100px;"> 
        <input style="cursor: pointer;" onmouseover="" onclick="uploadAttachment()" class="dialogbox" type="submit" value="Upload Report" /> 
       </div> 
     </form> 
    </div> 
</body> 

控制器端代碼:

@RequestMapping(value = "upload", method=RequestMethod.POST) 
public void upload(HttpServletRequest request, HttpServletResponse response, 
      @RequestPart("dataFile") MultipartFile file 
      ){ 
System.out.println(file.getSize()); 
} 
+0

你有沒有試過'@RequestParam字符串shortName'和'@RequestParam字符串fullName'? – sp00m

+0

是的,我確實嘗試了你的語法,但是這有助於幫助。感謝您的回覆。 – b22

回答

1

第一個變化的投入要素,並創建了name屬性都SHORTNAME和全名,像這樣:

<input type="hidden" id="shortNameId" name="shortName" value="michael"> 
<input type="hidden" id="fullNameId" name="fullName" value="michael jackson"> 

但是你可以刪除默認值屬性只需在頁面呈現時自己輸入值,值=「michael」& value =「邁克爾傑克遜」是可選的!

然後你可以檢索這些輸入內容是這樣的:

@RequestMapping(value = "upload", method=RequestMethod.POST) 
public void upload(HttpServletRequest request, HttpServletResponse response, @RequestParam("shortName")String shortName, @RequestParam("fullName")String fullName 
     @RequestPart("dataFile") MultipartFile file 
     ){ .... } 

祝您好運!

+0

這解決了我的查詢,謝謝隊友! – b22

+0

不客氣! – M2hp

0

在你的控制器,嘗試這樣的事情,

@RequestMapping(value = "/your/url/{formParamenter}", method = RequestMethod.GET) 
    public String yourfunction(@PathVariable("formParameter") Type formParameter{} 

類型是數據,(串/ INT /浮點類型..等等)。

在你的情況只是改變RequestPart到@PathVariable