logo
Home j4j Tag Library

j4j:param - Form Parameter Tag


Description

j4j:param tag allows to define and pass parameters to the next page. In contrast with the standard f:param tag, that can be defined only for the h:commandLink tag, j4j:param is defined for the h:form itself. It allows to pass parameters with using any form command control.

j4j:param has the following attribute:

  • id - This attribute works the same way as any JSF tag. The JSF implementation assigns the clientId for this attribute when it is rendered;
  • sid - Static id. If this attribute is defined, the renderer will generate the static id attribute with the value of the sid attribute;
  • binding - This attribute allows to provide the component binding. The property of the backing bean should have org.j4j.components.UIParam type;
  • rendered - If this attribute equals to true, the tag will be rendered, otherwize it will not;
  • name - This attribute is rendered as a parameter name;
  • value - This attribute is rendered as a parameter value;
  • method - This attribute defines the way how parameter will be passed to the next page. If the attribute equals to get, the name-value pair will be added to the end of the request URL, otherwize it will be passed as a hidden field value. Note: this attribute does not define the method for the form itself;

The attributes except id, rendered and binding, allows to use JSP scriplet or/and JSF 2.0 EL. The rendered and binding attributes allow only JSF EL.

How to Download

Download the compiled code using this URL: j4j.jar (6.5 K)

How to Use

1. Put the j4j.jar file in the WEB-INF/lib folder of your project.

2. Add the following declaration at the top of the page:

<%@ taglib uri="http://javascript4jsf.dev.java.net/" prefix="j4j" %>

3. Add the j4j:param component as a child of the JSF form element. For example:

  <h:form id="userForm">
  	....
	<j4j:param name="admin" value="yes"/>
	....
  </h:form>

Source Code

Here is the content of faces-config.xml referencing the proxy component:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE faces-config PUBLIC "-//Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.0//EN"
                              "http://java.sun.com/dtd/web-facesconfig_1_0.dtd">
<faces-config>
 <component>
  <component-type>org.j4j.param</component-type>
  <component-class>org.j4j.components.UIParam</component-class>
 </component>
  <!--
 .....
 other components desclarations
 .....
 -->
 <lifecycle/>
 <application>
  <locale-config/>
 </application>
 <factory/>
</faces-config>

Here is the content of the j4j.tld file:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"
                        "http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd">
<taglib>
 <tlib-version>0.3</tlib-version>
 <jsp-version>1.2</jsp-version>
 <short-name>j4j</short-name>
 <uri>http://javascript4jsf.dev.java.net/</uri>
 <!--
 .....
 other tags desclarations
 .....
 -->
  <tag>
  <name>param</name>
  <tag-class>org.j4j.tags.ParamTag</tag-class>
  <body-content>empty</body-content>
  <attribute>
   <name>id</name>
  </attribute>
  <attribute>
   <name>sid</name>
   <rtexprvalue>yes</rtexprvalue>
  </attribute>
  <attribute>
   <name>binding</name>
  </attribute>
  <attribute>
   <name>rendered</name>
   <rtexprvalue>yes</rtexprvalue>
  </attribute>
  <attribute>
   <name>name</name>
   <required>yes</required>
   <rtexprvalue>yes</rtexprvalue>
  </attribute>
  <attribute>
   <name>value</name>
   <rtexprvalue>yes</rtexprvalue>
  </attribute>
  <attribute>
   <name>method</name>
   <rtexprvalue>yes</rtexprvalue>
  </attribute>
 

</taglib>

Here is the source code for the org.j4j.tags.ParamTag class:

package org.j4j.tags;

import javax.faces.application.Application;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.el.ValueBinding;
import javax.faces.webapp.UIComponentTag;


/**
 * @author sim
 *
 */
public class ParamTag extends UIComponentTag {
	
	String name;
	String value;
	String sid;
	String method;
	
	/**
	 * @return Returns the method.
	 */
	public String getMethod() {
		return method;
	}
	/**
	 * @param method The method to set.
	 */
	public void setMethod(String method) {
		this.method = method;
	}
		/**
	 * @return Returns the sid.
	 */
	public String getSid() {
		return sid;
	}
	/**
	 * @param sid The sid to set.
	 */
	public void setSid(String sid) {
		this.sid = sid;
	}
	/**
	 * @return Returns the name.
	 */
	public String getName() {
		return name;
	}
	/**
	 * @param name The name to set.
	 */
	public void setName(String name) {
		this.name = name;
	}
	/**
	 * @return Returns the value.
	 */
	public String getValue() {
		return value;
	}
	/**
	 * @param value The value to set.
	 */
	public void setValue(String value) {
		this.value = value;
	}
	public String getComponentType() {
		return "org.j4j.param";
	}

