Dear Wiki user,

You have subscribed to a wiki page or wiki category on "Struts Wiki" for change 
notification.

The following page has been changed by MichaelJouravlev:
http://wiki.apache.org/struts/StrutsQuickStart1

------------------------------------------------------------------------------
  == Employee list with Struts + JSP ==
+ 
+ This example produces the same output as a servlet-based application, but now 
this is a Struts application.
  
  inline:employee_list.gif
  
- In the previous Model 2 example the name of the presentation page is 
hardcoded in the servlet. Would not it be nice to externalize this information? 
Struts framework allows to do that with {struts-config.xml} file. Instead of a 
servlet you will be developing a custom Action class:
+ In the servlet-based Model 2 example the name of the presentation page was 
hardcoded in the servlet. Would not it be nice to externalize this information? 
Struts framework allows to do that with the help of {{{struts-config.xml}}} 
file. Instead of a servlet you will be developing a custom Action class:
  
  {{{import org.apache.struts.action.ActionForward;
  import org.apache.struts.action.ActionMapping;
@@ -18, +20 @@

  import java.util.ArrayList;
  
  /**
-  * Accesses the virtual imployee database.
+  * This action prepares employees for display
   */
  public class EmployeesListAction extends Action {
  
@@ -28, +30 @@

      public ActionForward execute(ActionMapping mapping,
                                   ActionForm form,
                                   HttpServletRequest request,
-                                  HttpServletResponse response) throws 
Exception {
+                                  HttpServletResponse response) throws 
Exception
-         // Lookup virtual database, if not found - instantiate it
+     {
-         // and store in the session under "employees" name
+         // Load employee list and store it in the session under "employees" 
name
          HttpSession session = request.getSession();
          ArrayList employees = (ArrayList) session.getAttribute("employees");
          if (employees == null) {
@@ -38, +40 @@

              session.setAttribute("employees", employees);
          }
  
-         // Return "render" outcome to display employee list (see 
struts-config.xml)
+         // Return "success" outcome to display employee list (see 
struts-config.xml).
+         // "success" is not hardcoded, you can use any string you want.
-         return mapping.findForward("render");
+         return mapping.findForward("success");
      }
  }
  }}}
  
- Notice that {{{execute}}} method returns an action mapping, passing to it 
"render" outcome code. The correspondence of this code to a presentation page 
is defined in {{{struts-config.xml}}} file:
+ Notice that {{{execute}}} method returns an action mapping, passing to it 
"render" outcome code. The correspondence of this code to a presentation page 
is defined in {{{struts-config.xml}}} file. This file plays the role of the 
traffic cop in a Struts application, it contains all configuration information 
and shows the structure of Struts application.
  
  {{{<struts-config>
-     <action-mappings>
+   <action-mappings>
-       <action path  = "/employeesList" type  = "actions.EmployeesListAction">
+     <action path = "/employeesList" 
+             type = "actions.EmployeesListAction">
-           <forward name = "render" path = "/jspstruts1/employees.jsp"/>
+       <forward name = "success" path = "/pages/employees.jsp"/>
-       </action>
+     </action>
      ...
  <struts-config>}}}
  
- The presentation page is unchanged from previous Servlet/JSP example. 
+ As you can see, the Action class knows nothing about what happens after it 
finishes. The "forward" element in the action mapping can be changed without 
changing the action class. Also, Action class does not forward to a specific 
location, instead it returns the outcome code. This makes Action class more 
abstract that traditional servlet.
  
+ The presentation page is unchanged from servlet/JSP example, here it is for 
completeness:
+ 
+ {{{<%@ page contentType="text/html;charset=UTF-8" language="java" %>
+ 
+ <html>
+   <body>
+ 
+     <table>
+       <tr>
+         <th align="left">Emp #</th>
+         <th align="left">Name</th>
+         <th align="left">Salary</th>
+       </tr>
+       <c:forEach var="employee" items="${employees}">
+         <tr>
+           <td>${employee.id}</td>
+           <td>${employee.name}</td>
+           <td>${employee.salary}</td>
+         </tr>
+       </c:forEach>
+     </table>
+ 
+   </body>
+ </html>}}}
+ 
+ [:StrutsQuickStart2:Next: handling events in Struts Action]
+ 

Reply via email to