简体   繁体   English

我如何使用angular js将模型属性POJO对象发布到spring控制器

[英]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.

相关问题 Spring MVC - 如何使用控制器方法更新模型对象的属性? - Spring MVC - How do I update a model object's attributes using a controller method? 如何使用Spring / Thymeleaf表单将POJO添加到列表中? - How do I add a POJO to a list using Spring/Thymeleaf form? 如何使用spring注释将一个POJO对象注入另一个POJO? - How to inject one POJO object to another POJO using spring annotation? 如何从 angular.js 调用 spring mvc REST 控制器? - how do I call a spring mvc REST controller from angular.js? 如何将JSON对象分配给Hibernate Pojo + ext js 4 + Java + spring - How to assign JSON Object to Hibernate Pojo + ext js 4 + Java + spring 使用Spring初始化POJO对象 - Initialize POJO object using Spring 如何使用Spring MVC 3在控制器中从模型中获取对象? - How can I get an object out of the model in the controller with Spring MVC 3? 我们是否必须使用与控制器中的pojo对象完全相同的字段发布json对象? - Do we have to have to post json object with exactly same fields as in pojo object in controller? 在Spring中,如何从配置中创建具有默认值的POJO? - In Spring, how do I create a POJO with a default value from configuration? 我如何使用spring将对象从jsp传递到控制器 - How do i pass an object from jsp to controller with spring
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM