RSS Entries RSS
RSS Subscribe by Email

SiteMesh Tutorial with Examples

SiteMesh is a web layout framework for Java.  It differs from from frameworks such as Tiles in that it utilizes the decorator pattern.  For example, you create a number of pages and then you tell SiteMesh that you’d like to add the same header, footer, and menus to each of those pages.  This tutorial will give you a simple example of how SiteMesh can be used to give you a cleaner layout architecture and speed development times.

Start by downloading SiteMesh and adding sitemesh-2.3.jar to your WEB-INF/lib directory.  Then add the SiteMesh filter to your web.xml file like so:

<filter>
    <filter-name>sitemesh<filter-name>
    <filter-class>
        com.opensymphony.module.sitemesh.filter.PageFilter
    </filter-class>
<filter>

<filter-mapping>
    <filter-name>sitemesh<filter-name>
    <url-pattern>/*</url-pattern>
<filter-mapping>

This will call the SiteMesh filter whenever a page on your site is accessed.  Now we’ll need to create a /WEB-INF/decorators.xml file:

<decorators defaultdir="/WEB-INF/decorators">
    <decorator name="main" page="main.jsp">
        <pattern>/WEB-INF/pages/*</pattern>
    </decorator>
<decorators>

This will decorate all of the pages located under /WEB-INF/pages/ with the decorator /WEB-INF/decorators/main.jsp, which we’ll create next:

<%@ taglib prefix="decorator" uri="http://www.opensymphony.com/sitemesh/decorator" %>

<head>
  <title>
    Lumidant.com - <decorator:title default="SiteMesh Tutorial Example" />
  </title>
  <style type="text/css">@import "css/global.css";</style>
  <decorator:head />
  <body>
    <div id="header">
      <h2><a href="http://www.lumidant.com/">Lumidant.com</a> Tutorials</h2>
    </div>
    <div id="content">
      <decorator:body />
    </div>
  </body>
</html>

This decorator does a couple of things.  First off, it create an HTML title tag, which starts as “Lumidant.com - ” and then appends the title of the page that is being decorated.  If that page does not have a title tag, then a default is used to render “Lumidant.com - SiteMesh Tutorial Example”.  It then adds a global style sheet to every page being decorated and appends whatever is in that page’s head tag, which is useful for page-specific JavaScript, etc.  The decorator continues by adding a header to each page reading “Lumidant.com Tutorials” followed by the decorated page’s content.

Pretty cool, right?  If anyone is familiar with a similar decorator framework in PHP, please let me know.

Buy Me a Beer

Comments (3)

Setup and Configure openSUSE 11.0

Let me start off by saying that openSUSE 11.0 is the best Linux distribution I have ever used.  There are some rough edges surrounding KDE 4, but the package management in openSUSE 11.0 makes huge strides over that offered in previous versions.  If you want to get up and running with openSUSE 11.0 then there are likely a few customizations you’ll want to make.

Setup Multimedia

This is a perennial setup step on Linux distributions.  We’ll install the codecs needed to watch DVDs, handle MP3s, etc.  We’ll also setup firefox to be able to handle Windows media streams.

  1. YaST > “Software” > “Software Repositories”
  2. Click “Add”
  3. Select “Community Repositories”
  4. Select “Packman Repository” and “VideoLan Repository”
  5. YaST > “Software” > “Software Management”
  6. Uninstall xine-lib and install libxine1, w32codec-all, libdvdcss, k3b-codecs, and mplayerplug-in
  7. Open Firefox and type “about:config” into the address bar
  8. Right Click > “New” > “String”
  9. Enter “network.protocol-handler.app.mms”
  10. Enter “/opt/kde3/bin/kaffeine” (output of “which kaffeine” at command line)

Install NVIDIA drivers

If you have an NVIDIA card, then you’ll want to install the drivers.

  1. YaST > “Software” > “Software Repositories”
  2. Click “Add”
  3. Select “Community Repositories”
  4. Select “NVIDIA Repository”
  5. YaST > “Software” > “Software Management”
  6. Install “nvidia-gfxGO1-kmp-default”

Install CD ripper and ID3 tagger

For some reason, openSUSE 11.0 no longer ships with KAudioCreator or an ID3 tagger installed by default.  My guess would be that they haven’t been ported to KDE4 yet, but they’re nice to have, so we’ll go ahead and install them anyway.  We’ll also change KAudioCreator’s (stupid) default setting of not looking up CDDB information that hasn’t been cached on the local system.

  1. YaST > “Software” > “Software Repositories”
  2. Click “Add”
  3. Select “Community Repositories”
  4. Select “openSUSE BuildService - KDE:Community”
  5. YaST > “Software” > “Software Management”
  6. Install “kid3″ and “kdemultimedia3-CD”
  7. Open kaudiocreator
  8. Select “Settings” > “Configure KAudioCreator …” > “CDDB”
  9. Set lookup to “Cache and remote”

Upgrade WINE

WINE is continuing to evolve and getting closer every day to reaching maturity.  You’ll likely want the latest version instead of the one that was the latest when openSUSE shipped.

  1. YaST > “Software” > “Software Repositories”
  2. Click “Add”
  3. Select “Community Repositories”
  4. Select “openSUSE BuildService - Wine CVS Builds”
  5. YaST > “Software” > “Software Management”
  6. Do a search for wine and click the check mark until version upgrade is selected

Setup a static IP address

Having a static IP address is very nice when you want to remote desktop to your server or access it in some other way without worrying about what the IP address is.  There may also need to be some configuration done on your router for this one.  Or you may prefer to investigate DHCP reservations if your router supports them.

  1. YaST > “Network Devices” > “Network Settings”
  2. Under “Overview”, select your network card and click “Edit”
  3. Enter your static IP and save it
  4. Select “openSUSE BuildService - Wine CVS Builds”
  5. Under “Hostname/DNS”, enter your DNS servers and hit “Finish”

Setup remote desktop through NX

The two main remote desktop softwares for Linux are VLC and NX.  NX is much faster and KDE’s VLC server, KRfb, is broken openSUSE 11.0.  An NX server ships with openSUSE 11.0, but we want to install at least version 3.0 in order to do desktop sharing.  We’ll also open the SSH (NX is built on top of SSH) port in the firewall so that we can connect from another machine.

  1. Download the NX Linux packages
  2. Run “rpm -iv nxclient-3.1.0-2.i386.rpm”, “rpm -iv nxnode-3.1.0-3.i386.rpm”, and “rpm -iv nxserver-3.1.0-2.i386.rpm”
  3. Run “/usr/NX/scripts/setup/nxserver –install”
  4. Run “/usr/NX/bin/nxserver –keygen”
  5. In your NX client, open “Configure…” > “General” tab > “Key …”
  6. Copy the contents of “/usr/NX/share/keys/default.id_dsa.key” into the key window and save it
  7. Open “/usr/NX/etc/server.cfg”
  8. Change line 563 from ‘EnableSessionShadowingAuthorization = “1″‘ to ‘EnableSessionShadowingAuthorization = “0″‘ which will enable you to select “Shadow” in the client under the “General” tab’s “Desktop” framebox if you’d like to do desktop sharing
  9. YaST > “Security and Users” > “Firewall” > “Allowed Services”
  10. Allow “Secure Shell Server”

Setup Network File Share using Samba

Samba allows you to share files on your computer with others on the network.

  1. YaST > “Software” > “Software Management”
  2. Install “samba” if it is not already installed
  3. YaST > “Network Services” > “Samba Server”
  4. Change sharing settings as you’d like and hit “Finish”
  5. Add a user to Samba by running “smbpasswd -a username” where username is the user you’d like to create.
  6. YaST > “Security and Users” > “Firewall” > “Allowed Services”
  7. Allow “Samba Server”

Buy Me a Beer

Comments

Opening a Windows Command Prompt in Windows Explorer

If you’ve ever used Linux and gotten used to having a command prompt, then going back to the Windows command line is a difficult and frustrating change.  Luckily, there’s an amazing program called PowerShell that can alleviate your stress.

To open a command prompt in any directory in Windows Explorer, follow these steps to add a list item to the context menu:

  • Open up Windows Explorer
  • Click “Tools” -> “Folder Options” -> “File Types”
  • Select the “Folder” file type
  • Click “Advanced” -> “New…”
  • The Action will be displayed on the context.  For example, I entered “PowerShell Here”.
  • Now enter “C:\Windows\system32\WindowsPowerShell\v1.0\powershell.exe  -NoExit -Command Set-Location -LiteralPath ‘%L’” for the program to use or “C:\Windows\system32\cmd.exe” if you don’t have PowerShell installed.

If you want to later remove the entry from your menu and the “Remove” and “Edit…” buttons are grayed out as they were for me then you can open up regedit and delete the corresponding entry from HKEY_CLASSES_ROOT/FOLDER/shell

Buy Me a Beer

Comments

Essential Linux Commands

Getting started on Linux can be challenging.  Largely because the first time user won’t have any idea how to track down potential problems.  The following commands are essential to get additional information about your system when something goes wrong:

  • uname -mr - Shows what kernel version and processor you are running on
  • sudo fdisk -l - Can help you figure out how things are mounted
  • dmesg - Useful for tracking down problems during boot
  • tail -f /var/log/messages - Now run the process giving you problems and you might see helpful error messages

If you’ve got other suggestions, please feel free to comment below.  Thanks!

Buy Me a Beer

Comments

Running Quicken Premier 2008 on Linux with Wine

Wine attempts to create a Windows-compatible layer on top of Linux to allow you to run your favorite programs. Recently, Wine had its 1.0 release and has gotten quite strong when compared to earlier versions.  Quicken works reasonably well with wine-1.1.6 and later.

With wine-1.1.5 and earlier you’ll need to use a WINE override.  That is, you must tell Wine to use the native Windows version of gdiplus.dll:

  • Get a copy of gdiplus.dll ensuring that you adhere to any applicable licenses and put it in ~/.wine/drive_c/windows/system
  • Run winecfg
  • Hit “Add Application…” and browse to “drive_c/Program Files/Quicken/qw.exe”
  • Under the “Libraries” tab add a native override for gdiplus

Unfortunately, Quicken still cannot access the internet and there is no workaround for this since schannel, the library which implements SSL, has not yet been implemented.

If you need better debug logs for filing bugs you can set the WINEDEBUG environment variable to get more detailed output or suppress output that is overwhelming.  For example, you can put the following in your ~/.bashrc file:

export WINEDEBUG=fixme-richedit,trace+secur32

Then run “source ~/.bashrc” to reload the file.

Also, if you’d like to see the debug output scroll by on the screen as well as save it to a file then you can run the following:

wine qw.exe 2>&1 | tee trace.log

Buy Me a Beer

Comments (6)

Open Windows Explorer to New Default Directory

To get Windows Explorer to open to a default directory, you can create a new shortcut and modify the path. For example, the following shortcut path will open a directory of documents on a shared drive using Explorer instead of the normal window:

%SystemRoot%\explorer.exe /e,S:\Documents

Buy Me a Beer

Comments

Easy Java Bean toString() using BeanUtils

I often want to have a String description of my beans for debugging or logging purposes, but hate having to manually concatenate the fields in my class to create a toString() method.  This code snippet using Apache Commons (a.k.a. Jakarta Commons) is very helpful for just such occasions:

	public String toString() {
		try {
			return BeanUtils.describe(this).toString();
		} catch (Exception e) {
			Logger.getLogger(this.getClass()).error("Error converting object to String", e);
		}
		return super.toString();
	}

Buy Me a Beer

Comments

Hosting Ruby on Rails on Shared Hosting at FastDomain

I’ve been playing around with Ruby on Rails lately. I’m using Instant Rails as a development server locally. However, I wanted to try to publish my first Rails app to the world. I have a shared hosting account at FastDomain.com, but was having a lot of trouble getting it to work. The only error message I was given was “Application Error - Rails application failed to start properly” and I couldn’t find any informative logs. I believe that Fast Domain is run by the same people who run the more well-known BlueHost.com, so these instructions should work for that host too.

After awhile, I figured out that my problem was due to the fact that the permissions were not set properly when I FTP’d the app over. So, I deleted the app and ran “rails appname” to generate the basic rails structure. Then I copied over my app, config, and vendor/plugins directories and the relevant portions of the public directory. After that I was up and running. Make sure not to copy the entire public directory, but only the files you’ve created or edited so that you do not change the permissions of the dispatch scripts. You could also copy everything and run “chmod 755″ afterwards to reset the permissions. If you’re getting a similar error and the steps I described did not help then I recommend checking out this page at Hoopla!

At this point I was up and running, but my app was sloooowwww. The solution I found was to modify the .htaccess file to ensure that fcgi was being used instead of cgi. Makes sense since the f in fcgi stands for fast. So, go to the public directory of your app and change “RewriteRule ^(.*)$ dispatch.cgi [QSA,L]” to “RewriteRule ^(.*)$ dispatch.fcgi [QSA,L]“. At this point, when I visited the site, I was shown the script source instead of having it execute. Turns out that FastDomain uses a different fcgi handler so change the first line of the script (”AddHandler fastcgi-script .fcgi”) to “AddHandler fcgid-script .fcgi” and your app will be blazing fast.

The most frustrating problem I experienced is that all of my Rails pages were being cached and static content was not.  This made updates to the site very difficult.  For example, if I updated a Rails page and a JavaScript file on that page then the two would be out of sync.  I haven’t found an easy way to clear the Ruby on Rails cache at BlueHost/FastDomain.  The answer I came up with was to run “ps -ef | grep ruby”, to find the Rails process that was running.  Then look at the pid and use it to kill the process such as “kill 24018″.

Buy Me a Beer

Comments

HTML Parsing Showdown - New Contender Takes Title

One of my first posts was a comparison of HTML parsers. Today I found a particularly challenging document to parse. None of the parsers I had compared earlier were able to handle the malformed HTML in this table where the td elements were prematurely ended. The behavior of Neko and HtmlCleaner made the most sense (while still failing to clean the document) while the output from TagSoup and jTidy was a bit more strange.

However, I noticed that FireBug parsed the document correctly. So I did a bit of research into how I’d be able to use Firefox’s HTML parsing and found a project called Mozilla Parser that had been put together to do just that. Its setup is not quite as nice as the others, but is well documented. Follow the quick start to begin with. Then when you get to the portion where you write actual Java code you may want to follow the example below as it appears the API has been updated since the documentation was posted.

final String BASE_PATH = "C:\\Documents and Settings\\bjm733\\My Documents\\workspace\\MozillaHtmlParser\\";

try {
	File parserLibraryFile = new File(BASE_PATH + "native" + File.separator + "bin" + File.separator + "MozillaParser" + EnviromentController.getSharedLibraryExtension());
	String parseLibrary = parserLibraryFile.getAbsolutePath();
	MozillaParser.init(parseLibrary, BASE_PATH + "mozilla.dist.bin."+EnviromentController.getOperatingSystemName());
	MozillaParser parser = new MozillaParser();
	document = parser.parse("<html><body>hello world</body></html>");
} catch(Exception e) {
	e.printStackTrace();
}

Buy Me a Beer

Comments (1)

Wuala Invites Available

Wuala is a p2p backup and remote storage service.  It gives you 1GB of backup space for free and more when you share disk space with others.  If you have a large amount of data or multimedia to backup then this is a much cheaper alternative to services such as Mozy.  All your data is encrypted and replicated multiple times.  There is a very interesting Google tech talk on the subject which shares some insights into the workings of Wuala.  I found the portion dealing with erasure codes to be particularly interesting.  Contact me if you’d like an invite to Wuala.

Also, you may have noticed the pace of blogging my has slowed.  I’ve been extremely busy recently and expect this to continue for the next month or two.  Nonetheless, I am going to make time to post a series of Struts 2 tutorials based off a presentation I gave a few months ago.

Buy Me a Beer

Comments (2)