Don't settle for <html: error>

xiaoxiao2021-03-06  20

Many Struts Applications Can Get by with a Simple

. Tag at the top of a page If any error messages are present, it's this tag's job to print them all out To help prettify the output, . Checks for errors.header and errors.footer messages in the application resources .......................................... ..

Errors.Header =

    Errors.footer =

    Struts 1.0 developers can then include

  • and tags with the text of each message to used this way. In Struts 1.1, the situation improves with the addition of the errors.prefix and errors.suffix messages. Just as the Header And Footer Print Before and After The Block of Messages, The Prefix and Suffix Print Before The Individual Messages. So to Print A Simple List of Any Message That Might Arise, You Can Just Include

    Errors.Header =

      Errors.Footer

      Errors.PREFIX =

    • Errors.suffix =

      In Your Application Resources, and The (Struts 1.1) Tag Will Take Care of The REST.

      Though, many purists would complain that HTML markup has no place in a message resources file. And they would right. Even with the Struts 1.1 prefix and suffix feature, you may still need to use different markup different pages.

      For Struts 1.0 applications, the Struts Validation extension (see the Struts Resource page) offers a useful alternative to the standard. tag You can use these tags whether you are using the rest of the validator package or not Instead. of providing one omnibus tag, the validator approach is to use an iterator to expose each message, and then leave it up to the page to provide whatever other formatting is necessary Here's an example:.

      • In struts 1.1, the tags were adopted Into the core taglibs. Here's The Same Example Using The Struts 1.1 Rendition.

        • This is all great if you just want to print your messages as a batch. But many messages are related to data-entry validation and involve a specific field. Many page designs expect a message concerning a field to printed next to a field.

          NOT A Problem. When the error message is queued, you can to go with it. If you don't specify a property (using any of the tags we described), the all the message print. If you do Specify a Property, Then Only The Messages Queued for That Property print.

          The default code for queuing an error message is:

          Errors.Add (

          ActionerRORS.GLOBAL_ERROR,

          New ActionError ("Error.username.Required)

          );

          To Specify That this message is for the "username" Property, We Would Code this instead: errors.add (

          "Username", New Actionerror ("Error.userName.Required)));

          IF WE Specify A Property, We can use the tag (or any of the alternative) LIKE THIS:

          Username:

          password:

          The "Username" Errors Print next to the username field, and any "password" Errors Print Next to the Password Field.

          But what if you need to print Both specific and general errors?

          . Again, no problem You can also specify the "generic" property just like you did in the Java code First, at the top of your JavaServer Page import the Action and ActionErrors package so you can reference the appropriate constants.:

          <% @ page import = "org.apache.struts.Action.action"%>

          <% @ page import = "org.apache.struts.Action.Actionerror"%>

          The THE TAGS, USE A Runtime Expression to Specify The Constants:

          Viola! Specific Messages Print Out In Specific Places, And Any "General" Errors CAN Still Print Out in A Place of their OWN.

          Of course, you do not have to settle for any of these standard tags. If these variations still do not meet your specific needs, take a peek at the source code and cobble up your own! The framework provides the queue, but how IT Prints is up to you.

          Hth, TED.

转载请注明原文地址:https://www.9cbs.com/read-43750.html

New Post(0)