Java 如何将字符串数组从html输入传递到http Post?
我想将一些文本输入作为数组字符串传递给angular controller。我可以使用Java 如何将字符串数组从html输入传递到http Post?,java,angularjs,servlets,post,Java,Angularjs,Servlets,Post,我想将一些文本输入作为数组字符串传递给angular controller。我可以使用String key=request.getParameter(“key”)将单个输入作为POST参数发送,并在Serlvet中获取它 这是我的表格 <form ng-controller="FormController" ng-submit="submitForm()" class="ng-valid ng-scope ng-dirty ng-valid-parse"> &
String key=request.getParameter(“key”)将单个输入作为POST参数发送,并在Serlvet中获取它代码>
这是我的表格
<form ng-controller="FormController" ng-submit="submitForm()" class="ng-valid ng-scope ng-dirty ng-valid-parse">
<p>Text1: <input type="text" name="ancestor" ng-model="blob.ancestor" class="ng-valid ng-dirty ng-touched ng-empty"></p>
<p>Text2: <input type="text" name="ancestor" ng-model="blob.ancestor" class="ng-valid ng-dirty ng-valid-parse ng-empty ng-touched"></p>
<p><input type="submit" class="btn btn-primary" value="Confirm"></p>
</form>
同样,我可以发送单个参数,但我希望发送带有多个值的“祖先”,并使用String祖先[]=reuest.getParameterValues(“祖先”)获取它我在servlet上的Post方法中使用了code>。让我们假设,从您的后端,您将获得祖先作为数组的值[1,2,3,4
]。您可以像这样使用ng repeat
将此值绑定到输入字段
在控制器中:
$scope.blob = {};
$scope.blob.ancestor = [1, 2, 3, 4];
在HTML中:
<form ng-submit="submitForm()" class="ng-valid ng-scope ng-dirty ng-valid-parse">
<p ng-repeat="ancestor in blob.ancestor">Text {{$index +1}}: <input type="text" name="ancestor" ng-model="ancestor" class="ng-valid ng-dirty ng-touched ng-empty"></p>
<p><input type="submit" class="btn btn-primary" value="Confirm"></p>
</form>
数据将进入参数,url如下所示:
http://localhost:8080?ancestor=1&ancestor=2&ancestor=3&ancestor=4
谢谢!这对我有用。顺便说一句,我使用的是'.then(函数successCallback(response){}',而不是'.success(){}',对于Angular版本>1.4.x,它是不推荐使用的
$scope.submitForm = function() {
console.log($scope.blob); // result:{"ancestor":[1,2,3,4]}
$http({
url: 'http://localhost:8080',
method: "POST",
params: {
ancestor: $scope.blob.ancestor
}
}).success(function(data) {
console.log(data);
}).error(function(data) {
console.log(data);
});
}
http://localhost:8080?ancestor=1&ancestor=2&ancestor=3&ancestor=4