KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > samples > countries > web > CountriesController


1 package org.springframework.samples.countries.web;
2
3 import java.util.List JavaDoc;
4 import java.util.Locale JavaDoc;
5
6 import javax.servlet.ServletException JavaDoc;
7 import javax.servlet.http.HttpServletRequest JavaDoc;
8 import javax.servlet.http.HttpServletResponse JavaDoc;
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 /**
21  * @author Jean-Pierre Pawlak
22  * @author Juergen Hoeller
23  */

24 public class CountriesController extends MultiActionController {
25
26     private static final String JavaDoc COUNTRIES_ATTR = "countries";
27     private static final String JavaDoc HOME_VIEW = "homeView";
28     private static final String JavaDoc CONFIG_VIEW = "configView";
29     private static final String JavaDoc MAIN_VIEW = "countries_mainView";
30     private static final String JavaDoc DETAIL_VIEW = "countries_detailView";
31     private static final String JavaDoc EXCEL_VIEW = "countries_excelView";
32     private static final String JavaDoc PDF_VIEW = "countries_pdfView";
33
34     private CountryService countryService;
35
36     private String JavaDoc homeView = HOME_VIEW;
37     private String JavaDoc configView = CONFIG_VIEW;
38     private String JavaDoc mainView = MAIN_VIEW;
39     private String JavaDoc detailView = DETAIL_VIEW;
40     private String JavaDoc excelView = EXCEL_VIEW;
41     private String JavaDoc pdfView = PDF_VIEW;
42
43
44     public void setCountryService(CountryService countryService) {
45         this.countryService = countryService;
46     }
47
48     public void setConfigView(String JavaDoc view) {
49         this.configView = view;
50     }
51
52     public void setDetailView(String JavaDoc view) {
53         this.detailView = view;
54     }
55
56     public void setExcelView(String JavaDoc view) {
57         this.excelView = view;
58     }
59
60     public void setHomeView(String JavaDoc view) {
61         this.homeView = view;
62     }
63
64     public void setMainView(String JavaDoc view) {
65         this.mainView = view;
66     }
67
68     public void setPdfView(String JavaDoc view) {
69         this.pdfView = view;
70     }
71
72
73     /**
74      * Custom handler for home.
75      * @param request current HTTP request
76      * @param response current HTTP response
77      * @return a ModelAndView to render the response
78      */

79     public ModelAndView handleHome(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws ServletException JavaDoc {
80         return new ModelAndView(homeView);
81     }
82
83     /**
84      * Custom handler for config.
85      * @param request current HTTP request
86      * @param response current HTTP response
87      * @return a ModelAndView to render the response
88      */

89     public ModelAndView handleConfig(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws ServletException JavaDoc {
90         return new ModelAndView(configView);
91     }
92
93     /**
94      * Custom handler for countries main paged list.
95      * @param request current HTTP request
96      * @param response current HTTP response
97      * @return a ModelAndView to render the response
98      */

99     public ModelAndView handleMain(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws ServletException JavaDoc {
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     /**
116      * Custom handler for countries detail page.
117      * @param request current HTTP request
118      * @param response current HTTP response
119      * @return a ModelAndView to render the response
120      */

121     public ModelAndView handleDetail(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws ServletException JavaDoc {
122         Locale JavaDoc 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     /**
128      * Custom handler for countries Excel document.
129      * @param request current HTTP request
130      * @param response current HTTP response
131      * @return a ModelAndView to render the response
132      */

133     public ModelAndView handleExcel(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws ServletException JavaDoc {
134         RefreshablePagedListHolder listHolder =
135                 (RefreshablePagedListHolder) request.getSession(true).getAttribute(COUNTRIES_ATTR);
136         if (listHolder == null) {
137             throw new ServletException JavaDoc("No countries list found in session");
138         }
139         return new ModelAndView(this.excelView, "countries", listHolder);
140     }
141
142     /**
143      * Custom handler for countries PDF document.
144      * @param request current HTTP request
145      * @param response current HTTP response
146      * @return a ModelAndView to render the response
147      */

148     public ModelAndView handlePdf(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws ServletException JavaDoc {
149         RefreshablePagedListHolder listHolder =
150                 (RefreshablePagedListHolder) request.getSession(true).getAttribute(COUNTRIES_ATTR);
151         if (listHolder == null) {
152             throw new ServletException JavaDoc("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 JavaDoc loadList(Locale JavaDoc loc, Object JavaDoc filter) {
161             CountriesFilter cf = (CountriesFilter) filter;
162             return countryService.getFilteredCountries(cf.getName(), cf.getCode(), loc);
163         }
164     }
165
166 }
167
Popular Tags