通过角度.js将参数从jsp传递到servlet

时间:2015-05-11 07:30:45

标签: angularjs jsp servlets

我有一个像这样的jsp页面:

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Spring MVC AngularJS demo</title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.8/angular.min.js"></script>
<script>

    var app = angular.module('myApp', []);

    function MyController($scope, $http){
    //  $scope.user = {};
        $scope.getPersonDataFromServer = function() {           
            $http({
            //SSmethod: 'GET', 
            url: '/Fetch',
            headers: {'Content-Type': 'application/json'},
                    data:  $scope.user
            }).
            success(function(data, status, headers, config) {
                $scope.person = data;
            }).
            error(function(data, status, headers, config) {
              // called asynchronously if an error occurs
              // or server returns response with an error status.
            });

        };
    };
</script>
</head>
<body>
    <div data-ng-app="myApp">
        <div data-ng-controller="MyController">
            <button data-ng-click="getPersonDataFromServer()">Get Person data from server</button>
            <p>First Name : {{person.firstName}}</p>
            <input type="text" id="name" name="name" ng-model="user.name" placeholder="User Name"> 
            <p>Last Name : {{person.lastName}}</p>
             <input type="text" id="pwd" name="pwd" ng-model="user.pwd" placeholder="Your Password">
        </div>
    </div>
</body>
</html>

和控制器代码如下:

StringBuffer sb = new StringBuffer();
System.out.println("raju");
try 
{
  BufferedReader  reader = request.getReader();
  String line = null;
  System.out.println(reader);
  while ((line = reader.readLine()) != null)
  {
 System.out.println(line+"raju");
    sb.append(line);
  }
} catch (Exception e) { e.printStackTrace(); }

并且这工作正常,如果我填写并提交,我将获得json对象。 问题是我有价值循环:

<%for(int i=0;i<10;i++){%>
 <button data-ng-click="getPersonDataFromServer()">Get Person data from server</button>
            <p>First Name : {{person.firstName}}</p>
            <input type="text" id="name<%=i%>" name="name<%=i%>" ng-model="user.name" placeholder="User Name" value="<%=i%>"> 
%>

如果我运行它并单击按钮 - 只有servlet正在获取最后一个值。 现在我想:当我点击按钮时,我必须在servlet中获取其文本值,并且只应提交一个值,我无法设置动态值。请在breif中解释我,我是Angular JS的新手

请帮助我。

提前谢谢。

0 个答案:

没有答案