I am using Struts1.2 with Angular to post some data and want to get the data in java.
Im able to extract data from the server and display it on the screen.
Now I'm trying to send some data using Angular to the server and try to get the data from request.getParameter in java. I made three attempts, but I could not.
Below I have also provided the following files with explanations and screenshots of my three attempts.
1. CartController.js
var myApp = angular.module('cartApp',[]); myApp.controller('CartController', function ($scope,$http) { $scope.bill = {}; $scope.items = []; $http.post('/StrutsWithAngular/shopingCart.do') .success(function(data, status, headers, config) { $scope.items = data; }) .error(function(data, status, headers, config) { //alert("Error :: "+data); }); // First Try $scope.postData = function() { $http({ method: 'POST', url: '/StrutsWithAngular/shopingCart.do', data: 'value=' + 'Parameter From Request' , headers: {'Content-Type': 'application/x-www-form-urlencoded'} }).success(function(data, status, headers, config) { alert("Success :: "+status); $scope.items = data; }).error(function(data, status, headers, config) { alert("Error :: "+data); }); }; // Second Try $scope.postData = function() { $http({ method: 'POST', url: '/StrutsWithAngular/shopingCart.do', data: 'cartValues=' + {cartValues : $scope.items} , headers: {'Content-Type': 'application/x-www-form-urlencoded'} }).success(function(data, status, headers, config) { $scope.items = data; }).error(function(data, status, headers, config) { // alert("Error :: "+data); }); }; // Third try $scope.postData = function() { $http({ method: 'POST', url: '/StrutsWithAngular/shopingCart.do', data: $scope.items }).success(function(data, status, headers, config) { $scope.items = data; }).error(function(data, status, headers, config) { // alert("Error :: "+data); }); }; });
2. CartAction.java
package com.myapp.action; import com.myapp.dto.ShopingCartDto; import java.io.IOException; import java.io.PrintWriter; import java.util.ArrayList; import java.util.List; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import net.sf.json.JSONArray; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; public class CartAction extends org.apache.struts.action.Action { private static final String SUCCESS = "success"; @Override public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { System.out.print("Cart App"); String value = request.getParameter("value"); System.out.print("value ::"+ value); String requestValue = request.getParameter("cartValues"); System.out.print("Requested Values ::"+ requestValue); if(requestValue!=null){ System.out.println("Requested Values :: "+requestValue); JSONObject object = JSONObject.fromObject(requestValue); System.out.println("Object Keys ::" +object.keys()); Iterator iter = object.keys(); System.out.println("Iter :: "+iter.toString()); while (iter.hasNext()) { String key = (String) iter.next(); System.out.println("Keys " + key); JSONArray array = object.getJSONArray(key); for (int i = 0; i < array.size(); i++) { JSONObject powertrainOperationJSON = JSONObject.fromObject(array.get(i)); } } } List<Object> shopingCartDtos = new ArrayList<>(); ShopingCartDto shopingCartDtoOne = new ShopingCartDto(); ShopingCartDto shopingCartDtoTwo = new ShopingCartDto(); ShopingCartDto shopingCartDtoThree = new ShopingCartDto(); shopingCartDtoOne.setSno(1); shopingCartDtoOne.setTitle("Title 1"); shopingCartDtoOne.setQuantity("11"); shopingCartDtoOne.setPrice("25"); shopingCartDtoTwo.setSno(2); shopingCartDtoTwo.setTitle("Title 2"); shopingCartDtoTwo.setQuantity("12"); shopingCartDtoTwo.setPrice("25"); shopingCartDtoThree.setSno(3); shopingCartDtoThree.setTitle("Title 3"); shopingCartDtoThree.setQuantity("13"); shopingCartDtoThree.setPrice("25"); shopingCartDtos.add(shopingCartDtoOne); shopingCartDtos.add(shopingCartDtoTwo); shopingCartDtos.add(shopingCartDtoThree); ajaxResponse(response, shopingCartDtos); return null; } }
/////
First try: when I tried with a single parameter in the request. Im was able to get the value in java
In the controller: -
data: 'value=' + 'Parameter From Request'
In Java: -
String value = request.getParameter("value"); System.out.print("value ::"+ value);
Parameter Value: Screenshot

Console exit

Second attempt:
Now, when I tried to get some heap of values in java, I couldn’t, here in this case I passed $ scope.item with the content type "application / x-www-form-urlencoded" in the parameter "cartValues", In java when trying get the value from request.getParameter ("cartValues") the value is printed as [object Object], as in the request. But when trying to parse value using JSON api in java, there is an exception
In the controller: -
data: 'cartValues=' + {cartValues : $scope.items} , headers: {'Content-Type': 'application/x-www-form-urlencoded'}
In Java: -
String requestValue = request.getParameter("cartValues"); System.out.print("Requested Values ::"+ requestValue);
Screenshot of my second attempt


Third attempt:
In this case, I only passed $ scope.item and removed the content type to pass it as JSON, but I have no clear idea of how to get the value in Java
In the controller: -
data: $scope.items
Screenshot of the third attempt
