Accesing the configuration from JSP files

When developing J2EE applications it is sometimes useful to access the configuration directly from JSPs. EasyConf provides two jsp tags for this purpose:

  • property: to read a single property object of a component
  • configurationObject: to access the configuration object of a component
Both tags retrieve an object and make it available to the JSP as a variable and as a request parameter (ala bean:define tag). You can later use JSTL, struts bean tags, or similar methods to print or use the object in any desired way.

The tag library definition file (TLD) for this libraries can be found inside the easyconf-${version}.jar file, inside the tld directory.

property tag

Tag attributes

  • id: the name of the variable/attribute which will hold the property value (required)
  • component: the name of the configuraton component from which the property value should be read (required)
  • type: the type of the Java object to which the property shold be converted. It is equivalent to calling the different getters of the ComponentProperties interface. The allowed values are: java.lang.String (the default), java.lang.Double, java.lang.Float, java.lang.Byte, java.math.BigDecimal, java.lang.BigInteger, java.lang.Boolean, java.lang.Short, java.lang.Long and java.lang.String[], java.util.List. (This attribute is optional).
  • selector1, selector2, selector3, selector4, selector5: allow setting up to 5 selectors for choosing the value of the property (optional)
  • defaultValue: the default value if the property does not exist. This default value is only used if the type is java.lang.String. In future versions it will be extended to other types. (optional)

Example (String property)

            
<%@ taglib uri="/WEB-INF/tld/easyconf.tld" prefix="easyconf" %>
<easyconf:property id="advertisement_enabled"
                   component="xpression-ui"
                   property="login.advertisement.enabled"
                   selector1="<%=request.getParameter("show-ad")%>/>
<logic:equals name="advertisement_enabled" value="true"/>
  ...
</logic:equals>
                           
         

Example (java.util.List property)

            
<%@ taglib uri="/WEB-INF/tld/easyconf.tld" prefix="easyconf" %>
<easyconf:property id="companies"
                   component="presentation"
                   property="partner.names"
                   type="java.util.list"
                   %>/>
<logic:iterate id="companyName" name="companies"/>
  ...
</logic:iterate>
                           
         

configurationObject tag

Tag attributes

  • id: the name of the variable/attribute which will hold the configuration object (required)
  • component: the name of the configuraton component from which the XML file should be read to fill the configuration object (required)
  • type: the type of the configuration object POJO (optional, default is java.lang.Object)

Example

            
<%@ taglib uri="/WEB-INF/tld/easyconf.tld" prefix="easyconf" %>

<easyconf:configurationObject id="cmsConfig"
                   component="xpression-cms"
                   type="com.foo.bar.cms.ContentTypeDefinitions"/>
<logic:iterate id="type" name="cmsConfig" property="contentTypes">
  ...
</logic:iterate>                   
                           
         

Taglib URI

It is a good practice when working with J2EE taglibs to use a symbolic URI instead of the real path to the tld file. The URI that you should use to refer the EasyConf taglib is:

				http://easyconf.sourceforge.net/easyconf-tags
			

One way to map this URI to the real location of the tld file is adding the following lines in appropriate place inside the web.xml file:

            
<taglib>
	<taglib-uri>http://easyconf.sourceforge.net/easyconf-tags</taglib-uri>
	<taglib-location>/WEB-INF/tld/easyconf.tld</taglib-location>
</taglib>