	public String getRendererType() {
		return null;
	}
	
	protected void setProperties(UIComponent component) {

		super.setProperties(component);
		setString( component , "sid" , sid );
		setString( component , "name" , name );
		setString( component , "value" , value );
		setString( component , "method" , method );
		
	}
	
	 public static void setString(UIComponent component, String attributeName,
			String attributeValue) {
		if (attributeValue == null)
			return;
		if (UIComponentTag.isValueReference(attributeValue))
			setValueBinding(component, attributeName, attributeValue);
		else
			component.getAttributes().put(attributeName, attributeValue);
	}
	 
		public static void setValueBinding(UIComponent component,
				String attributeName, String attributeValue) {
			FacesContext context = FacesContext.getCurrentInstance();
			Application app = context.getApplication();
			ValueBinding vb = app.createValueBinding(attributeValue);
			component.setValueBinding(attributeName, vb);
		}

	
}

Here is the source code for the org.j4j.tags.UIParam class:

package org.j4j.components;

import java.io.IOException;

import javax.faces.component.UIOutput;
import javax.faces.context.FacesContext;
import javax.faces.context.ResponseWriter;
import javax.faces.component.UIComponent;
import javax.faces.component.UIViewRoot;
import javax.faces.component.UIForm;


/**
 * @author sim
 *
 */
public class UIParam extends UIOutput {
	
	
	public void encodeBegin(FacesContext context) throws IOException {
		return;
	}

	public void decode(FacesContext context) {
		return;
	}
	public void encodeEnd(FacesContext context) throws IOException {
		ResponseWriter writer = context.getResponseWriter();
	    String name = (String)getAttributes().get("name");
		UIComponent actionComponent=super.getParent();
		String acId = actionComponent.getClientId(context); 

		UIForm form= getForm(actionComponent);
		if (form != null) {
			
		    String method = (String)getAttributes().get("method");
		    String sid = (String)getAttributes().get("sid");
		    String value = (String)getAttributes().get("value");
		    if (value==null) value="";
		    
			if (sid!=null) {
		    	writer.writeAttribute("id", sid, "sid");
			} else {
			    if (shouldWriteIdAttribute(this))
			    	writer.writeAttribute("id", this.getClientId(context), "id");
			}
		    
		    
		    if (method == null) method="post";
			if ("get".equalsIgnoreCase(method)) {
				String formId = form.getClientId(context);

				writer.startElement("script", null);

				writer.write("var f = document.forms['"+formId+"'];\n");
				writer.write("var faction=f.action;\n");
				writer.write("faction=(faction.indexOf('?')>-1?faction+'&':faction+'?');\n");
				
				writer.write("faction=faction+'"+name+"="+value+"';\n");
				writer.write("f.action=faction;\n");
				writer.endElement("script");

			} else {
				writer.startElement("input", null);
				
				writer.writeAttribute("type", "hidden", "");
				setStringAttribute( context , "name" , "name" );
				setStringAttribute( context , "value" , "value" );
			}
		} else {
			throw new NullPointerException("Param tag with name "+name+" must be declared inside the form.");
		}
	
	}
	
	
    private UIForm getForm(UIComponent component) {
        while (component != null) {
            if (component instanceof UIForm) {
                break;
            }
            component = component.getParent();
        }
        return (UIForm) component;
    }

	
	public void setStringAttribute(FacesContext context, String htmlTag,
			String cTag) throws IOException {
		String value = (String)getAttributes().get(cTag);
		if (value!=null) {
			ResponseWriter writer = context.getResponseWriter();
			writer.writeAttribute(htmlTag, value, null);
		}
	}

	
    protected boolean shouldWriteIdAttribute(UIComponent component) {
        String id;
        return (null != (id = component.getId()) &&
            !id.startsWith(UIViewRoot.UNIQUE_ID_PREFIX));
    }

}

Example

Exadel JSF Studio (Eclipse) project URL: j4j-testApp.zip (1.68M)

Ready-to-deploy war file URL: j4j-testApp.war(1.68M)

See Also:

Proxy Id for a JSF Custom Component


This component allows you to get the dynamic id for any of the other JSF components.

j4j:defaultAction - Default Action for a JSF Form


The component allows you to define the default action for a form by adding <j4j:defaultAction /> as a child tag to the commandButton tag.