gpt4 book ai didi

java - Ajax 表单提交和文件上传不起作用 Spring MVC

转载 作者:行者123 更新时间:2023-11-30 03:49:31 25 4
gpt4 key购买 nike

Ajax 代码:

var str = $("#observationForm").serialize();

$.ajax({
type : "post",
data : str,
url : "updateObservation",
async : true,
/* dataType : "multipart/form-data", */
success : function() {
alert("success");
}
});

JSP-Spring 表单:

<form:form modelAttribute="ObservationModal" action="updateObservation" id="observationForm">
<label class="control-label">Tooth No</label>
<input type="text" class="form-control" name="tooth" id="tooth" placeholder="Enter tooth no" />
<label class="control-label">Uploaded file(PDF)</label>
<input type="file" class="form-control" name="attachment" value="" id="attachment" placeholder="" />
<input type="button" value="Save" onclick="updateObservation();" />
</form:form>

Controller 类

@RequestMapping(value = "/updateObservation", method = RequestMethod.POST)
public @ResponseBody String updateObservation(
@ModelAttribute("ObservationModal") ObservationModal formObj) {
String result = "";

System.out.println(":" + formObj.getTooth());
System.out.println(formObj.getAttachment());

return result;
}

模态类

public class ObservationModal implements Serializable {
int tooth;
private List<MultipartFile> attachment;

public int getTooth() {
return tooth;
}

public void setTooth(int tooth) {
this.tooth = tooth;
}

public List<MultipartFile> getAttachment() {
return attachment;
}

public void setAttachment(List<MultipartFile> attachment) {
this.attachment = attachment;
}

}

我无法获取 Controller 中的值文本框值或附件。 ObservationModal 始终为 null

最佳答案

要进行 ajax 调用,url 类型必须为“/projectName/actualurl”。在您的情况下,网址:'/projectName/updateObservation'。并将 dataType:'text' 添加到调用中。

关于java - Ajax 表单提交和文件上传不起作用 Spring MVC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24773077/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com