SecureIndexController.java 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. package sample.contact;
  16. import org.springframework.beans.factory.InitializingBean;
  17. import org.springframework.util.Assert;
  18. import org.springframework.web.servlet.ModelAndView;
  19. import org.springframework.web.servlet.mvc.Controller;
  20. import java.io.IOException;
  21. import java.util.HashMap;
  22. import java.util.List;
  23. import java.util.Map;
  24. import javax.servlet.ServletException;
  25. import javax.servlet.http.HttpServletRequest;
  26. import javax.servlet.http.HttpServletResponse;
  27. /**
  28. * Controller for secure index page.
  29. *
  30. * @author Ben Alex
  31. * @version $Id$
  32. */
  33. public class SecureIndexController implements Controller, InitializingBean {
  34. //~ Instance fields ================================================================================================
  35. private ContactManager contactManager;
  36. //~ Methods ========================================================================================================
  37. public void afterPropertiesSet() throws Exception {
  38. Assert.notNull(contactManager, "A ContactManager implementation is required");
  39. }
  40. public ContactManager getContactManager() {
  41. return contactManager;
  42. }
  43. public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response)
  44. throws ServletException, IOException {
  45. List myContactsList = contactManager.getAll();
  46. Contact[] myContacts;
  47. if (myContactsList.size() == 0) {
  48. myContacts = null;
  49. } else {
  50. myContacts = (Contact[]) myContactsList.toArray(new Contact[] {});
  51. }
  52. Map model = new HashMap();
  53. model.put("contacts", myContacts);
  54. return new ModelAndView("index", "model", model);
  55. }
  56. public void setContactManager(ContactManager contact) {
  57. this.contactManager = contact;
  58. }
  59. }