< Previous | Next >

Lesson 1.6: Edit an action mapping

In this lesson, you will lean how to edit an action mapping. Action mapping represents an action that gets executed, and in that execution, determines the correct path that the application will take.

Action mapping is defined in the Struts configuration file, generally referencing a Java class , but sometimes it simply forwards the request to another page. When user codes in the JSP, they can simply reference the action mapping to trigger the invocation of the Action.

The action class contains an execute method that will get called when the form is submitted. This is where you supply the logic to determine whether the calculated result is "success" (defined by the forward pointing to the output.jsp), or "failure" (defined by the forward pointing back to index.jsp)

To edit an action mapping:

  1. In Project Explorer, navigate to DayOfWeek > Java Resources: src > dayofweek.actions.
  2. Double-click ComputeDayAction.java.
  3. Select all of the text in the ComputeDayAction.java file, then press Delete.
  4. Copy and paste the following code into the ComputeDayAction.java file:
    package dayofweek.actions;
    
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import org.apache.struts.action.Action;
    import org.apache.struts.action.ActionError;
    import org.apache.struts.action.ActionErrors;
    import org.apache.struts.action.ActionForm;
    import org.apache.struts.action.ActionForward;
    import org.apache.struts.action.ActionMapping;
    
    import com.ibm.dayofweek.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 {
    
    		ActionErrors errors = new ActionErrors();
    		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 ActionError("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);
    
    	}
    
    }
    Note: It is possible that the package name could change depending on your project. If the package name has changed, copy the code starting at:
    import javax.servlet.http.HttpServletRequest;
    Also make sure not to line wrap the // comments because as after the line wrap, anything that is not code will be flagged as error.
  5. Save and close the file.

Lesson checkpoint

Your action class now contains an execute method that will get called when the form is submitted. You have supplied the logic to determine whether the calculated result is "success" (defined by the forward pointing to the output.jsp), or "failure" (defined by the forward pointing back to index.jsp).

You learned the following:
  • How to add an execute method.
  • How to supply the logic to determine either a success or a failure.
  • What determines a success or failure result.
< Previous | Next >