无法将动态发票值发送到struts2服务器

时间:2019-11-16 11:26:30

标签: javascript angularjs struts2

我正在开发angularjs 1.6.5

我正在使用以下代码发送发票页面的数据。

$scope.products = [{qty: 0, description: ' ', sellingPrice: 0.0, unitPrice: 0.0, total: 0.0}];
$scope.fproducts = [];
$scope.generateInvoice = function () {
    console.log("generateInvoice");
    console.log($scope.fproducts);
    console.log("sub total " + $scope.subTotal + " ft " + $scope.finalTotal + "  pamt " + $scope.paidAmount + "  baldue " + $scope.balancedue);
    $scope.bd1 = {
        'subTotal': $scope.subTotal,
        'total': $scope.finalTotal,
        'TotalPaid': $scope.paidAmount,
        'invDateStr': $filter('date')($scope.invoiceDate, "MM/dd/yyyy"),

    };
    if ($scope.fproducts.length > 0) {
        $scope.fproducts.forEach((total, index) => {
            Object.entries(total).forEach(([key, value]) => {
                console.log(index + "  " + key + " " + value);
                $scope.bd1[`billProductList[${index}].${key}`] = value;
            });
        });
    }
    //commenting above for each and removing comment from below code is 
    // working properly but I want to send dynamic data with above code
/*  $scope.bd1[`billProductList[0].id`] = 1;
    $scope.bd1[`billProductList[0].description`] = 1;
    $scope.bd1[`billProductList[0].discountPercent`] = 150;
    $scope.bd1[`billProductList[0].qty`] = 10;
    $scope.bd1[`billProductList[0].sellingPrice`] = 100;
    $scope.bd1[`billProductList[0].unitPrice`] = 100;
    $scope.bd1[`billProductList[0].total`] = 150;*/

    $scope.bd1[`BillPaidDetailses[0].paymentMode`] = $scope.paymentMode;
    $scope.bd1[`BillPaidDetailses[0].amount`] = $scope.paidAmount; 
    console.log($scope.bd1);
    $http({
        method: 'POST',
        url: 'added-invoice',
        data: $httpParamSerializerJQLike($scope.bd1),
        headers: {'Content-Type': 'application/x-www-form-urlencoded'}
    }).then(function successCallback(data, status, headers, config) {
        if (data.data.st === 1) {
            $scope.success = true;
            window.location = 'bhome#!/show-tax';

        } else if (data.data.st === 0) {
            $scope.success = false;
            $scope.failure = true;
        } else if (data.data.st === -1) {
            $scope.success = false;
            $scope.failure = true;
        }
    }, function errorCallback(data, status, header, config) {
        $scope.success = false;
        $scope.failure = true;
    });
}

以上代码未将值传递给服务器端对象。 其console.log值为enter image description here

修改以上数据

   /*if ($scope.fproducts.length > 0) {
        $scope.fproducts.forEach((total, index) => {
            Object.entries(total).forEach(([key, value]) => {
                console.log(index + "  " + key + " " + value);
                $scope.bd1[`billProductList[${index}].${key}`] = value;
            });
        });
    }*/
    $scope.bd1[`billProductList[0].id`] = 1;
    $scope.bd1[`billProductList[0].description`] = 1;
    $scope.bd1[`billProductList[0].discountPercent`] = 150;
    $scope.bd1[`billProductList[0].qty`] = 10;
    $scope.bd1[`billProductList[0].sellingPrice`] = 100;
    $scope.bd1[`billProductList[0].unitPrice`] = 100;
    $scope.bd1[`billProductList[0].total`] = 150;

因为这是将值传递给服务器对象 其console.log为enter image description here

我想使用if条件中指定的动态值来运行我的代码。 我无法解决什么问题?

EDIT (我正在使用服务器端struts2) 我的代码是

public class BillNewAction extends ActionSupport implements ModelDriven<BillDetails> {

    BillDetails bd = new BillDetails();
    private List<BillDetails> billList = new ArrayList<BillDetails>();

