如何使用AngularJS表单将JSON发送到Struts2 Action? [英] How to send JSON to Struts2 Action with AngularJS form?

查看:111
本文介绍了如何使用AngularJS表单将JSON发送到Struts2 Action?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用AngularJS表单将JSON发送到Struts2 Action?

How to send JSON to Struts2 Action with AngularJS form?

我的代码:

我的struts。 xml

My struts.xml

<package name="default" extends="json-default">

    <interceptors>
        <interceptor-stack name="defaultStack">
            <interceptor-ref name="json">
                <param name="enableSMD">true</param>
            </interceptor-ref>
        </interceptor-stack>
    </interceptors>

    <default-interceptor-ref name="defaultStack" />

    <action name="angularAction" class="actions.CustomerAction" method="findCustomer" >
        <result type="json">
              <param name="root">customer</param>
              <param name="excludeNullProperties">true</param>
              <param name="noCache">true</param>
        </result>
    </action>

</package>

我的Java类:

public class Customer {

    private String id;
    private String name;
    private String email;
    private String phone;

    public Customer() {

    }

    public Customer(String id, String name, String email, String phone) {

        this.id = id;
        this.name = name;
        this.email = email;
        this.phone = phone;

    }

    //getters and setters omitted
}

My Struts2 Action

My Struts2 Action

public class ClienteAction extends ActionSupport {

    private List<Customer> jsonCustomer;

    public List<Customer> getJsonCustomer() {
        return jsonCustomer;
    }


    public void setJsonCustomer(List<Customer> jsonCustomer) {
        this.jsonCustomer = jsonCustomer;
    }

    public String findCustomer() {

        List<Customer> jc = getJsonCustomer();

        return SUCCESS;

    }

}

My Angular Code :

My Angular Code:

angular.module('myApp', []);

angular.module('myApp').
controller('MyController',  function ($scope, $http) {
    $scope.getDataFromServer = function(customer) {

        var data1 = JSON.stringify(customer);

            $http({
                url: 'angularAction?jasonCustomer=' + data1,
                method: 'GET'
            }).then (function successCallback(response) {
                $scope.customer = response.data;                    

            }, function errorCallback(response) {
                console.log(response.data) ;
            })      

        };
    });

My Libs

它不起作用! angularAction中的变量jsonCustomer为null。

It doesn't work! The variable jsonCustomer in angularAction is null.

我是angular的新手。我正在使用本教程,但是角度代替jquery。

I'm newbie with angular. I'm using this tutorial, but angular instead jquery.

http://tech.learnerandtutor.com/send-json-object-to-struts-2-action-by-jquery-ajax/

感谢您的帮助!

推荐答案

首先你需要改变你的angularJS,这里是下面的变化:

First of all you need to change in your angularJS and here is the change given below :

angular.module('myApp', []);

angular.module('myApp').
controller('MyController',  function ($scope, $http) {
    $scope.getDataFromServer = function(customer) {

        var data1 = JSON.stringify(customer);

            $http({
                url: 'angularAction',
                method: 'POST',
                'jasonCustomer' : data1
            }).then (function successCallback(response) {
                $scope.customer = response.data;                    

            }, function errorCallback(response) {
                console.log(response.data) ;
            })      

        };
    });

现在 angularAction 获取参数 jasonCustomer 最后我们在动作类中获取json数据:

Now angularAction get JSON data with parameter jasonCustomer and finally we get json data in our action class :

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.IOUtils;
import org.apache.struts2.ServletActionContext;

import com.blogspot.geekonjava.db.ProductDB;
import com.blogspot.geekonjava.gs.Product;
import com.opensymphony.xwork2.Action;

public class CustomerAction{
 static HttpServletRequest request ;


   public static String findCustomer()
   {
    try {
     request  =  ServletActionContext.getRequest();
     String filename = IOUtils.toString(request.getInputStream());
     System.out.println("jsonData "+filename);
    // Here you can use your code



    } catch (IOException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    } 
    return "success";
   }



   public String execute() {
     return Action.SUCCESS;
       }

}

为了您的帮助或参考,您可以关注:此链接

For your help or refrences you can follow : this link

这篇关于如何使用AngularJS表单将JSON发送到Struts2 Action?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