@POST
@Path("post")
@Consumes("multipart/form-data")
public Response postCategory(@FormDataParam("text1") String text1,
@FormDataParam("file1") InputStream file1,
@FormDataParam("text2") String text2,
@FormDataParam("file2") InputStream file2) {
System.out.println("CategoryService.postCategory()");
System.out.println("text1:" + text1);
System.out.println("text2:" + text2);
String uploadedFileLocation = "d://uploaded/test.jpg";
writeToFile(file1, uploadedFileLocation);
return Response.ok().build();
}
通过这段代码,我可以获得 HtmlInputFile 作为 InputStream,HtmlInputText 作为 String,但我想获得HtmlInputText 的其他属性,如 id、name、class
甚至更多:我的动态属性如下所示:
<input type='text' myattr='myattr-value' name='inpp'/>
最佳答案
提交表单时(根据 WC3 spec ),名称和值将发送到服务器。要获得其他值,我建议创建一些额外的隐藏输入。
<input type="hidden" name="valueOne" value="">
<input type="hidden" name="valueTwo" value="">
还有一些 javascript 在提交时设置这些字段。
<script>
document.yourForm.elements["valueOne"].value = "someValue";
document.yourForm.elements["valueTwo"].value = "someOtherValue";
</script>
<form name="yourForm" method="POST" onSubmit="setValues();">
但是您可能想问为什么 id 和类对表单提交很重要。这种类型的设计可能会将您的后端 Controller 与前端标记过于紧密地耦合在一起。这可能会导致一些问题。
关于java - 获取 FormDataParam 属性和特性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13788254/