    public String insert() {
        System.out.println("total " + bd.getTotal()
                + " subTotal " + bd.getSubTotal() + " paid " 
                + bd.getTotalPaid()
                + " invoiceDate " + bd.getInvDateStr()
        );
        SimpleDateFormat formatter1 = new SimpleDateFormat("MM/dd/yyyy");
        try {
            bd.setInvoiceDate((new java.sql.Date(formatter1.parse(bd.getInvDateStr()).getTime())));
        } catch (ParseException ex) {
            Logger.getLogger(BillNewAction.class.getName()).log(Level.SEVERE, null, ex);
        }
        for (BillPaidDetails b : bd.getBillPaidDetailses()) {
            System.out.println("type " + b.getPaymentMode() + " amount "
                + b.getAmount()
            );
        }
        System.out.println("product size " + bd.getBillPrList().size());
        for (BillProduct b : bd.getBillPrList()) {
            System.out.println("id " + b.getId() + " desc  " 
                + b.getDescription() + " qty " + b.getQty()
                + " sp " + b.getSellingPrice() + " up " 
                + b.getUnitPrice() + " " + b.getTotal()
            );
        }
    }
}

公共类BillDetails实现java.io.Serializable {

private Long billNo;
private Client client;
private BigDecimal subTotal;
private BigDecimal TotalAmount;//total price
private BigDecimal TotalPaid;//Amount paid for  getting items
private BigDecimal vat;
private BigDecimal total;
private String invoiceNo;
private Date invoiceDate;
private String invDateStr;
private List<BillPaidDetails> BillPaidDetailses = new ArrayList<BillPaidDetails>();
private List<BillProduct> billPrList = new ArrayList<BillProduct>();
//getter and setter
 }

我必须将$scope.bd1[billProductList[0].id] = 1;格式的数据发送到服务器

分配将数据传递给服务器的单个值,但我没有动态值,因此我正在尝试

if ($scope.fproducts.length > 0) { 
    $scope.fproducts.forEach((total, index) => {
        Object.entries(total).forEach(([key, value]) => {
            console.log(index + " " + key + " " + value);
            $scope.bd1[billProductList[${index}].${key}] = value;
        });
    });
}

不起作用

3 个答案:

答案 0 :(得分:1)

$ scope.bd1 [billProductList[${index}].${key}] =值;您正在覆盖此代码行中的值

  

$ scope.bd1 [billProductList[${index}${index2}].${key}],您可以根据需要更改对象的形状

$scope.fproducts.forEach((total, index) => {
    Object.entries(total).forEach(([key, value],index2) => {
        console.log(index + "  " + key + " " + value);
        $scope.bd1[`billProductList[${index}${index2}].${key}`] = value;
    });
})

答案 1 :(得分:1)

考虑使用Struts JSON Plugin

JSON插件提供了将结果序列化为JSON的json结果类型。

  1. content-type必须为application/json
  2. 对于必须填充的字段,操作必须具有公共的“ setter”方法。
  3. 支持的人口类型为:Primitives (int,long…String)DateListMapPrimitive Arrays,其他类别和Array {1}}类。
  4. 要在列表或映射中填充的JSON中的任何对象都将是Other类型(从属性映射到值),任何整数都将是Map类型,则任何十进制数字将为Long类型,而任何数组将为Double类型。

如果您的操作在使用JSON拦截器的程序包中或通过添加对它的引用,则可以接受传入的JSON

这简化了AngularJS HTTP POST请求:

List

答案 2 :(得分:0)

这可能是因为console.log(index + " " + key + " " + value)在进入内部循环时正在返回正确的值。

在分配给$scope.bd1之后,尝试访问以下代码:

Object.entries(total).forEach(([key, value]) => {
  $scope.bd1[`billProductList[${index}].${key}`] = value;
  console.log($scope.bd1[`billProductList[${index}].${key}`]);
});

我猜$scope.bd1尚未更新,因此没有将值传递给服务器端对象。