Archive for the ‘Java’ Category

J2SE 1.5…Um, 5.0

Monday, July 12th, 2004

Ok, what was going to be Java 2 Platform, Standard Edition version 1.5 is now going to be Standard Edition version 5.0. I didn’t like Sun’s way of naming the versions of their products before, and I don’t like it now.

Everything is still Java version 2 and I think it’d be hard to do a third version since the 2 (J2SE, J2EE) has become very much a part of the brand names. Obviously, the 2 doesn’t have much of a meaning anymore. So, when what everyone expected to be J2SE 1.5 is suddenly 5.0, it just adds to the mess. I wonder who came up with that idea.

Well, let’s leave that aside. Here’s a pretty good article for catching up with what’s new in 5.0 (though the page still says 1.5).

Generics and autoboxing are, in my opinion, good features. The enumerated types and static imports are good as well. I guess one can always question how those features have been implemented, and while they don’t actually bring much new, I believe they will make development faster and easier. I am looking forward to J2SE 5.0.

How to Set Up Struts Tiles

Tuesday, May 4th, 2004

Here is what I’ve found to be the easiest and fastest way of setting up Struts Tiles for a webapp project that doesn’t already use Struts. (If your webapp already uses Struts, just skip the steps for setting up Struts and add the relevant Tiles parts to your current configuration.) For more information about Struts Tiles, please read my entry about Java Layout Frameworks.

  1. Download the latest Struts binary release (please note that these instructions are for version 1.1).
  2. Extract the contents of the downloaded zip/tar.gz file into a directory within your webapp project, e.g. project/lib. This directory will now contain a directory named something like jakarta-struts-1.1. For simplicity, I have renamed it to jakarta-struts.
  3. Add the following to your Ant build.xml file (or equivalent):
    <target name="struts">
    	<copy todir="dist/webapp/WEB-INF">
    		<fileset dir="lib/jakarta-struts">
    			<include name="lib/commons-*.jar" />
    			<include name="lib/struts.jar" />
    		</fileset>
    		<fileset dir="lib/jakarta-struts/lib">
    			<include name="struts-*.tld" />
    		</fileset>
    	</copy>
    </target>

    Change dist/webapp to the directory where the files to be depolyed (i.e. put into a WAR file) are located.

  4. Add the following to your WEB-INF/web.xml file:
    <servlet>
    	<servlet-name>action</servlet-name>
    	<servlet-class>
    		org.apache.struts.action.ActionServlet
    	</servlet-class>
    	<init-param>
    		<param-name>config</param-name>
    		<param-value>
    			/WEB-INF/struts-config.xml
    		</param-value>
    	</init-param>
    	<load-on-startup>1</load-on-startup>
    </servlet>
    
    <taglib>
    	<taglib-uri>
    		/tags/struts-tiles
    	</taglib-uri>
    	<taglib-location>
    		/WEB-INF/struts-tiles.tld
    	</taglib-location>
    </taglib>
  5. Create a file in WEB-INF named struts-config.xml containing the following:
    <!DOCTYPE struts-config PUBLIC
      "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
      "http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd">
    <struts-config>
    	<plug-in className="org.apache.struts.tiles.TilesPlugin">
    		<set-property
    			property="definitions-config"
    			value="/WEB-INF/tiles-defs.xml" />
    		<set-property
    			property="definitions-debug"
    			value="2" />
    		<set-property
    			property="definitions-parser-details"
    			value="2" />
    		<set-property
    			property="definitions-parser-validate"
    			value="true" />
    	</plug-in>
    </struts-config>
  6. Create a file in WEB-INF named tiles-defs.xml containing the following:
    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE tiles-definitions PUBLIC
      "-//Apache Software Foundation//DTD Tiles Configuration 1.1//EN"
      "http://jakarta.apache.org/struts/dtds/tiles-config_1_1.dtd">
    <tiles-definitions>
    	<definition name="default" path="/layout.jsp">
    		<put name="title" value="" type="string" />
    		<put name="body" value="" type="string" />
    		<!-- Insert more attributes here as
    		     appropriate for your site -->
    	</definition>
    </tiles-definitions>

    Here’s a good reference of how to use the Struts Tiles JSP tags and XML elements (the JSP tags and the elements in the tiles-defs.xml file work basically the same way).

