Chủ Nhật, 4 tháng 1, 2009

Viết ứng dụng đầu tiên với Struts (Phần 2)

4. Tạo JSP CustomerForm sử dụng Struts Tags :

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
   
         <bean:message key="exercise01.formpage.title"/>
        
   
   
         

         
         
               :
              
              
               :
              
              
              
                    
              
              
          
    

5. Tạo các class Action :

public class CustomerAction extends Action
{
      public ActionForward execute(ActionMapping mapping, ActionForm form,
           HttpServletRequest request, HttpServletResponse response) throws Exception
           {
           ActionForward nextPage = null;
           if (isCancelled(request)) {
                 System.out.println("Cancel Operation Performed");
                 return mapping.findForward("mainpage");
           }
           CustomerForm custForm = (CustomerForm) form;
           if ("Save".equals(custForm.getStep()))
           {
                  String firstName = custForm.getFirstName();
                  String lastName = custForm.getLastName();
                  System.out.println("Customer First name is " + firstName);
                  System.out.println("Customer Last name is " + lastName);
                  nextPage = mapping.findForward("success");
           }
           return nextPage;
      }
}

6. Thêm properties cho MessageResources.properties :

########################################
# Exercise01 index page strings
########################################
exercise01.indexpage.title=Welcome to Exercise01

########################################
# Exercise01 CustomerForm strings
########################################
exercise01.formpage.title=Please enter your details
prompt.customer.firstname=First Name
prompt.customer.lastname=Last Name
button.save=Save
button.cancel=Cancel

7. Thêm validation cho các bean Form :

public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
       ActionErrors errors = new ActionErrors();

       // Firstname cannot be empty
       if (firstName == null || firstName.trim().equals("")) {
              errors.add("firstName", new ActionError("error.cust.firstname.empty"));
       }

       // Lastname cannot be empty
       if (lastName == null || lastName.trim().equals("")) {
              errors.add("lastName", new ActionError("error.cust.lastname.empty"));
       }
      return errors;
}

8. Thêm các khóa cho ActionError từ Message Resources :


########################################
# Common
########################################
errors.header=

Validation Error

You must correct the following error(s) before proceeding:
errors.footer=
errors.prefix=
  • errors.suffix=

    ########################################
    # Exercise01 CustomerForm ActionErrors
    ########################################
    error.cust.firstname.empty=First Name is Required
    error.cust.lastname.empty=Last Name is Required

    Xem tiếp phần còn lại.

    DangTrung.

    Không có nhận xét nào:

    Đăng nhận xét