在java中如何在控制器上检索表单元素
我试图使用JavaSpring向控制器提交一个表单,在下面的代码中,我成功地按照下面的方式检索文件元素,但没有获得如何检索其他元素(shortname和FullName)值。 请帮帮我在java中如何在控制器上检索表单元素,java,spring,multipartform-data,form-data,Java,Spring,Multipartform Data,Form Data,我试图使用JavaSpring向控制器提交一个表单,在下面的代码中,我成功地按照下面的方式检索文件元素,但没有获得如何检索其他元素(shortname和FullName)值。 请帮帮我 <body> <div style="text-align: center; margin-top: 60px;"> <form action="upload" enctype="multipart/form-data"> <
<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 = "/your/url/{formParamenter}", method = RequestMethod.GET)
public String yourfunction(@PathVariable("formParameter") Type formParameter{}
类型是数据的类型,(String/int/float..等)
在您的情况下,只需将RequestPart更改为@PathVariable,首先更改输入元素并为shortName和fullName创建name属性,如下所示:
<input type="hidden" id="shortNameId" name="shortName" value="michael">
<input type="hidden" id="fullNameId" name="fullName" value="michael jackson">
祝你好运 你试过
@RequestParam-String-shortName
和@RequestParam-String-fullName
吗?是的,我试过你的语法,但没用。谢谢你的回复。
<input type="hidden" id="shortNameId" name="shortName" value="michael">
<input type="hidden" id="fullNameId" name="fullName" 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
){ .... }