All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.struts.faces.renderer.ErrorsRenderer Maven / Gradle / Ivy

There is a newer version: 1.5.0-RC2
Show newest version
/*
 * $Id$
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package org.apache.struts.faces.renderer;


import java.beans.Beans;
import java.io.IOException;
import java.util.Iterator;
import java.util.Locale;

import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.context.ResponseWriter;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.Globals;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.util.MessageResources;


/**
 * 

Renderer implementation for the errors tag * from the Struts-Faces Integration Library.

* * @version $Rev$ $Date$ */ public class ErrorsRenderer extends AbstractRenderer { // -------------------------------------------------------- Static Variables /** *

The Log instance for this class.

*/ private final static Log LOG = LogFactory.getLog(ErrorsRenderer.class); /** * The dummy message resources for this package. */ protected static MessageResources dummy = MessageResources.getMessageResources ("org.apache.struts.faces.renderer.Dummy"); // ---------------------------------------------------------- Public Methods /** *

Render a combination of error messages from JavaServer Faces * Validators, and Struts messages from form bean * validate() methods and corresponding business logic * error checks.

* * @param context FacesContext for the request we are processing * @param component UIComponent to be rendered * * @exception IOException if an input/output error occurs while rendering * @exception NullPointerException if context * or component is null */ public void encodeEnd(FacesContext context, UIComponent component) throws IOException { if ((context == null) || (component == null)) { throw new NullPointerException(); } if (LOG.isDebugEnabled()) { LOG.debug("encodeEnd() started"); } // Look up availability of our predefined resource keys MessageResources resources = resources(context, component); if (Beans.isDesignTime() && (resources == null)) { resources = dummy; } Locale locale = context.getViewRoot().getLocale(); boolean headerPresent = resources.isPresent(locale, "errors.header"); boolean footerPresent = resources.isPresent(locale, "errors.footer"); boolean prefixPresent = resources.isPresent(locale, "errors.prefix"); boolean suffixPresent = resources.isPresent(locale, "errors.suffix"); // Set up to render the error messages appropriately boolean headerDone = false; ResponseWriter writer = context.getResponseWriter(); String id = component.getId(); String property = (String) component.getAttributes().get("property"); if (id != null) { writer.startElement("span", component); if (id != null) { writer.writeAttribute("id", component.getClientId(context), "id"); } } // Render any JavaServer Faces messages Iterator messages = context.getMessages(property); while (messages.hasNext()) { FacesMessage message = (FacesMessage) messages.next(); if (LOG.isTraceEnabled()) { LOG.trace("Processing FacesMessage: " + message.getSummary()); } if (!headerDone) { if (headerPresent) { writer.write (resources.getMessage(locale, "errors.header")); } headerDone = true; } if (prefixPresent) { writer.write(resources.getMessage(locale, "errors.prefix")); } writer.write(message.getSummary()); if (suffixPresent) { writer.write(resources.getMessage(locale, "errors.suffix")); } } // Render any Struts messages ActionMessages errors = (ActionMessages) context.getExternalContext().getRequestMap().get (Globals.ERROR_KEY); if (errors != null) { if (LOG.isTraceEnabled()) { LOG.trace("Processing Struts messages for property '" + property + "'"); } Iterator reports = null; if (property == null) { reports = errors.get(); } else { reports = errors.get(property); } while (reports.hasNext()) { ActionMessage report = reports.next(); if (LOG.isTraceEnabled()) { LOG.trace("Processing Struts message key='" + report.getKey() + "'"); } if (!headerDone) { writer = context.getResponseWriter(); if (headerPresent) { writer.write (resources.getMessage(locale, "errors.header")); } headerDone = true; } if (prefixPresent) { writer.write (resources.getMessage(locale, "errors.prefix")); } writer.write(resources.getMessage(locale, report.getKey(), report.getValues())); if (suffixPresent) { writer.write (resources.getMessage(locale, "errors.suffix")); } } } // Append the list footer if needed if (headerDone && footerPresent) { writer.write(resources.getMessage(locale, "errors.footer")); } if (id != null) { writer.endElement("span"); } if (LOG.isDebugEnabled()) { LOG.debug("encodeEnd() finished"); } } // ------------------------------------------------------ Protected Methods /** *

Return the MessageResources bundle from which * we should return any Struts based error messages. If no such * bundle can be located, return null.

* * @param context FacesContext for the request we are processing * @param component UIComponent to be rendered */ protected MessageResources resources(FacesContext context, UIComponent component) { String bundle = (String) component.getAttributes().get("bundle"); if (bundle == null) { bundle = Globals.MESSAGES_KEY; } return ((MessageResources) context.getExternalContext().getApplicationMap().get(bundle)); } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy