I am using sping3 mvc in my application.
I have some entities which need the CRUD operation.
When query the record,the pagination may be needed,so I create the class Pagination:
class Pagination{
int page,
int pageSize,
String sort;
}
This is my Dao:
public List<Post> list(Pagination pagination) {
/////
return xxx;
}
In controller:
@RequestMapping(value = "/", method = RequestMethod.GET)
public String list(@RequestParam int page,@RequestParam int pageSize,@RequestParam String sort) {
List<Post> posts=postDao.list(new Pagination(page,pageSize,sort));
return "";
}
Now,if there no page or pageSize parameter in the httprequest,it will throw the typeconvert exception,in fact,I know I can redirect to the exception page but I do not want this. I want to use some default configuation in this case,for example:
http://xx/post/list ==>return all records without pagination
http://xx/post/list?page=1&pageSize=10&sort=id ==>pagination
http://xx/post/list?page=1 ==>pagination with the default pageSize(maybe 10) and sort(id)
http://xx/post/list?page=notnumber&pageSize=10 ==>pagination with page=1
Then I want to know where and how to validate and reset the Pagination parameter?
You can change your parameter datatype of int page and pageSize to String page and String pageSize, and after doing this you can just check whether the passed parameter values is not null. If parameter value is not null use passed value otherwise use default value.
like this