简体   繁体   中英

How do i post model attribute POJO object to spring controller using angular js

I am new to angular JS, spring mvc.

Basically I am trying to post form data on my portal through angular JS as a model attribute POJO object to spring controller.

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.

@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---->

$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

When you're sending data over $http post you're sending jsons, not model attributes. Model attributes (as Spring sees them) are submitted forms. You should be able to find CheckInRequest in request params.

@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. You can go further and configure an ObjectMapper or a binder to have them deserialized by Spring.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM