Web page design - Each MessageResourcesinstance is stored in the ServletContext when

Each MessageResourcesinstance is stored in the ServletContext when the application is initialized and can be accessed from just about any component within the servlet container. However, you’ll more typically use a combination of the custom tags and the ActionMessageor ActionError classes to access the resources, rather than calling methods on the MessageResources class directly. In fact, if you use a combination of the declarative exception handling discussed in Chapter 10 and the Validator framework from Chapter 11, it’s possible that you won’t have to create ActionMessageor ActionError instances at all; the framework will do it for you automatically. If you need to create an ActionMessageor ActionErrormanually, however, you can. From the validate( ) method of an ActionForm, it would look something like this: public ActionErrors validate(ActionMapping mapping, HttpServletRequest request){ ActionErrors errors = new ActionErrors( ); if(getEmail() == null || getEmail().length( ) < 1) { errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("security.error.email.required")); } if(getPassword() == null || getPassword().length( ) < 1) { errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("security.error.password.required")); } return errors; } The String argument passed into the constructor of the ActionError class must be a key in the resource bundle. The ActionMessageand ActionError classes have several constructors, most of which allow you to pass in substitution arguments, for when your messages are compound messages, as explained earlier in this chapter. You can also create ActionMessages and ActionErrors from an Action class. The following is a small fragment from the Storefront LoginActionclass: // Log in through the security service IStorefrontService serviceImpl = getStorefrontService( ); String errorMsg = null; UserView userView = null; try{ // Attempt to authenticate the user userView = serviceImpl.authenticate(email, password); }catch( InvalidLoginException ex ){ ActionErrors errors = new ActionErrors( );
We recommend cheap and reliable webhost to host and run your web applications: Coldfusion Web Hosting services.

Leave a Reply