ThreadLocal in Java

September 15, 2009

ThreadLocal is one of the rarely used class in Java. I found it is one of the powerful class in Java on multi-threaded programming. In multi threaded program generally we use session object to track the current users information.

These information is passed to various method to retrieve desired value. For example in Struts execute method passes HttpServletRequest and HttpServletResponse, what if we want the instance of ServletContext? we have to change the method signatore to pass ServletContext. One can use ThreadLocal to keep certain objects/values available throught the thread execution.

ThreadLocal object is not required on day to day programming unlike ArrayList or HashMap. But it is good choice to solve a few problems at framework level. In one of my recent project I have used ThreadLocal to expose user specific information like User info, UserId, Roles and access, etc.

The main reason is I want hide the code which identifies these values so that in future it is easy to change the behavior and fine tune the code across the application.


struts with tiles

April 4, 2008

This post describes a way I figured out how to integrate Struts 2 with tiles. Struts 2 provides a plugin for integrating tiles 2. This plugin is included in the complete bundle (struts-2.x.x.x-all.zip). The following are are the steps needed to integrate struts2 with tiles.

  1. Download the struts complete bundle from the struts 2 website
  2. Download tiles 2 from tiles 2 website
  3. Download the tiles dependencies from the jakarta commons site
    • Commons BeanUtils 1.7.0 or above
    • Commons Digester 1.8 or above
    • Commons Logging 1.1 or above
  4. Create the layout page, and related files (except the layout, all the other files are basic jsps )

<%@ taglib uri=“http://tiles.apache.org/tags-tiles” prefix=“tiles” %>

<!DOCTYPE html PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN” “http://www.w3.org/TR/html4/loose.dtd”>

<html>

<head>

<meta http-equiv=“Content-Type” content=“text/html; charset=ISO-8859-1″>

<title>Insert title here</title>

</head>

<body>

<table width=“100%” height=“100%”>

      <tr height=“20%”>

            <td colspan=“2″ align=”centerbgcolor=“skyblue”><tiles:insertAttribute

                  name=“header” /></td>

      </tr>

      <tr>

            <td bgcolor=“cyan” width=“75%”><tiles:insertAttribute name=“body” /></td>

      </tr>

      <tr height=“20%”>

            <td colspan=“2″ align=”centerbgcolor=“skyblue”><tiles:insertAttribute

                  name=“footer” /></td>

      </tr>

</table>

</body>

</html>

  1. Create the HelloWorld Action class package example; import com.opensymphony.xwork2.ActionSupport; public class HelloWorld extends ActionSupport { public String execute() throws Exception { System.out.println(”Hello World”); return SUCCESS; } }
  2. Configure the Web Deployment descriptor by adding a tiles listener to the web.xml file of your web application.

<?xml version=“1.0″ >

<web-app xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance” xmlns=“http://java.sun.com/xml/ns/javaee” xmlns:web=“http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd” xsi:schemaLocation=“http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd” id=“WebApp_ID” version=“2.5″>

 

  <display-name>tilesTest</display-name>

   <listener>

    <listener-class> org.apache.struts2.tiles.StrutsTilesListener </listener-class>

    </listener>

 

 

     <filter>

      <filter-name>struts2</filter-name>

      <filter-class> org.apache.struts2.dispatcher.FilterDispatcher </filter-class>

       </filter>

      

      <filter-mapping>

      <filter-name>struts2</filter-name>

       <url-pattern>/*</url-pattern>

       </filter-mapping>

      

       <welcome-file-list>

       <welcome-file>index.html</welcome-file>

       </welcome-file-list>

</web-app>

 

  1. Configure struts to work with tiles, this can be done by either

1.      Extending the sturts package from “tiles-default” <package name=”tilesTest” extends=”tiles-default”>OR

2.      Declaring a new “result-type”, tiles, that will map to “org.apache.struts2.views.tiles.TilesResult” <result-types> <result-type name=”tiles” class=”org.apache.struts2.views.tiles.TilesResult” /> </result-types>

  1. Set the type of the results in the package to “tiles” <result name=”success” type=”tiles”>helloworld.home</result>struts.xml

<?xml version=”1.0″ encoding=”UTF-8″ ?>

<!DOCTYPE struts PUBLIC “-//Apache Software Foundation//DTD Struts Configuration 2.0//EN” “http://struts.apache.org/dtds/struts-2.0.dtd”>

<struts>

      <package name=”tilesTest” extends=”struts-default”>

            <result-types>

                  <result-type name=”tiles”

                        class=”org.apache.struts2.views.tiles.TilesResult” />

            </result-types>

            <action name=”helloWorld” class=”example.HelloWorld”>

                  <result name=”success” type=”tiles”>helloworld.home</result>

            </action>

      </package>

</struts>

 

Note that the result “helloword.home” must match the definition name in tiles.xml file.

  1. Create definitions for tiles in WEB-INF/tiles.xml file.

<!DOCTYPE tiles-definitions PUBLIC “-//Apache Software Foundation//DTD Tiles Configuration 2.0//EN” “http://tiles.apache.org/dtds/tiles-config_2_0.dtd”>

<tiles-definitions>

      <definition name=”helloworld.home” template=”/layouts/layout.jsp”>

            <put-attribute name=”header” value=”/layouts/header.jsp” />

            <put-attribute name=”body” value=”/index.html” />

            <put-attribute name=”footer” value=”/layouts/footer.jsp” />

      </definition>

</tiles-definitions>