Archive for the ‘Development’ Category

Weblog Added to java.blogs

Tuesday, July 20th, 2004

The Java category (feed) of this weblog is now aggregated on java.blogs. I thought it would be a good idea to add it there, and the referrer statistics seem to prove me right.

Since I added the feed of the Java category, entries like this one won’t show up on java.blogs. After all, I’ve been posting quite a lot of entries unrelated to Java, so I thought that would be the best way to do it.

Anyhow, all of you who’ve come here from java.blogs, and all other new readers as well, welcome!

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.

Entries (RSS)