我正在尝试使用java spring向控制器提交一个表单,在下面的代码中,我通过以下方式成功检索文件元素但未获取如何检索其他元素(短名称和全名)值。 请帮帮我。
<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 :(得分:1)
首先更改输入元素并为shortName和fullName创建name属性,如下所示:
<input type="hidden" id="shortNameId" name="shortName" value="michael">
<input type="hidden" id="fullNameId" name="fullName" value="michael jackson">
但是你可以删除默认值属性,只需在页面渲染时自己输入值,这样值=“michael”&amp; value =“michael jackson”是可选的!
然后你可以检索这样的输入元素:
@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
){ .... }
祝你好运!
答案 1 :(得分:0)
在您的控制器中,尝试这样的事情,
@RequestMapping(value = "/your/url/{formParamenter}", method = RequestMethod.GET)
public String yourfunction(@PathVariable("formParameter") Type formParameter{}
Type是数据类型,(String / int / float..etc)。
在您的情况下,只需将RequestPart更改为@PathVariable
即可