< 上一课 | 下一课 >

课程 6:编辑操作映射

操作映射表示一项在运行的操作。当它正在运行时,它将确定应用程序的流。

操作映射是一个配置文件条目,通常包含对操作类的引用。此条目可以包含对某操作可使用的表单 bean 的引用,此外,还可以定义仅对此操作可视的本地转发和异常。为 JavaServer Pages(JSP)页编写代码时,可以引用操作映射以触发某项操作。

操作类中包含在提交表单时就会调用的 execute 方法。在此方法中,可以提供逻辑以确定计算结果是“成功”(由指向输出 JSP 页的转发来定义)还是“失败”(由指向输入 JSP 页的转发来定义)。

要编辑操作映射:

  1. 在“项目资源管理器”中,浏览至 DayOfWeek\Java Resources: src\dayofweek.actions 文件夹。
  2. 双击 ComputeDayAction.java 文件以将它打开。
  3. 在该文件中,选择所有文本并按 Delete 键以删除这些文本。
  4. 复制以下代码并将它粘贴到该文件中:
    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);
    
      }
    
    }
    提示: 您的项目中的包名可能不是 dayofweek.actions。在这种情况下,应将上述第一个代码行中的 dayofweek.actions 替换为您的包名。
  5. 保存并关闭该文件。

课程要点

现在,操作类中包含在提交 Web 表单时就会调用的 execute 方法。您已经创建了逻辑以确定计算结果是“成功”(由指向输出 JSP 页的转发来定义)还是“失败”(由指向输入 JSP 页的转发来定义)。

您学习了如何完成下列任务:
  • 向操作类添加 execute 方法。
  • 创建逻辑以确定操作是成功还是失败。
< 上一课 | 下一课 >