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

org.apache.struts.faces.renderer.FormRenderer 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.io.IOException;
import java.util.Map;

import javax.faces.component.NamingContainer;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.context.ResponseWriter;
import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.Globals;
import org.apache.struts.config.ActionConfig;
import org.apache.struts.config.ModuleConfig;
import org.apache.struts.faces.component.FormComponent;
import org.apache.struts.faces.util.StrutsContext;
import org.apache.struts.faces.util.Utils;


/**
 * 

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

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

The Log instance for this class.

*/ private final static Log LOG = LogFactory.getLog(FormRenderer.class); // ---------------------------------------------------------- Public Methods /** *

Perform setup processing that will be required for decoding the * incoming request.

* * @param context FacesContext for the request we are processing * @param component UIComponent to be processed * * @exception NullPointerException if context * or component is null */ public void decode(FacesContext context, UIComponent component) { if ((context == null) || (component == null)) { throw new NullPointerException(); } String clientId = component.getClientId(context); Map map = context.getExternalContext().getRequestParameterMap(); if (LOG.isDebugEnabled()) { LOG.debug("decode(" + clientId + ") --> " + map.containsKey(clientId)); } component.getAttributes().put ("submitted", map.containsKey(clientId) ? Boolean.TRUE : Boolean.FALSE); } private static String passThrough[] = { "enctype", "method", "onreset", "onsubmit", "style", "target", }; /** *

Render the beginning of an HTML <form> * control.

* * @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 encodeBegin(FacesContext context, UIComponent component) throws IOException { if ((context == null) || (component == null)) { throw new NullPointerException(); } // Calculate and cache the form name FormComponent form = (FormComponent) component; String action = form.getAction(); ModuleConfig moduleConfig = StrutsContext.getModuleConfig(context); ActionConfig actionConfig = moduleConfig.findActionConfig(action); if (actionConfig == null) { throw new IllegalArgumentException("Cannot find action '" + action + "' configuration"); } String beanName = actionConfig.getAttribute(); if (beanName != null) { form.getAttributes().put("beanName", beanName); } // Look up attribute values we need String clientId = component.getClientId(context); if (LOG.isDebugEnabled()) { LOG.debug("encodeBegin(" + clientId + ")"); } String styleClass = Utils.getMapValue(String.class, component.getAttributes(), "styleClass"); // Render the beginning of this form ResponseWriter writer = context.getResponseWriter(); writer.startElement("form", form); writer.writeAttribute("id", clientId, "clientId"); if (beanName != null) { writer.writeAttribute("name", beanName, null); } writer.writeAttribute("action", action(context, component), "action"); if (styleClass != null) { writer.writeAttribute("class", styleClass, "styleClass"); } if (component.getAttributes().get("method") == null) { writer.writeAttribute("method", "post", null); } renderPassThrough(context, component, writer, passThrough); writer.writeText("\n", null); // Add a marker used by our decode() method to note this form is submitted writer.startElement("input", form); writer.writeAttribute("type", "hidden", null); writer.writeAttribute("name", clientId, null); writer.writeAttribute("value", clientId, null); writer.endElement("input"); writer.writeText("\n", null); // Add a transaction token if necessary HttpSession session = (HttpSession) context.getExternalContext().getSession(false); if (session != null) { String token = (String) session.getAttribute(Globals.TRANSACTION_TOKEN_KEY); if (token != null) { writer.startElement("input", form); writer.writeAttribute("type", "hidden", null); writer.writeAttribute ("name", "org.apache.struts.taglib.html.TOKEN", null); writer.writeAttribute("value", token, null); writer.endElement("input"); writer.writeText("\n", null); } } // Create an instance of the form bean if necessary if (component instanceof FormComponent) { ((FormComponent) component).createActionForm(context); } } /** *

Render the ending of an HTML <form> * control.

* * @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(); } String clientId = component.getClientId(context); if (LOG.isDebugEnabled()) { LOG.debug("encodeEnd(" + clientId + ")"); } ResponseWriter writer = context.getResponseWriter(); // Render the hidden variable our decode() method uses to detect submits writer.startElement("input", component); writer.writeAttribute("type", "hidden", null); writer.writeAttribute("name", component.getClientId(context), null); writer.writeAttribute("value", component.getClientId(context), null); writer.endElement("input"); writer.write("\n"); // Write our state information (if necessary) context.getApplication().getViewHandler().writeState(context); // Render the ending of this form writer.endElement("form"); writer.writeText("\n", null); // Render focus JavaScript if requested if (!(component instanceof FormComponent)) { return; } String focus = (String) component.getAttributes().get("focus"); if (focus == null) { return; } String focusIndex = (String) component.getAttributes().get("focusIndex"); writer.writeText("\n", null); FormComponent form = (FormComponent) component; writer.startElement("script", form); writer.writeAttribute("type", "text/javascript", null); if (!isXhtml(component)) { writer.writeAttribute("language", "JavaScript", null); } writer.writeText("\n", null); // if (!isXhtml(component)) { // writer.write("\n"); // } writer.endElement("script"); writer.writeText("\n", null); } // ------------------------------------------------------- Protected Methods /** *

Calculate and return the value to be specified for the * action attribute on the <form> * element to be rendered.

* * @param context FacesContext for the current request * @param component Component being processed */ protected String action(FacesContext context, UIComponent component) { String actionURL = context.getApplication().getViewHandler(). getActionURL(context, context.getViewRoot().getViewId()); if (LOG.isTraceEnabled()) { LOG.trace("getActionURL(" + context.getViewRoot().getViewId() + ") --> " + actionURL); } return (context.getExternalContext().encodeActionURL(actionURL)); } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy