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