22
33import org .slf4j .Logger ;
44import org .slf4j .LoggerFactory ;
5+ import org .springframework .data .domain .Page ;
6+ import org .springframework .data .domain .PageRequest ;
7+ import org .springframework .data .domain .Pageable ;
8+ import org .springframework .data .domain .Sort ;
59import org .springframework .stereotype .Controller ;
610import org .springframework .ui .Model ;
711import org .springframework .validation .BindingResult ;
812import org .springframework .web .bind .annotation .PathVariable ;
913import org .springframework .web .bind .annotation .RequestMapping ;
1014import org .springframework .web .bind .annotation .RequestMethod ;
15+ import org .springframework .web .bind .annotation .RequestParam ;
1116import org .woehlke .greenshop .admin .AdminMenuCategory ;
1217import org .woehlke .greenshop .customer .service .AddressFormatService ;
1318import org .woehlke .greenshop .customer .service .CountryService ;
@@ -32,36 +37,44 @@ public class CountryController {
3237 @ Inject
3338 private AddressFormatService addressFormatService ;
3439
40+ private final static int PAGE_SIZE = 20 ;
41+
42+ private final static String FIRST_PAGE = "0" ;
43+
3544 @ RequestMapping (value = "/admin/countries" , method = RequestMethod .GET )
36- public String countries (Model model ){
45+ public String countries (@ RequestParam ( value = "page" , defaultValue = FIRST_PAGE ) int page , Model model ){
3746 int menuCategory = AdminMenuCategory .LOCATION_TAXES .ordinal ();
3847 model .addAttribute ("menuCategory" ,menuCategory );
39- List <Country > countries = countryService .findAllCountriesOrderByName ();
48+ Pageable pageRequest = new PageRequest (page ,PAGE_SIZE , Sort .Direction .ASC , "name" );
49+ Page <Country > countries = countryService .findAllCountriesOrderByName (pageRequest );
4050 model .addAttribute ("countries" ,countries );
4151 Country thisCountry = null ;
42- if (countries .size ()>0 ){
52+ if (countries .getContent (). size ()>0 ){
4353 thisCountry = countries .iterator ().next ();
4454 }
4555 model .addAttribute ("thisCountry" ,thisCountry );
4656 return "admin/countries" ;
4757 }
4858
4959 @ RequestMapping (value = "/admin/countries/{countryId}" , method = RequestMethod .GET )
50- public String countries (@ PathVariable long countryId , Model model ){
60+ public String countries ( @ RequestParam (value ="page" ,defaultValue =FIRST_PAGE ) int page ,
61+ @ PathVariable long countryId , Model model ){
5162 int menuCategory = AdminMenuCategory .LOCATION_TAXES .ordinal ();
5263 model .addAttribute ("menuCategory" ,menuCategory );
5364 Country thisCountry = countryService .findCountryById (countryId );
5465 model .addAttribute ("thisCountry" ,thisCountry );
55- List <Country > countries = countryService .findAllCountriesOrderByName ();
66+ Pageable pageRequest = new PageRequest (page ,PAGE_SIZE , Sort .Direction .ASC , "name" );
67+ Page <Country > countries = countryService .findAllCountriesOrderByName (pageRequest );
5668 model .addAttribute ("countries" ,countries );
5769 return "admin/countries" ;
5870 }
5971
6072 @ RequestMapping (value = "/admin/countries/insert" , method = RequestMethod .GET )
61- public String countriesInsertForm (Model model ){
73+ public String countriesInsertForm (@ RequestParam ( value = "page" , defaultValue = FIRST_PAGE ) int page , Model model ){
6274 int menuCategory = AdminMenuCategory .LOCATION_TAXES .ordinal ();
6375 model .addAttribute ("menuCategory" ,menuCategory );
64- List <Country > countries = countryService .findAllCountriesOrderByName ();
76+ Pageable pageRequest = new PageRequest (page ,PAGE_SIZE , Sort .Direction .ASC , "name" );
77+ Page <Country > countries = countryService .findAllCountriesOrderByName (pageRequest );
6578 model .addAttribute ("countries" ,countries );
6679 Country thisCountry = new Country ();
6780 model .addAttribute ("thisCountry" ,thisCountry );
@@ -71,43 +84,51 @@ public String countriesInsertForm(Model model){
7184 }
7285
7386 @ RequestMapping (value = "/admin/countries/insert" , method = RequestMethod .POST )
74- public String countriesInsertPerform (@ Valid Country thisCountry , BindingResult result , Model model ){
87+ public String countriesInsertPerform (@ RequestParam (value ="page" ,defaultValue =FIRST_PAGE ) int page ,
88+ @ Valid Country thisCountry , BindingResult result , Model model ){
7589 logger .info ("Country: " +thisCountry .toString ());
7690 if (result .hasErrors ()){
7791 int menuCategory = AdminMenuCategory .LOCATION_TAXES .ordinal ();
7892 model .addAttribute ("menuCategory" ,menuCategory );
79- List <Country > countries = countryService .findAllCountriesOrderByName ();
93+ Pageable pageRequest = new PageRequest (page ,PAGE_SIZE , Sort .Direction .ASC , "name" );
94+ Page <Country > countries = countryService .findAllCountriesOrderByName (pageRequest );
8095 model .addAttribute ("countries" ,countries );
8196 model .addAttribute ("thisCountry" ,thisCountry );
8297 List <AddressFormat > addressFormats = addressFormatService .findAllAddressFormat ();
8398 model .addAttribute ("addressFormats" ,addressFormats );
8499 return "admin/countriesInsertForm" ;
85100 } else {
86101 countryService .createCountry (thisCountry );
87- return "redirect:/admin/countries/" +thisCountry .getId ();
102+ return "redirect:/admin/countries/" +thisCountry .getId ()+ "?page=" + page ;
88103 }
89104 }
90105
91106 @ RequestMapping (value = "/admin/countries/{countryId}/edit" , method = RequestMethod .GET )
92- public String countriesEditForm (@ PathVariable long countryId , Model model ){
107+ public String countriesEditForm (@ PathVariable long countryId ,
108+ @ RequestParam (value ="page" ,defaultValue =FIRST_PAGE ) int page ,
109+ Model model ){
93110 int menuCategory = AdminMenuCategory .LOCATION_TAXES .ordinal ();
94111 model .addAttribute ("menuCategory" ,menuCategory );
95112 Country thisCountry = countryService .findCountryById (countryId );
96113 model .addAttribute ("thisCountry" ,thisCountry );
97- List <Country > countries = countryService .findAllCountriesOrderByName ();
114+ Pageable pageRequest = new PageRequest (page ,PAGE_SIZE , Sort .Direction .ASC , "name" );
115+ Page <Country > countries = countryService .findAllCountriesOrderByName (pageRequest );
98116 model .addAttribute ("countries" ,countries );
99117 List <AddressFormat > addressFormats = addressFormatService .findAllAddressFormat ();
100118 model .addAttribute ("addressFormats" ,addressFormats );
101119 return "admin/countriesEditForm" ;
102120 }
103121
104122 @ RequestMapping (value = "/admin/countries/{countryId}/edit" , method = RequestMethod .POST )
105- public String countriesEditSave (@ PathVariable long countryId , @ Valid Country thisCountry , BindingResult result , Model model ){
123+ public String countriesEditSave (@ PathVariable long countryId ,
124+ @ RequestParam (value ="page" ,defaultValue =FIRST_PAGE ) int page ,
125+ @ Valid Country thisCountry , BindingResult result , Model model ){
106126 logger .info ("Country: " +thisCountry .toString ());
107127 if (result .hasErrors ()){
108128 int menuCategory = AdminMenuCategory .LOCATION_TAXES .ordinal ();
109129 model .addAttribute ("menuCategory" ,menuCategory );
110- List <Country > countries = countryService .findAllCountriesOrderByName ();
130+ Pageable pageRequest = new PageRequest (page ,PAGE_SIZE , Sort .Direction .ASC , "name" );
131+ Page <Country > countries = countryService .findAllCountriesOrderByName (pageRequest );
111132 model .addAttribute ("countries" ,countries );
112133 model .addAttribute ("thisCountry" ,thisCountry );
113134 List <AddressFormat > addressFormats = addressFormatService .findAllAddressFormat ();
@@ -116,25 +137,30 @@ public String countriesEditSave(@PathVariable long countryId, @Valid Country thi
116137 } else {
117138 thisCountry .setId (countryId );
118139 countryService .updateCountry (thisCountry );
119- return "redirect:/admin/countries/" +countryId ;
140+ return "redirect:/admin/countries/" +countryId + "?page=" + page ;
120141 }
121142 }
122143
123144 @ RequestMapping (value = "/admin/countries/{countryId}/delete" , method = RequestMethod .GET )
124- public String countriesDeleteForm (@ PathVariable long countryId , Model model ){
145+ public String countriesDeleteForm (@ PathVariable long countryId ,
146+ @ RequestParam (value ="page" ,defaultValue =FIRST_PAGE ) int page ,
147+ Model model ){
125148 int menuCategory = AdminMenuCategory .LOCATION_TAXES .ordinal ();
126149 model .addAttribute ("menuCategory" ,menuCategory );
127150 Country thisCountry = countryService .findCountryById (countryId );
128151 model .addAttribute ("thisCountry" ,thisCountry );
129- List <Country > countries = countryService .findAllCountriesOrderByName ();
152+ Pageable pageRequest = new PageRequest (page ,PAGE_SIZE , Sort .Direction .ASC , "name" );
153+ Page <Country > countries = countryService .findAllCountriesOrderByName (pageRequest );
130154 model .addAttribute ("countries" ,countries );
131155 return "admin/countriesDeleteForm" ;
132156 }
133157
134158 @ RequestMapping (value = "/admin/countries/{countryId}/delete" , method = RequestMethod .POST )
135- public String countriesDeleteSave (@ PathVariable long countryId , Model model ){
159+ public String countriesDeleteSave (@ PathVariable long countryId ,
160+ @ RequestParam (value ="page" ,defaultValue =FIRST_PAGE ) int page ,
161+ Model model ){
136162 Country thisCountry = countryService .findCountryById (countryId );
137163 countryService .deleteCountry (thisCountry );
138- return "redirect:/admin/countries" ;
164+ return "redirect:/admin/countries?page=" + page ;
139165 }
140166}
0 commit comments