DOC

The following are the list of changes that occurred using the Struts tag library

By Charles Warren,2014-09-16 12:52
12 views 0
The following are the list of changes that occurred using the Struts tag library

    ;3

作者?friendcn

日期?2001-2-22 12:50:46

Page changes

The following are the list of changes that occurred

    using the Struts tag library:

Imports

<%@ taglib uri="/WEB-INF/struts.tld"

    prefix="struts" %>

    The <%@page import? for Java has been replaced by <%@ taglib uri? for the Struts tag library.

Text

The resource property file contains the text for

    join.title. In this example, ApplicationResources property file contains the name-value pair. This

    makes string review and changes for internationalization easier.

Errors

    ActionServlet or ActionForm builds the error message to display. These error messages can also be contained in the property file. ApplicationResources also provides a way of formatting the error by setting error.header and error.footer.

HTML Form

    JSP rm> tags and attributes replace HTML

tags and attributes. name="join"> has changed to

    action="join.do" focus="email" >.

HTML tag has been replaced by .

HTML tag has been replaced by

    .

Model -- Session state

    JoinForm subclasses ActionForm and contains the form

data. The form data in this example is simply the e-

    mail address. I have added a setter and getter for the e-mail address for the framework to access. For demonstration purposes, I overwrote the validate() method and used the error tracking feature of Struts. Struts will create JoinForm and set the state information.

Model -- Business logic

    As we discussed earlier, Action is the interface between the Controller and the actual business

    object. JoinAction wraps the calls to the business.jar that was originally in join.jsp. The perform() method for JoinAction is displayed in Listing 5.

Listing 5. - JoinAction.perform()

public ActionForward perform(ActionMapping mapping,

     ActionForm form,

     HttpServletRequest

    request,

     HttpServletResponse

    response)

     throws IOException,

    ServletException {

     // Extract attributes and parameters we will need

     JoinForm joinForm = (JoinForm) form;

     String email = joinForm.getEmail();

     ActionErrors errors = new ActionErrors();

     // store input....

     try {

     business.db.MailingList.AddEmail(email);

     } catch (Exception e) {

     // log, print stack

     // display error back to user

     errors.add("email",new ActionError("error.mailing.db.add"));

     }

     // If any messages is required, save the

    specified error messages keys

     // into the HTTP request for use by the

     tag.

     if (!errors.empty()) {

     saveErrors(request, errors);

     // return to the original form

     return (new

    ActionForward(mapping.getInput()));

     }

     // Forward control to the specified 'success' URI that is in the Action.xml

     return (mapping.findForward("success"));

}