Created
July 4, 2011 00:02
-
-
Save esfand/1062729 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Controller | |
@RequestMapping(value = "/book") | |
public class BookController { | |
private final Map<Integer, Book> books = new ConcurrentSkipListMap<Integer, Book>(); | |
@RequestMapping(value = "/{id}", method = GET) | |
public @ResponseBody Book read(@PathVariable("id") int id) { | |
return books.get(id); | |
} | |
@RequestMapping(method = GET) | |
public @ResponseBody Page<Book> listBooks( | |
@RequestParam(value = "page", required = false, defaultValue = "1") int page, | |
@RequestParam(value = "max", required = false, defaultValue = "20") int max) { | |
final ArrayList<Book> booksList = new ArrayList<Book>(books.values()); | |
final int startIdx = (page - 1) * max; | |
final int endIdx = Math.min(startIdx + max, books.size()); | |
return new Page<Book>(booksList.subList(startIdx, endIdx), page, max, books.size()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Controller | |
@RequestMapping(value = "/book") | |
public class BookController { | |
private final Map<Integer, Book> books = new ConcurrentSkipListMap<Integer, Book>(); | |
@RequestMapping(value = "/{id}", method = GET) | |
public @ResponseBody Book read(@PathVariable("id") int id) { | |
//... | |
} | |
@RequestMapping(method = GET) | |
public | |
@ResponseBody | |
Page<Book> listBooks( | |
@RequestParam(value = "page", required = false, defaultValue = "1") int page, | |
@RequestParam(value = "max", required = false, defaultValue = "20") int max) { | |
//... | |
} | |
@RequestMapping(value = "/{id}", method = PUT) | |
@ResponseStatus(HttpStatus.NO_CONTENT) | |
public void updateBook(@PathVariable("id") int id, @RequestBody Book book) { | |
//... | |
} | |
@RequestMapping(method = POST) | |
public ResponseEntity<String> createBook(HttpServletRequest request, @RequestBody Book book) { | |
//... | |
} | |
@RequestMapping(value = "/{id}", method = DELETE) | |
@ResponseStatus(HttpStatus.NO_CONTENT) | |
public void deleteBook(@PathVariable("id") int id) { | |
//... | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<table id="grid"></table> | |
<div id="pager"></div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$.extend($.jgrid.defaults, { | |
datatype: 'json', | |
jsonReader : { | |
repeatitems:false, | |
total: function(result) { | |
//Total number of pages | |
return Math.ceil(result.total / result.max); | |
}, | |
records: function(result) { | |
//Total number of records | |
return result.total; | |
} | |
}, | |
prmNames: {rows: 'max', search: null}, | |
height: 'auto', | |
viewrecords: true, | |
rowList: [10, 20, 50, 100], | |
altRows: true, | |
loadError: function(xhr, status, error) { | |
alert(error); | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var URL = 'rest/book'; | |
var options = { | |
url: URL, | |
editurl: URL, | |
colModel:[ | |
{ | |
name:'id', label: 'ID', | |
formatter:'integer', | |
width: 40, | |
editable: true, | |
editoptions: {disabled: true, size:5} | |
}, | |
{ | |
name:'title', | |
label: 'Title', | |
width: 300, | |
editable: true, | |
editrules: {required: true} | |
}, | |
{ | |
name:'author', | |
label: 'Author', | |
width: 200, | |
editable: true, | |
editrules: {required: true} | |
}, | |
{ | |
name:'cover', | |
label: 'Cover', | |
hidden: true, | |
editable: true, | |
edittype: 'select', | |
editrules: {edithidden:true}, | |
editoptions: { | |
value: {'PAPERBACK': 'paperback', 'HARDCOVER': 'hardcover', 'DUST_JACKET': 'dust jacket'} | |
} | |
}, | |
{ | |
name:'publishedYear', | |
label: 'Published year', | |
width: 80, | |
align: 'center', | |
editable: true, | |
editrules: {required: true, integer: true}, | |
editoptions: {size:5, maxlength: 4} | |
}, | |
{ | |
name:'available', | |
label: 'Available', | |
formatter: 'checkbox', | |
width: 46, | |
align: 'center', | |
editable: true, | |
edittype: 'checkbox', | |
editoptions: {value:"true:false"} | |
}, | |
{ | |
name:'comments', | |
label: 'Comments', | |
hidden: true, | |
editable: true, | |
edittype: 'textarea', | |
editrules: {edithidden:true} | |
} | |
], | |
caption: "Books", | |
pager : '#pager', | |
height: 'auto' | |
}; | |
$("#grid") | |
.jqGrid(options) | |
.navGrid('#pager', {edit:true,add:true,del:true, search: false}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$("#grid") | |
.jqGrid({ | |
url:'rest/book', | |
colModel:[ | |
{name:'id', label: 'ID', formatter:'integer', width: 40}, | |
{name:'title', label: 'Title', width: 300}, | |
{name:'author', label: 'Author', width: 200}, | |
{name:'publishedYear', label: 'Published year', width: 80, align: 'center'}, | |
{name:'available', label: 'Available', formatter: 'checkbox', width: 46, align: 'center'} | |
], | |
caption: "Books", | |
pager : '#pager', | |
height: 'auto' | |
}) | |
.navGrid('#pager', {edit:false,add:false,del:false, search: false}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@XmlRootElement | |
public class Page<T> { | |
private List<T> rows; | |
private int page; | |
private int max; | |
private int total; | |
//... | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Content-Type: application/x-www-form-urlencoded in the following format: | |
id=&title=And+Then+There+Were+None&author=Agatha+Christie&cover=PAPERBACK&publishedYear=1939&available=true&comments=&oper=add |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// $ curl -v -H "Accept: application/json" "http://localhost:8080/books/rest/book?page=1&max=3" | |
{ | |
"total":43, | |
"max":3, | |
"page":1, | |
"rows":[ | |
{ | |
"id":1, | |
"available":true, | |
"author":"Charles Dickens", | |
"title":"A Tale of Two Cities", | |
"publishedYear":1859, | |
"cover":"PAPERBACK", | |
"comments":null | |
}, | |
{ | |
"id":2, | |
"available":true, | |
"author":"J. R. R. Tolkien", | |
"title":"The Lord of the Rings", | |
"publishedYear":1954, | |
"cover":"HARDCOVER", | |
"comments":null | |
}, | |
{ | |
"id":3, | |
"available":true, | |
"author":"J. R. R. Tolkien", | |
"title":"The Hobbit", | |
"publishedYear":1937, | |
"cover":"PAPERBACK", | |
"comments":null | |
} | |
] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- $ curl -v "http://localhost:8080/books/rest/book?page=1&max=2" --> | |
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> | |
<page> | |
<max>3</max> | |
<page>1</page> | |
<rows xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="book"> | |
<author>Charles Dickens</author> | |
<available>true</available> | |
<cover>PAPERBACK</cover> | |
<id>1</id> | |
<publishedYear>1859</publishedYear> | |
<title>A Tale of Two Cities</title> | |
</rows> | |
<rows xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="book"> | |
<author>J. R. R. Tolkien</author> | |
<available>true</available> | |
<cover>HARDCOVER</cover> | |
<id>2</id> | |
<publishedYear>1954</publishedYear> | |
<title>The Lord of the Rings</title> | |
</rows> | |
<rows xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="book"> | |
<author>J. R. R. Tolkien</author> | |
<available>true</available> | |
<cover>PAPERBACK</cover> | |
<id>3</id> | |
<publishedYear>1937</publishedYear> | |
<title>The Hobbit</title> | |
</rows> | |
<total>43</total> | |
</page> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment