< Previous | Next >

 

 

Lesson 6: Edit an action mapping

 

+

Search Tips   |   Advanced Search

 

Use this lesson to learn how an action mapping represents an action that is run, and when running, it determines the flow of the application.

An action mapping is a configuration file entry that usually contains a reference to an action class. This entry can contain a reference to a form bean that the action can use, and can also define local forwards and exceptions that are visible only to this action. When you write code for a JSP page, you can refer to the action mapping to trigger an action.

The action class contains an execute method that is called when the form is submitted. In this method, you can supply the logic to determine if the calculated result is success (which is defined by the forward that points to the output JSP page), or failure (which is defined by the forward that points to the input JSP page).

To edit an action mapping:

  1. In the Enterprise Explorer, double-click

    DayOfWeek | Java Resources: src | com.setgetweb.dayofweek.actions | ComputeDayAction.java

  2. In the file, select all of the text and press Delete to remove it.

  3. Copy and paste the following code into the file:

    package com.setgetweb.dayofweek.actions;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import org.apache.struts.action.Action;
    import org.apache.struts.action.ActionMessage;
    import org.apache.struts.action.ActionMessages;
    import org.apache.struts.action.ActionForm;
    import org.apache.struts.action.ActionForward;
    import org.apache.struts.action.ActionMapping;
    
    import com.setgetweb.dayofweek.forms.DateData;
    
    /**
     * @version   1.0
     * @author
     */
    
    public class ComputeDayAction extends Action {
    
      /**
       * Constructor
       */
      public ComputeDayAction() {
    
        super();
    
      }
    
      public ActionForward execute(
        ActionMapping mapping,
        ActionForm form,
        HttpServletRequest request,
        HttpServletResponse response)
        throws Exception {
    
        ActionMessages errors = new ActionMessages();
        ActionForward forward = new ActionForward();
        // return value
        DateData dateData = (DateData) form;
        int year;
        int month;
        int day;
        int dayofWeek;
        int valcen;
        int valleap;
        int valyear;
        int valmon;
        int valday;
        int[] centuries = new int[4];
        int[] months = new int[13];
        String[] daysOfWeek = new String[7];
        centuries[0] = 2;
        centuries[1] = 0;
        centuries[2] = 5;
        centuries[3] = 3;
        months[1] = 5;
        months[2] = 1;
        months[3] = 0;
        months[4] = 3;
        months[5] = 5;
        months[6] = 1;
        months[7] = 3;
        months[8] = 6;
        months[9] = 2;
        months[10] = 4;
        months[11] = 0;
        months[12] = 2;
        daysOfWeek[0] = "Sunday";
        daysOfWeek[1] = "Monday";
        daysOfWeek[2] = "Tuesday";
        daysOfWeek[3] = "Wednesday";
        daysOfWeek[4] = "Thursday";
        daysOfWeek[5] = "Friday";
        daysOfWeek[6] = "Saturday";
    
        try {
          day = dateData.getDay();
          month = dateData.getMonth();
          year = dateData.getYear();
          if (month < 3) {
            year--; // Subtract 1 from year
          }
          valcen = centuries[year / 100 % 4];
          valleap = year % 100 / 4;
          valyear = year % 100 % 7;
          valmon = months[month];
          valday = day % 7;
          dayofWeek = valcen + valleap + valyear + valmon + valday;
          dayofWeek = dayofWeek % 7;
          dateData.setDayOfWeek(daysOfWeek[dayofWeek]);
          request.setAttribute("dateData", dateData);
    
        } catch (Exception e) {
    
          // Report the error using the appropriate name and ID.
          errors.add("name", new ActionMessage("id"));
          e.printStackTrace();
        }
    
        // If a message is required, save the specified key(s)
        // into the request for use by the tag. 
    
        if (!errors.isEmpty()) {
          saveErrors(request, errors);
          forward = mapping.findForward("failure");
        } else {
          forward = mapping.findForward("success");
        }
    
        // Finish with
        return (forward);
    
      }
      
      protected void addErrors(javax.servlet.http.HttpServletRequest request,
              ActionMessages errors) {
    	 // request.
      }
    
    }

    If you get an error with...

    dateData.setDayOfWeek(daysOfWeek[dayofWeek]);

    ...hover cursor of line and choose...

    Change to setDayofWeek[...]

    ...so that it looks like...

    dateData.setDayofWeek(daysOfWeek[dayofWeek]);

  4. Save and close the file.

 

Lesson checkpoint

Your action class now contains an

execute method that is invoked when the Web form is submitted. You have created the logic to determine if the calculated result is success (which is defined by the forward that points to the output JSP page), or failure (which is defined by the forward that points to the input JSP page).

You learned to do these tasks:

< Previous | Next >