View Javadoc

1   /*
2    * $Id: PrepareLogicAction.java 421486 2006-07-13 03:37:08Z wsmoak $
3    *
4    * Copyright 2005 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  package examples.logic;
20  
21  import java.util.ArrayList;
22  
23  import javax.servlet.http.HttpServletRequest;
24  import javax.servlet.http.HttpServletResponse;
25  
26  import org.apache.struts.action.Action;
27  import org.apache.struts.action.ActionErrors;
28  import org.apache.struts.action.ActionForm;
29  import org.apache.struts.action.ActionForward;
30  import org.apache.struts.action.ActionMapping;
31  import org.apache.struts.action.ActionMessage;
32  import org.apache.struts.action.ActionMessages;
33  
34  import examples.TestBean;
35  import examples.options.BookBean;
36  
37  /***
38   * Perform any tasks and setup any data that
39   * must be prepared before the form is displayed.
40   *
41   * @version $Rev: 421486 $ $Date: 2006-07-12 20:37:08 -0700 (Wed, 12 Jul 2006) $
42   */
43  public class PrepareLogicAction extends Action {
44  
45      // ------------------------------------------------------------ Constructors
46  
47      /***
48       * Constructor for PrepareOptionsAction.
49       */
50      public PrepareLogicAction() {
51          super();
52      }
53  
54      // ---------------------------------------------------------- Action Methods
55  
56      /***
57       * Process the request and return an <code>ActionForward</code> instance
58       * describing where and how control should be forwarded, or
59       * <code>null</code>if the response has already been completed.
60       *
61       * @param mapping The ActionMapping used to select this instance
62       * @param form The optional ActionForm bean for this request (if any)
63       * @param request The HTTP request we are processing
64       * @param response The HTTP response we are creating
65       *
66       * @exception Exception if an exception occurs
67       *
68       * @return the ActionForward to forward control to
69       */
70      public ActionForward execute(
71          ActionMapping mapping,
72          ActionForm form,
73          HttpServletRequest request,
74          HttpServletResponse response)
75          throws Exception {
76  
77          TestBean bean = new TestBean();
78          request.setAttribute("testBean", bean);
79  
80          ArrayList items = new ArrayList();
81          request.setAttribute("items", items);
82  
83          request.setAttribute("intValue", new Integer(7));
84          request.setAttribute("stringValue", "Hello, world!");
85  
86          /* Collection of custom beans */
87          ArrayList books = new ArrayList();
88          books.add(new BookBean("0596003285", "Programming Jakarta Struts"));
89          books.add(new BookBean("1930110502", "Struts in Action"));
90          books.add(new BookBean("1861007817", "Professional Struts Applications"));
91          books.add(new BookBean("0672324725", "Struts Kick Start"));
92          books.add(new BookBean("0471213020", "Mastering Jakarta Struts"));
93          books.add(new BookBean("1558608621", "The Struts Framework"));
94          books.add(new BookBean("0971661901", "Struts Fast Track"));
95          request.setAttribute("books", books);
96  
97          ActionErrors errors = new ActionErrors();
98  
99          errors.add(ActionMessages.GLOBAL_MESSAGE,
100             new ActionMessage("errors.detail", "This is a global error #1"));
101         errors.add(ActionMessages.GLOBAL_MESSAGE,
102             new ActionMessage("errors.detail", "This is a global error #2"));
103         errors.add("test",
104             new ActionMessage("errors.detail", "This is a test error"));
105 
106         ActionMessages messages = new ActionMessages();
107         messages.add(ActionMessages.GLOBAL_MESSAGE,
108             new ActionMessage("message.detail", "This is global message #1"));
109         messages.add(ActionMessages.GLOBAL_MESSAGE,
110             new ActionMessage("message.detail", "This is global message #2"));
111         messages.add("test",
112             new ActionMessage("message.example.simple"));
113 
114 
115         saveMessages(request, messages);
116         saveErrors(request, errors);
117 
118         // Forward to the form
119         return mapping.findForward("success");
120 
121     }
122 
123 }