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

时间:2015-10-26 09:58:33

标签: javascript json angularjs struts2

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

我的代码:

我的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
}

我的Struts2动作

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:

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。

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

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

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

首先,您需要更改angularJS,以下是给出的更改:

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数据,最后我们在动作类中获取json数据:

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;
       }

}

为了您的帮助或参考,您可以关注:this link