1 package org.springframework.samples.countries.web; 2 3 import java.util.List ; 4 import java.util.Locale ; 5 6 import javax.servlet.ServletException ; 7 import javax.servlet.http.HttpServletRequest ; 8 import javax.servlet.http.HttpServletResponse ; 9 10 import org.springframework.beans.support.PagedListSourceProvider; 11 import org.springframework.beans.support.RefreshablePagedListHolder; 12 import org.springframework.samples.countries.Country; 13 import org.springframework.samples.countries.CountryService; 14 import org.springframework.validation.BindException; 15 import org.springframework.web.bind.BindUtils; 16 import org.springframework.web.servlet.ModelAndView; 17 import org.springframework.web.servlet.mvc.multiaction.MultiActionController; 18 import org.springframework.web.servlet.support.RequestContextUtils; 19 20 24 public class CountriesController extends MultiActionController { 25 26 private static final String COUNTRIES_ATTR = "countries"; 27 private static final String HOME_VIEW = "homeView"; 28 private static final String CONFIG_VIEW = "configView"; 29 private static final String MAIN_VIEW = "countries_mainView"; 30 private static final String DETAIL_VIEW = "countries_detailView"; 31 private static final String EXCEL_VIEW = "countries_excelView"; 32 private static final String PDF_VIEW = "countries_pdfView"; 33 34 private CountryService countryService; 35 36 private String homeView = HOME_VIEW; 37 private String configView = CONFIG_VIEW; 38 private String mainView = MAIN_VIEW; 39 private String detailView = DETAIL_VIEW; 40 private String excelView = EXCEL_VIEW; 41 private String pdfView = PDF_VIEW; 42 43 44 public void setCountryService(CountryService countryService) { 45 this.countryService = countryService; 46 } 47 48 public void setConfigView(String view) { 49 this.configView = view; 50 } 51 52 public void setDetailView(String view) { 53 this.detailView = view; 54 } 55 56 public void setExcelView(String view) { 57 this.excelView = view; 58 } 59 60 public void setHomeView(String view) { 61 this.homeView = view; 62 } 63 64 public void setMainView(String view) { 65 this.mainView = view; 66 } 67 68 public void setPdfView(String view) { 69 this.pdfView = view; 70 } 71 72 73 79 public ModelAndView handleHome(HttpServletRequest request, HttpServletResponse response) throws ServletException { 80 return new ModelAndView(homeView); 81 } 82 83 89 public ModelAndView handleConfig(HttpServletRequest request, HttpServletResponse response) throws ServletException { 90 return new ModelAndView(configView); 91 } 92 93 99 public ModelAndView handleMain(HttpServletRequest request, HttpServletResponse response) throws ServletException { 100 RefreshablePagedListHolder listHolder = 101 (RefreshablePagedListHolder) request.getSession(true).getAttribute(COUNTRIES_ATTR); 102 if (null == listHolder) { 103 listHolder = new RefreshablePagedListHolder(); 104 listHolder.setSourceProvider(new CountriesProvider()); 105 listHolder.setFilter(new CountriesFilter()); 106 request.getSession(true).setAttribute(COUNTRIES_ATTR, listHolder); 107 } 108 BindException ex = BindUtils.bind(request, listHolder, "countries"); 109 listHolder.setLocale(RequestContextUtils.getLocale(request)); 110 boolean forceRefresh = request.getParameter("forceRefresh") != null; 111 listHolder.refresh(forceRefresh); 112 return new ModelAndView(mainView, ex.getModel()); 113 } 114 115 121 public ModelAndView handleDetail(HttpServletRequest request, HttpServletResponse response) throws ServletException { 122 Locale locale = RequestContextUtils.getLocale(request); 123 Country country = this.countryService.getCountry(request.getParameter("code"), locale); 124 return new ModelAndView(this.detailView, "country", country); 125 } 126 127 133 public ModelAndView handleExcel(HttpServletRequest request, HttpServletResponse response) throws ServletException { 134 RefreshablePagedListHolder listHolder = 135 (RefreshablePagedListHolder) request.getSession(true).getAttribute(COUNTRIES_ATTR); 136 if (listHolder == null) { 137 throw new ServletException ("No countries list found in session"); 138 } 139 return new ModelAndView(this.excelView, "countries", listHolder); 140 } 141 142 148 public ModelAndView handlePdf(HttpServletRequest request, HttpServletResponse response) throws ServletException { 149 RefreshablePagedListHolder listHolder = 150 (RefreshablePagedListHolder) request.getSession(true).getAttribute(COUNTRIES_ATTR); 151 if (listHolder == null) { 152 throw new ServletException ("No countries list found in session"); 153 } 154 return new ModelAndView(this.pdfView, "countries", listHolder); 155 } 156 157 158 private class CountriesProvider implements PagedListSourceProvider { 159 160 public List loadList(Locale loc, Object filter) { 161 CountriesFilter cf = (CountriesFilter) filter; 162 return countryService.getFilteredCountries(cf.getName(), cf.getCode(), loc); 163 } 164 } 165 166 } 167 | Popular Tags |