我正在使用Spring。我有一个控制器,可以为用户创建一个页面,然后用户输入数据。我需要捕获这些数据并放入数据库中 我的控制器
@Controller
@RequestMapping({"/pdc"})
public class PDCController {
// generate page to send to user
@RequestMapping(value = {"/{ffaAssgnId}"}, method = RequestMethod.GET)
public String getAssignment(Model model, @PathVariable Integer ffaAssgnId) {
Integer userContactId = userService.getUserContactId();
PDCAssgn assignment = pdcService.getAssgn(ffaAssgnId, userContactId);
List<MagValidate> statuses = validateService.getValidateByType("FAS");
Map<Integer, MagValidate> mapStatuses = new HashMap<>();
for (MagValidate status : statuses) {
mapStatuses.put(status.getValidateId(), status);
}
model.addAttribute("mapStatus", mapStatuses);
model.addAttribute("assignment", assignment);
return "pdc";
}
// get response from user
@RequestMapping(value = {""}, method = RequestMethod.POST)
public @ResponseBody AjaxResponse updateAssignment(@RequestBody PDCAssgn assignment) {
System.out.println(assignment.toString());
// try {
// pdcService.updateAssgn(assignment);
// } catch (Exception ex) {
// ex.printStackTrace();
// List<String> errors = new ArrayList<>();
// errors.add("Error saving DARF.");
// return new AjaxResponse("ERROR", null, errors);
// }
return new AjaxResponse("OK", null, null);
}
}
我呈现的表单的一部分
<td><input id="wholesalerIssues0.assgnIssue.promoDealers" name="wholesalerIssues[0].assgnIssue.promoDealers" class="numberLarge" type="text" value=""/></td>
<td><input id="wholesalerIssues0.assgnIssue.dealersFound" name="wholesalerIssues[0].assgnIssue.dealersFound" class="numberLarge" type="text" value=""/></td>
<td><input id="wholesalerIssues0.assgnIssue.curDraw" name="wholesalerIssues[0].assgnIssue.curDraw" class="numberLarge" type="text" value=""/></td>
<td><input id="wholesalerIssues1.assgnIssue.promoDealers" name="wholesalerIssues[1].assgnIssue.promoDealers" class="numberLarge" type="text" value=""/></td>
<td><input id="wholesalerIssues1.assgnIssue.dealersFound" name="wholesalerIssues[1].assgnIssue.dealersFound" class="numberLarge" type="text" value=""/></td>
<td><input id="wholesalerIssues1.assgnIssue.curDraw" name="wholesalerIssues[1].assgnIssue.curDraw" class="numberLarge" type="text" value=""/></td>
等效的jsp
<td><form:input path="wholesalerIssues[${whStatus.index}].assgnIssue.promoDealers" cssClass="numberLarge" placeholder="${targetTitle.promoDealers}"/></td>
<td><form:input path="wholesalerIssues[${whStatus.index}].assgnIssue.dealersFound" cssClass="numberLarge" placeholder="${targetTitle.dealersFound}"/></td>
<td><form:input path="wholesalerIssues[${whStatus.index}].assgnIssue.curDraw" cssClass="numberLarge" placeholder="${targetTitle.curDraw}"/></td>
用于发送数据的javascript
function doSendA(e) {
e.preventDefault();
var form = $("#frm-assignment");
var formFields = $("#frm-assignment input:not([readonly])")
var formData = {}
$.each(formFields, function(i, v){
var input = $(v);
// populate form data as key-value pairs
// with the name of input as key and its value as value
formData[input.attr("name")] = input.val();
});
console.log(formData);
$.ajax({
type: "POST",
url: "${pageContext.request.contextPath}/pdc",
contentType: "application/json; charset=utf-8" ,
data: JSON.stringify(formData),
dataType: "json",
success : function(ajaxResponse) {
if (ajaxResponse.status !== "OK") {
hideLoading();
showToast("bad", ajaxResponse.errors[0]);
return;
}
hideLoading();
// location.reload();
showToast("good", "Your data was saved.");
},
error : function(res) {
hideLoading();
showToast("bad", "There was a problem saving your data.");
}
});
}
console.log(formData);
{"wholesalerIssues[0].assgnIssue.promoDealers":"123","wholesalerIssues[0].assgnIssue.dealersFound":"","wholesalerIssues[0].assgnIssue.curDraw":"","wholesalerIssues[1].assgnIssue.promoDealers":"","wholesalerIssues[1].assgnIssue.dealersFound":"","wholesalerIssues[1].assgnIssue.curDraw":""}
控制器的System.out.println(assignment.toString());
magforce.model.PDCAssgn@4f71f251[wholesalerIssues=<null>]
我尝试打印请求中的内容,发现绑定期间没有错误,但是我得到的数据对象仍然为空。我已经做了很多Google查询,但是还没有找到任何使用Spring的表单标签(<form:form>
,<form:input>
等)以及ajax json和Spring的@RequestBody的东西。
答案 0 :(得分:0)
不是一个真正的答案,而是将其放在此处以扩展我的评论。这里的目的是向您展示如何测试您的终点。
您的控制器正在公开一个端点。如果启动您的应用程序,则您将具有一个URL,在这种情况下,您可以访问“ http://localhost:8080/pdc”(除非您为URL加上前缀,但根据发布的内容,这应该是您的URL)。如果您使用的是* NIX系统,则可以使用卷发器。如果您使用Windows,则可能需要尝试Postman。要在POST端点上使用curl,对端点的调用将类似于以下内容。当然,我不知道您的对象结构,因此请确保根据您的对象结构调整JSON。
curl -i -H "Accept: application/json" -H "Content-Type: application/json" \
-X POST -d \
'{
"yourObject":{
"someArray":[
{
"aDate":{
"year":2019,
"month":10,
"day":1
},
"someValue":4,
"otherVaule":10000,
"anId":1
}
],
"someOtherValue":100000,
"someStatus":"DRAFT"
}
}' http://localhost:8080/pdc
通过使用类似这样的工具测试您的端点,您可以找出问题所在。成功校准端点后,您至少会知道端点是正确的,并且任何问题都将在JavaScript代码中。