Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/390.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 未使用角Post函数执行操作_Javascript_Angularjs - Fatal编程技术网

Javascript 未使用角Post函数执行操作

Javascript 未使用角Post函数执行操作,javascript,angularjs,Javascript,Angularjs,在这里,我将Angular与struts结合使用,我是Angular的新手 我有一个控制器(controller.js),其中我使用post方法调用action类(CartAction) 从controller.js中的post调用action/StrutsAngular/ControllerAction.do时,我没有发现任何错误 但是action类没有执行,甚至system.out.println也没有执行。 当我在成功功能中添加警报时,我能够获得输入页面。 当我给出错误的路径时,我进入了错

在这里,我将Angular与struts结合使用,我是Angular的新手

我有一个控制器(controller.js),其中我使用post方法调用action类(CartAction

从controller.js中的post调用action/StrutsAngular/ControllerAction.do时,我没有发现任何错误

但是action类没有执行,甚至system.out.println也没有执行。 当我在成功功能中添加警报时,我能够获得输入页面。 当我给出错误的路径时,我进入了错误函数

我不明白为什么不采取行动

我还有另一个澄清,如果在本例中-->如果调用该操作,我可以从中获取响应数据

前面我使用了jQuery,还提供了我使用的示例代码。 我们如何使用AngularJS以类似的方式完成它

请帮助我,让我知道的情况下,进一步的信息

提前感谢您的回答

Controller.js

function Controller($scope,$http) {

 $http.post('/StrutsAngular/ControllerAction.do').
        success(function(data, status, headers, config) {
        // this callback will be called asynchronously
        // when the response is available
        alert("Success :: "+data);
         }).
    error(function(data, status, headers, config) {
        // called asynchronously if an error occurs
        // or server returns response with an error status.
        alert("Error :: "+data);
    });


}
public class CartAction extends org.apache.struts.action.Action {


    /**
     * This is the action called from the Struts framework.
     *
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        System.out.println("Entering Shop Cart Action");

    List<Object> objectList= new ArrayList<>(); 

    CartDto cartDto = new ShopingCartDto();
        cartDto.setSno(1);
        cartDto.setTitle("Title One");

    objectList.add(cartDto);


        response.setHeader("cache-control","no-cache");
        response.setContentType("text/json");
        PrintWriter out = response.getWriter();

    JSONArray jsonArray = JSONArray.fromObject(objectList);
        out.println(jsonArray.toString());

        return null;
    }


}
行动类

function Controller($scope,$http) {

 $http.post('/StrutsAngular/ControllerAction.do').
        success(function(data, status, headers, config) {
        // this callback will be called asynchronously
        // when the response is available
        alert("Success :: "+data);
         }).
    error(function(data, status, headers, config) {
        // called asynchronously if an error occurs
        // or server returns response with an error status.
        alert("Error :: "+data);
    });


}
public class CartAction extends org.apache.struts.action.Action {


    /**
     * This is the action called from the Struts framework.
     *
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        System.out.println("Entering Shop Cart Action");

    List<Object> objectList= new ArrayList<>(); 

    CartDto cartDto = new ShopingCartDto();
        cartDto.setSno(1);
        cartDto.setTitle("Title One");

    objectList.add(cartDto);


        response.setHeader("cache-control","no-cache");
        response.setContentType("text/json");
        PrintWriter out = response.getWriter();

    JSONArray jsonArray = JSONArray.fromObject(objectList);
        out.println(jsonArray.toString());

        return null;
    }


}
JSP

<%-- 
    Document   : Cart
    Created on : 25 Mar, 2014, 5:14:42 PM
    Author     : Arun
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html ng-app>
    <head>
        <title>Cart</title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta name="description" content="">
        <meta name="author" content="">
        <script src="js/lib/angularjs-1.0.2/angular.js"></script>
        <script src="js/lib/controllers/controllers.js"></script>
        <link href="js/lib/bootstrap/css/bootstrap.css" rel="stylesheet">
    </head>
    <body ng-controller='Controller'>

       <div class="container">


           <form name="shoppingForm" id="formId">

                <table class="table table-striped table-hover">
                    <caption></caption>
                    <thead>
                       <tr>
                        <th>S No</th>
                        <th>Item</th>
                       </tr> 
                    </thead>

                    <tr ng-repeat='item in items | orderBy:title | filter:search' >
                         <td ><input ng-model='item.sno' class="form-control" size="3" placeholder="Quantity" maxlength="3" required></td>
                        <td ><input ng-model='item.title' class="form-control" size="10" maxlength="10" placeholder="Item Name" required></td>
                    </tr>
                </table>
                <div><button class="btn btn-lg btn-success btn-block">Submit</button></div>
           </form>

       </div>
    </body>
</html>

运货马车
没有
项目
提交

好的,您有一些问题,其中大多数似乎是对角度如何工作的误解。有一篇关于jQuery背景下的“角度思考”的文章非常棒:

要根据到目前为止的代码为您提供一些详细信息:

首先,您需要创建一个应用程序(angular.module),为其分配ng app属性。例如:

 var myApp = angular.module('myApp',[]); // this creates a new angular module named "myApp";

 <html ng-app="myApp"> // this will bootstrap your myApp module at the html DOM element once the domIsReady event fires
var myApp=angular.module('myApp',[]);//这将创建一个名为“myApp”的新角度模块;
//一旦触发DomsReady事件,这将在html DOM元素引导myApp模块
其次,需要在模块上使用Angular定义控制器,并传递函数。有一个角度的方法,但我建议在尝试之前用标准的方法

myApp.controller('myCtrl', function ($scope,$http) {

 $http.post('/StrutsAngular/ControllerAction.do').
        success(function(data, status, headers, config) {
        // this callback will be called asynchronously
        // when the response is available
        alert("Success :: "+data);
         }).
    error(function(data, status, headers, config) {
        // called asynchronously if an error occurs
        // or server returns response with an error status.
        alert("Error :: "+data);
    });
});

<div ng-controller="myCtrl">
myApp.controller('myCtrl',函数($scope,$http){
$http.post('/StrutsAngular/ControllerAction.do')。
成功(函数(数据、状态、标题、配置){
//将异步调用此回调
//当响应可用时
警报(“成功::”+数据);
}).
错误(函数(数据、状态、标题、配置){
//发生错误时异步调用
//或服务器返回带有错误状态的响应。
警报(“错误::”+数据);
});
});
最后,传递给controller()的函数只会被调用一次,并且只有在它被用于某个地方之后才会被调用。因此,在本例中,绑定到DIV将在加载DOM时产生一个$http.post权限。它不会做任何其他事情。我想这只是你在测试它到了那里。对于“真实”代码,您将通过$scope公开某些函数,并从视图中调用它们:

myApp.controller('myCtrl', function ($scope,$http) {
   $scope.sendData = function() {
     $http.post(.....your other code....);
   }
});

<div ng-controller="myCtrl"><a ng-click="sendData()">send data</a></div>
myApp.controller('myCtrl',函数($scope,$http){
$scope.sendData=函数(){
$http.post(……您的其他代码……);
}
});
发送数据

您还可以分享绑定控制器和引导ng应用程序的HTML吗?@Mattpillegi我添加了前端jsp供您参考,如果需要更多信息,请告诉我现在我了解了如何开始,非常感谢,按照建议,我创建了该服务,并尝试了初始加载和单击两种方式,但现在仍然无法调用Action类。。如果有进一步的信息,请告诉我……我在struts-config.xml中发现了问题,这里的tag validate中的问题应该是no