如何使用REST将数据从AngularJS发布到Struts 2 [英] How to POST data from AngularJS to Struts 2 using REST

查看:100
本文介绍了如何使用REST将数据从AngularJS发布到Struts 2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从客户端到服务器端获取值.我正在使用AngularJS和Struts2 REST.

I need to get the value from client side to server side. I am using AngularJS and Struts2 REST.

我的控制器没有传递值,或者在传递或使用RESTful控制器时我错了吗?

My controller doesn't get the value passed or am I wrong in passing or using RESTful controller?

这是我的代码:

angularcontroller.js

app.controller('saveAddCatCtrl', function($scope, $http){
    $scope.save = function(newAddCat){
        $http({
            method: "POST",
            url: "api/additionalcategory",
            data: $scope.additionalCategory.addCatName
        }).success(function(data, status, header, config){
            //success
        }).error(function(data, status, header, config){
            //error
        });
}
});

这是我的REST控制器:

public class AdditionalcategoryController extends ActionSupport implements ModelDriven<Object>{

private AdditionalCategoryRepository additionalCategoryRepository =
        (AdditionalCategoryRepository)ServletActionContext.getServletContext()
        .getAttribute("additionalCategoryRepository");

private List<AdditionalCategory>            additionalCategories;
public AdditionalCategory                   additionalCategory = new AdditionalCategory();
private int                                 id;

public HttpHeaders index(){
    setAdditionalCategories(additionalCategoryRepository.findAllAdditionalCategory());
    System.out.println("GET api/additionalCategory");
    return new DefaultHttpHeaders("index").disableCaching();
}

public HttpHeaders create(){
    additionalCategoryRepository.createAdditionalCategory(additionalCategory);
    return new DefaultHttpHeaders("create").disableCaching();
}

public void setAdditionalCategories(List<AdditionalCategory> additionalCategories){
    this.additionalCategories = additionalCategories;
}

@Override
public Object getModel() {
    if (additionalCategories == null){
        return additionalCategory;
    }else{
        return additionalCategories;
    }
}

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public List<AdditionalCategory> getAdditionalCategories() {
    return additionalCategories;
}

public AdditionalCategory getAdditionalCategory() {
    return additionalCategory;
}

public void setAdditionalCategory(AdditionalCategory additionalCategory) {
    this.additionalCategory = additionalCategory;
}

}

推荐答案

由于角度服务 http 发送JSON,因此您可以使用 data 属性发送JSON对象.JSON对象将反序列化为模型类属性,因为否则使用的是 ModelDriven Action 类属性.

Since angular service http sends JSON, you can send a JSON object using data property. The JSON object will be deserialized to your model class properties because you are using ModelDriven or Action class properties otherwise.

Struts2 REST插件用于处理请求中的JSON内容.可能由于 ContentTypeInterceptor (称为 rest )能够找到请求的JSON处理程序.可以从 Content-Type 标头或 .json 扩展名到URL中的文件来猜测JSON处理程序.您可以使用任何一种方法,但是第二种方法更容易.

Struts2 REST plugin is made to handle JSON content from the request. It's possible due ContentTypeInterceptor, which is called rest, be able to find JSON handler for the request. The JSON handler is guessed from the Content-Type header or .json extension to the file in the URL. You can use either ways, but second is easier.

注意:

没有文件扩展名的URL,Struts2 REST插件默认处理 xhtml 内容.这样您就可以修改代码

URLs without a file extension the Struts2 REST plugin defaulting to handle xhtml content. So you can modify your code

$http({
    method: "POST",
    url: "api/additionalcategory.json",
    data: {}
}).success(function(data, status, header, config){
    //success
}).error(function(data, status, header, config){
    //error
});

这篇关于如何使用REST将数据从AngularJS发布到Struts 2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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