我正在尝试绑定与AJAX调用中的其他数据一起序列化的表单数据。我似乎无法让Spring接受它。它只是实例化一个空表格。
JavaScript的:
$.ajax({
method: "GET",
url: contextPath,
data: {"form": serializedForm, "otherStuff": otherStuff},
contentType:"application/json; charset=utf-8",
cache: false,
success: function(data) {});
控制器:
@ResponseBody
@RequestMapping(value="/blah", method=RequestMethod.GET)
public String blah(@ModelAttribute("form") Form form, @RequestParam(value="otherStuff[]", required=false) String[] otherStuff, HttpServletResponse response){
//stuff
}
有没有办法让Spring在方法参数中映射我的表单?
似乎我可以从HttpServletRequest中单独获取参数,但我最好喜欢我的表单来通过InitBinder。
答案 0 :(得分:0)
问题似乎与AJAX请求有关。对每个方法参数使用键值配对不适用于数据绑定。相反,如果我将序列化数据连接成单个字符串,Spring将能够将它映射到每个对象。
public GoogleCloudDialogflowV2WebhookResponse Search(GoogleCloudDialogflowV2WebhookRequest request)
{
GoogleCloudDialogflowV2WebhookResponse whr = new GoogleCloudDialogflowV2WebhookResponse();
whr.FulfillmentMessages = new List<GoogleCloudDialogflowV2IntentMessage>();
whr.FulfillmentMessages.Add(new GoogleCloudDialogflowV2IntentMessage()
{
Platform = "ACTIONS_ON_GOOGLE",
SimpleResponses = new GoogleCloudDialogflowV2IntentMessageSimpleResponses()
{
SimpleResponses = new List<GoogleCloudDialogflowV2IntentMessageSimpleResponse>()
{
new GoogleCloudDialogflowV2IntentMessageSimpleResponse()
{
DisplayText = "sample text",
Ssml = "<speak>sample text</speak>"
}
}
},
BasicCard = new GoogleCloudDialogflowV2IntentMessageBasicCard()
{
Title = "sample title",
Subtitle = "sample subtitle",
FormattedText = "sample formatted text",
Image = new GoogleCloudDialogflowV2IntentMessageImage()
{
ImageUri = "https://assistant.google.com/static/images/molecule/Molecule-Formation-stop.png",
AccessibilityText = "sample image"
}
}
});
return whr;
}
我不确定是否有更简洁的方法可以避免将对象聚集成一个字符串,但这是有效的。