[英]How do i post model attribute POJO object to spring controller using angular js
I am new to angular JS, spring mvc. 我是Spring MVC的Angular JS新手。
Basically I am trying to post form data on my portal through angular JS as a model attribute POJO object to spring controller. 基本上,我试图通过angular JS将表单数据发布到我的门户网站上,作为spring控制器的模型属性POJO对象。
How do it go about it? 怎么做呢? You can find below the sample data that i require from the FORM and the angular JS function add() that works on ng-submit on the form. 您可以在FORM下方找到我需要的示例数据,并且可以在表单上的ng-submit上使用角度JS函数add()。
@RequestMapping(value="sample", method = RequestMethod.POST)
public String processSampleForm(@ModelAttribute("checkInRequest") CheckInRequest checkInRequest,
@ModelAttribute("sampleRequest") SampleRequestMO sampleRequest,
HttpServletRequest request, HttpServletResponse response, ModelMap model)
throws Exception
<------Angular JS----> <------ Angular JS ---->
$scope.add = function(){
var request = $http({
method: "post",
url: "sample.htm",
transformRequest: transformRequestAsFormPost,
sampleRequest: {
'phone': 9999999999,
'name': "Kim"
},
CheckInRequest: {
'merchantCode': 11,
'outletCode': 100
}
});
Any help will be highly appreciated. 任何帮助将不胜感激。
TIA TIA
When you're sending data over $http post you're sending jsons, not model attributes. 当您通过$ http帖子发送数据时,您发送的是json,而不是模型属性。 Model attributes (as Spring sees them) are submitted forms. 模型属性(如Spring所见)是提交的表单。 You should be able to find CheckInRequest
in request params. 您应该能够在请求参数中找到CheckInRequest
。
@RequestMapping(value="sample", method = RequestMethod.POST)
public String processSampleForm(@RequestParam("CheckInRequest") String checkInRequest, @RequestParam("sampleRequest") String sampleRequest, HttpServletRequest request, HttpServletResponse response, ModelMap model)
Note that without further configuration the params will be received as strings (representing json) and you have to deserialize them manually. 请注意,如果不进行进一步配置,则参数将作为字符串(代表json)接收,您必须手动反序列化它们。 You can go further and configure an ObjectMapper
or a binder to have them deserialized by Spring. 您可以走得更远,并配置一个ObjectMapper
或联编程序,以使它们由Spring反序列化。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.