我使用 jersy 作为 RESTful web 服务和 angularJS 来同时发布所有的 todo 对象使用你的 person 对象同时更新数据
//Define an angular module for our app
var sampleApp = angular.module('sampleApp', ['ngRoute','ngResource']);
sampleApp.config(['$routeProvider',
function($routeProvider) {
$routeProvider.
when('/AddNewOrder', {
templateUrl: 'add_order.jsp',
controller: 'AddOrderController'
}).
when('/TestArray', {
templateUrl: 'list.html',
controller: 'TestArrayController'
}).
}).
otherwise({
redirectTo: '/AddNewOrder'
});
}]);
sampleApp.controller( 'TestArrayController', function ( $scope, $location,$http,$resource) {
var array=new Array();
var postObject1 = new Object();
postObject1.description = "testDesc1";
postObject1.summary = "testSummary1";
array.push(postObject1);
var postObject2 = new Object();
postObject2.description = "testDesc2";
postObject2.summary = "testSummary2";
array.push(postObject2);
$http.post("/AngularJS/JerseyWebService/todo/arrayTest", array).success(function(data){
//Callback function here.
//"data" is the response from the server.
$scope.updateMessage=data;
});
网页
<html lang="en">
<head>
<title>AngularJS Routing example</title>
<script src="jquery.min.js" type="text/javascript" language="javascript"></script>
<script src="xml2json.js" type="text/javascript" language="javascript"></script>
<script src="angular.js"></script>
<script src="angularjs/angular-route.js"></script>
<script src="angularjs/angular-resource.js"></script>
<script src="app1.js"></script>
<script type="text/javascript">
var jquery = $.noConflict();
</script>
</head>
<body ng-app="sampleApp">
<div class="container">
<div class="row">
<div class="col-md-3">
<ul class="nav">
<!--<li><a href="#AddNewOrder"> Add New Order </a></li>
<li><a href="#ShowOrders"> Show Orders</a></li>-->
<li><a href="#TestArray">Test Array Update</a></li>
</ul>
<div>{{updateMessage}}</div>
</div>
<div class="col-md-9">
<div ng-view></div>
</div>
</div>
</div>
</body>
</html>
杰西网络服务
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.ws.rs.Consumes;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
@Path("/todo")
public class TodoResource {
@POST
@Path("/arrayTest")
@Consumes({ MediaType.APPLICATION_JSON })
@Produces({ MediaType.APPLICATION_JSON })
public String wantsJSONArray(List<Todo> array)
{
System.out.println("---------------------------------------------------"+array);
// here's your array
return "success";
}
}
Todo.java
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
// JAX-RS supports an automatic mapping from JAXB annotated class to XML and JSON
// Isn't that cool?
public class Todo {
private String summary;
private String description;
public String getSummary() {
return summary;
}
public void setSummary(String summary) {
this.summary = summary;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String toString(){
return "{summary:"+summary+"description:"+description+"}";
}
}
web.xml 条目
<servlet>
<servlet-name>JerseyWebService</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>JerseyWebService</servlet-name>
<url-pattern>/JerseyWebService/*</url-pattern>
</servlet-mapping>