That’s pretty much it!

To get you started with using Tiles in your JSP files, here’s an example. Create a file named index.jsp, containing this:

<%@ page pageEncoding="UTF-8"
	contentType="text/html; charset=UTF-8" %>
<%@ taglib uri="/tags/struts-tiles" prefix="tiles" %>
<tiles:insert definition="default" flush="true">
	<tiles:put name="title" type="string" value="Home" />
	<tiles:put name="body" value="/index-content.jsp" />
</tiles:insert>

Then create a file named index-content.jsp containing:

<%@ page pageEncoding="UTF-8" %>
<p>These are the body-contents of index.jsp.</p>

Finally, create the file used for putting the layout together, layout.jsp, containing:

<%@ page pageEncoding="UTF-8" %>
<%@ taglib uri="/tags/struts-tiles" prefix="tiles" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
  "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US">
	<head>
		<meta http-equiv="Content-Type"
			content="text/html; charset=UTF-8" />
		<meta http-equiv="Content-Language"
			content="en-US" />
		<title>
			Website
			<tiles:insert attribute="title" />
		</title>
	</head>
	<body>
		<tiles:insert attribute="body" />
	</body>
</html>

Now you should be ready to build your project (don’t forget to invoke the struts build-file task) and deploy it to your servlet container. Good luck! If you have any questions or suggestions of how to do this differently, feel free to add a comment.

Java Layout Frameworks for Web Pages

Saturday, April 24th, 2004

To make things easier when developing and maintaining this website, I decided to use a layout framework instead of simply including header and footer files in my JSP files. Sure, header and footer files would have done it as well, but with a layout consisting of a main body content area, sidebar, navigation bar and page footer, where each can differ from page to page, a layout framework helps a lot.

First of all, there’s the important decision of which layout framework to use. There are two well-known Java layout frameworks for web pages: SiteMesh and Struts Tiles.

SiteMesh is mostly about intercepting complete HTML pages to which a layout is then applied. That is, you could create JSP files containing as simple HTML code as possible and, when the page is requested, intercept it and apply your website’s layout using a decorator.

The different parts of a page that can be intercepted and inserted into a layout are quite limited; the HTML <head>, <title> and <body>. In other words, if you want to find the intercepted page’s sidebar and include it into the layout’s sidebar, you won’t be able to do it very easily. Supposedly, SiteMesh also allows inclusion of pages into layouts as panels but there doesn’t seem to be any documentation about it. However, including a small amount of text, e.g. for a footer, would be easy; in the page to be intercepted, enter the text into a <meta> tag and, in the decorator, insert a <decorator:getProperty /> tag where the footer text is to be located, e.g. <decorator:getProperty name=”meta.footer” /> for a <meta> tag with the name “footer”.

Struts Tiles, on the other hand, doesn’t intercept pages at all. Instead, for each page, you create a definition that specifies what content (e.g. a JSP file or a text string) is to be located where in your layout file (typically a JSP file). The location can be anywhere you like—it is named, by yourself, in the layout file and referenced, by the same name, in your page definitions. These definitions can be created in an XML file, in JSP files and in Struts Actions. Also, a definition can extend another definition, overriding some or all of its configuration. All this makes Struts Tiles quite a powerful but simple layout framework.

Here’s what I would do when deciding which layout framework to use: If I were starting from scratch with a relatively simple webapp, I’d go with Struts Tiles. If I were going to apply a layout to an existing webapp or integrating the layout of several webapps, I’d try doing it with SiteMesh. For larger and more complicated webapps it’d be harder—possibly either of these or something else entirely; Struts Tiles basically requires a new definition for each page which can be tough when dealing with a lot of pages and SiteMesh, as I mentioned before, is quite limited in its flexibility.

With the exception of this blog, this website obviously fits into the first category (starting from scratch with a relatively simple webapp). By creating a new theme for the blog and completely adapting it to the website (new JSP files for viewing of entries etc), I pretty much made it fit perfectly into the first category, so Struts Tiles was an easy choice. It was made even easier by the fact that I needed more flexibility than SiteMesh had to offer and that I was thinking about using Struts on my website at some point anyway.

Entries (RSS)