UTF-8, JSP and Spring MVC

If you are using UTF-8 with your Spring web app, then I would suggest using the following configurations.

In web.xml add:

<jsp-config>
    <jsp-property-group>
        <url-pattern>*.jsp</url-pattern>
        <page-encoding>UTF-8</page-encoding>
    </jsp-property-group>
</jsp-config>

(so that you don’t have to add <%@ page pageEncoding="utf-8" %> to every jsp file) and

<filter>
    <!-- This filter has to come before other filters. -->
    <filter-name>characterEncodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
        <param-name>encoding</param-name>
        <param-value>UTF-8</param-value>
    </init-param>
</filter>

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

And if you are using ResourceBundleMessageSource you need to set defaultEncoding in you Spring Servlet config:

<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
    <property name="basename" value="messages" />
    <property name="defaultEncoding" value="UTF-8" />
</bean>

Note that if you are not using JDK 1.6 or above you have to use ReloadableResourceBundleMessageSource instead of ResourceBundleMessageSource to be able to set defaultEncoding.

Now, if you remember to save all files using UTF-8, you should be fine.

box-sizing: border-box and IE7

Internet Explorer 7 doesn’t support box-sizing: border-box. You can, however, achieve the same thing with nested divs.

Replace

<div class="border-box">

    ... content ...

</div>
.border-box {
    box-sizing: border-box;
    width: 50%;
    font-size: 50%;
    margin-bottom: 0.5em;
    background: green;
    padding: 5em;
    border: 5px solid red;
    overflow: hidden;
}

with

<div class="border-boxish">
    <div class="border-boxish-inner">

        ... content ...

    </div>
</div>
.border-boxish {
    width: 50%;
    font-size: 50%;
    margin-bottom: 0.5em;
    background: green;
}

.border-boxish-inner {
    padding: 5em;
    border: 5px solid red;
    overflow: hidden;
}

You don’t need to use all css-rules from the example. I just included them to show what goes where.

Gradle Selenium (WebDriver) task

If you want to run your Selenium (WebDriver) tests from Gradle you can do it like this:

1. Add Jetty plugin to build.gradle:

apply plugin: 'jetty'

2. Create a src/selenium/java folder in you project root.

3. Add a SourceSet to build.gradle:

sourceSets {
    selenium
}

4. Add Selenium dependencies to build.gradle:

seleniumCompile 'junit:junit:4.11'
seleniumCompile 'org.seleniumhq.selenium:selenium-java:2.30.0'

Add jettyDaemon and selenium tasks to build.gradle:

task jettyDaemon(type: org.gradle.api.plugins.jetty.JettyRun) {
    daemon = true
}

task selenium(type: Test, dependsOn: jettyDaemon) {
    testClassesDir = sourceSets.selenium.output.classesDir
    classpath = sourceSets.selenium.runtimeClasspath
}

5. If you are using Eclipse, add Selenium dependencies to the Eclipse classpath in build.gradle:

eclipse {
    classpath {
        plusConfigurations += configurations.seleniumCompile
    }
}

You can now place you Selenium tests in src/selenium/java and run:

gradle clean selenium

For a complete example check out https://github.com/shagstrom/spring-mvc-hibernate-skeleton .

JavaFX and Groovy Exception

You might have tried out the JavaFX Hello World example, from Oracle’s Getting Started with JavaFX guide, using Groovy.

You will then get the following exception:


Exception in thread "main" java.lang.RuntimeException: Error: class sun.reflect.NativeMethodAccessorImpl is not a subclass of javafx.application.Application

In order to fix this, change

launch(args);

to

launch(HelloWorld.class, args);

The example now runs fine with Groovy.

SQL and alpha-numeric sort order

Say you have this data set:

my_table
some_column
abc1
abc2
abc10
def1abc1
def2abc1
def10abc1
def10abc2
def10abc10

If you now do:

SELECT * FROM my_table ORDER BY some_column

you will get the following result:

some_column
abc1
abc10
abc2
def10abc1
def10abc10
def10abc2
def1abc1
def2abc1

which might not be what you want.

Now, if you create the following funtion:

CREATE FUNCTION pad_numbers(text) RETURNS text AS $$
  SELECT regexp_replace(regexp_replace(regexp_replace(regexp_replace($1,
    E'(^|\\D)(\\d{1,3}($|\\D))', E'\\1000\\2', 'g'),
      E'(^|\\D)(\\d{4,6}($|\\D))', E'\\1000\\2', 'g'),
        E'(^|\\D)(\\d{7}($|\\D))', E'\\100\\2', 'g'),
          E'(^|\\D)(\\d{8}($|\\D))', E'\\10\\2', 'g');
$$ LANGUAGE SQL;

you can use this query:

SELECT * FROM my_table ORDER BY pad_numbers(some_column)

with this result:

some_column
abc1
abc2
abc10
def1abc1
def2abc1
def10abc1
def10abc2
def10abc10

Now you got your data sorted alpha-numerically.

Note: I have used PostgreSQL 9.0.

Split Pane jQuery plugin

I have created a jQuery plugin for adding split panes to you web site or web application.

Split panes can be nested. Component min-widths and min-heights are supported. I have not implemented support for max-width and max-height, yet.

I’ve tested the code in the latest versions of Chrome, Firefox and Safari on OS X, and in IE8 on XP.

The source code can be found at github.

