View Javadoc

1   /* Copyright 2004 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  
16  package sample.contact;
17  
18  import org.springframework.beans.factory.InitializingBean;
19  
20  import org.springframework.web.servlet.ModelAndView;
21  import org.springframework.web.servlet.mvc.Controller;
22  import org.springframework.util.Assert;
23  
24  import java.io.IOException;
25  
26  import java.util.HashMap;
27  import java.util.List;
28  import java.util.Map;
29  
30  import javax.servlet.ServletException;
31  import javax.servlet.http.HttpServletRequest;
32  import javax.servlet.http.HttpServletResponse;
33  
34  
35  /***
36   * Controller for secure index page.
37   *
38   * @author Ben Alex
39   * @version $Id: SecureIndexController.java,v 1.9 2005/04/15 01:21:32 luke_t Exp $
40   */
41  public class SecureIndexController implements Controller, InitializingBean {
42      //~ Instance fields ========================================================
43  
44      private ContactManager contactManager;
45  
46      //~ Methods ================================================================
47  
48      public void setContactManager(ContactManager contact) {
49          this.contactManager = contact;
50      }
51  
52      public ContactManager getContactManager() {
53          return contactManager;
54      }
55  
56      public void afterPropertiesSet() throws Exception {
57          Assert.notNull(contactManager, "A ContactManager implementation is required");
58      }
59  
60      public ModelAndView handleRequest(HttpServletRequest request,
61          HttpServletResponse response) throws ServletException, IOException {
62          List myContactsList = contactManager.getAll();
63          Contact[] myContacts;
64  
65          if (myContactsList.size() == 0) {
66              myContacts = null;
67          } else {
68              myContacts = (Contact[]) myContactsList.toArray(new Contact[] {});
69          }
70  
71          Map model = new HashMap();
72          model.put("contacts", myContacts);
73  
74          return new ModelAndView("index", "model", model);
75      }
76  }