It’s only version 0.3, so there will probably be some changes in the near future. Please give me you feed back!

Check out the following pages for examples on how to use it:

N.B., not all markup in the examples are needed for setting up the split panes. <div class="split-pane-frame"> and <div class="decoration"> are used for styling purposes only. Feel free to use them, but you might want to style things your own way.

JMenuBar not working for JApplet in Firefox on Mac

I’m having this issue with Firefox 12.0 on Mac OS X 10.7.4 using the following code:

import javax.swing.*;

public class AppletWithMenuBar extends JApplet {
	public void init() {
		JMenuBar menuBar = new JMenuBar();

		JMenu fileMenu = menuBar.add(new JMenu("File"));
		fileMenu.add(new JMenuItem("New"));
		fileMenu.add(new JMenuItem("Open"));
		fileMenu.add(new JMenuItem("Save"));

		JMenu editMenu = menuBar.add(new JMenu("Edit"));
		editMenu.add(new JMenuItem("Cut"));
		editMenu.add(new JMenuItem("Copy"));
		editMenu.add(new JMenuItem("Paste"));

		setJMenuBar(menuBar);
	}
}

and

<applet code="AppletWithMenuBar.class" width="400" height="100"></applet>

Whenever I select an item from the “Edit” menu the “File” menu gets highlighted.

Check it out for yourself here or have a look at the videos below.

This issue is also discussed here.

rApache on Debian 6.0 squeeze or Ubuntu 10.04 Lucid Lynx

This is how I installed and configured rApache on Debian 6.0 (squeeze). It should work on Ubuntu 10.04 Lucid Lynx also.

Install R and Apache MPM prefork (I assume that Apache 2 is installed already):

> sudo aptitude install r-base r-base-dev apache2-mpm-prefork apache2-prefork-dev

Download and build rApache from source:

> cd /usr/local/src
> sudo wget http://biostat.mc.vanderbilt.edu/rapache/rapache-1.1.17.tar.gz
> sudo tar xzf rapache-1.1.17.tar.gz
> cd rapache-1.1.17
> sudo ./configure
> sudo make
> sudo make install

Create file /etc/apache2/mods-available/R.load with content:

LoadModule R_module /usr/lib/apache2/modules/mod_R.so

Enable mod_R:

> sudo a2enmod R

Create folder /var/www/r-scripts .

Add the following to /etc/apache2/sites-available/default inside <VirtualHost *:80>:

<VirtualHost *:80>
	...
	<Directory /var/www/r-scripts/>
		SetHandler r-script
		RHandler sys.source
	</Directory>
	...
</Directory>

Created file /var/www/r-scripts/hello.R with the following content:

setContentType(type='text/plain')
cat('Hello')
DONE

Restart Apache:

> sudo /etc/init.d/apache2 restart

Verify that it works:

> curl -s http://localhost/r-scripts/hello.R
Hello

Done.

References:

Apache Server, Tomcat and mod_jk on Debian 6.0 squeeze

This is how I integrated Tomcat 6 with Apache 2 on Debian 6.0 (squeeze) using mod_jk. It should also work on Ubuntu 10.04 Lucid Lynx.

Install Apache Web Server:

> aptitude install apache2

Verify that the server is running:

> curl -s http://localhost | grep "It works"
<html><body><h1>It works!</h1>

Install Tomcat (you don’t need to install tomcat6-examples, I did it only for the purpose of this How To):

> aptitude install tomcat6 tomcat6-examples

Verify that Tomcat server is running:

> curl -s http://localhost:8080 | grep "It works"
<h1>It works !</h1>

Install mod_jk:

> aptitude install libapache2-mod-jk

Create file /etc/apache2/workers.properties with the following content:

worker.list=worker1
worker.worker1.type=ajp13
worker.worker1.host=localhost
worker.worker1.port=8009

Create file /etc/apache2/mods-available/jk.conf with the following content:

JkWorkersFile	/etc/apache2/workers.properties
JkShmFile	/var/log/apache2/mod_jk.shm
JkLogFile	/var/log/apache2/mod_jk.log
JkLogLevel	info

Enable jk conf:

> a2enmod jk

Add mount point to /etc/apache2/sites-available/default:

<VirtualHost *:80>
	...
	JkMount		/examples/* worker1
	...
</VirtualHost>

Uncomment the following line in /etc/tomcat6/server.xml:

<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />

Restart Tomcat:

> /etc/init.d/tomcat6 restart

Restart Apache web server:

> /etc/init.d/apache2 restart

Now verify that everything works:

> curl -s http://localhost/examples/ | grep "Tomcat Examples"
<H3>Apache Tomcat Examples</H3>

Done.

Here is a complete script for setting up Apache 2, Tomcat 6 and mod_jk on Ubuntu 10.04.

References:

MacPorts, Clojure and ClassNotFoundException

After installing Clojure with MacPorts I got the following exception when running a basic Clojure script:

Exception in thread "main" java.lang.NoClassDefFoundError: Pad:/opt/local/share/java/clojure/bin////lib/clojure/jar
Caused by: java.lang.ClassNotFoundException: Pad:.opt.local.share.java.clojure.bin....lib.clojure.jar
	at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
	at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:247)

As it turned out, I had run the script from a folder with a space in it’s name, “Scratch Pad”. After moving the script to an other folder, the script ran just fine.