Hidding stack traces to users in Struts

When an error happens in a web application a stack trace is shown to the user. How to avoid this and show a beautiful error page while logging internally the error that happened? Use the Struts exception handler.

Add the following code to your struts-config.xml. If you don’t want to show different error messages you only need that exception with type=”java.lang.Exception”. The other ones are to show different error messages using Spring exception hierarchy.

  <global-exceptions>
    <exception key="errors.optimistic.locking.failure" path=".error"
               type="org.springframework.dao.OptimisticLockingFailureException"/>
    <exception key="errors.data.access.resource.failure" path=".error"
               type="org.springframework.dao.DataAccessResourceFailureException"/>
    <exception key="errors.data.retrieval.failure" path=".error"
               type="org.springframework.dao.DataRetrievalFailureException"/>
    <exception key="errors.data.access" path=".error"
               type="org.springframework.dao.DataAccessException"/>
    <exception key="errors.transaction" path=".error"
               type="org.springframework.transaction.TransactionException"/>
    <exception key="errors.unknown" path=".error"
               type="java.lang.Exception"/>
  </global-exceptions>

  • errors.optimistic.locking.failure=The object was already modified
  • errors.data.access.resource.failure=Can’t connect to the database
  • errors.data.retrieval.failure=The object you try to access doesn’t exist
  • errors.data.access=Data access error
  • errors.transaction=Transaction error
  • errors.unknown=Unknown error

If you’re using log4j for logging add to your log4j.properties

log4j.rootLogger=ERROR, stdout
log4j.logger.org.apache.struts.action.ExceptionHandler=DEBUG, stdout

# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

This way the stack traces are logged to standard output (in Tomcat this is logged to logs/stdout.txt)

Planet Apache

Planet Apache is a sindication of blogs from Apache Software Foundation committers (as me). Great people with interesting things to say.

Multiple Message Resources in Struts

I’ve find a good example about using multiple message resources in Struts as if they were only one, this means that you don’t need to specify the bundle in each tag.

For those people out there that use JSTL+Struts, as I do, you know that message resources must be configurated in Struts config file besides JSTL config because Struts Validator needs it.

I’ve tidied up the code for using it in ONess, you can get it in the oness-common-webapp-controller (check ActionServlet and CustomMessageResources in the sources) and the configuration in oness-common-webapp-view, you need to change the action servlet in web.xml to use the customized one and in struts-config.xml specify the message resources separated by commas.

Now I need something similar to be used in JSTL, I think that Spring has some support for that.

Maven OR Ant? Maven AND Ant?

There is too much controversy of moving from Ant to Maven, but maybe people don’t know that ant build scripts can be used with maven.

These are the step by step instructions:

  1. Create a minimal project.xml, that we’ll call POM (Project Object Model) or project descriptor. The most tedious task will be adding the dependencies needed to compile your classes, but you can use this search tool to get groupId and artifactId for them. If any of that dependencies are not at ibiblio repository copy the url of the repo, you’ll need it later. Change sourceDirectory to suit your needs.
    
    
        3
        yourgroup
        yourjar
        
            
            
                hibernate
                hibernate
                2.1.6
            
            
                springframework
                spring-core
                1.1
            
            
        
        
            src/java
        
    
    
  2. Create a file maven.xml. This is the place to write ant scripts. You just have to rename targets to goals. You can get the current dir using ${basedir}.
    <?xml version="1.0" encoding="ISO-8859-1"?>
    <project
      xmlns:j="jelly:core"
      xmlns:ant="jelly:ant"
      xmlns:maven="jelly:maven"
      xmlns:util="jelly:util">
      <goal name="doSomething">
        <!-- do some stuff using ant scripts -->
        <delete dir="${basedir}/adir"/>
        <mkdir dir="${basedir}/adir" />
        <copy todir="${basedir}/adir">
          <fileset dir="${basedir}/anotherdir"/>
        </copy>
      </goal>
    </project>
    
  3. If you use some dependency not at ibiblio create a project.properties file with a line maven.repo.remote=http://www.ibiblio.org/maven,http://cvs.apache.org/repository where http://cvs.apache.org/repository is the url you copied early. You can add as much urls as you want, separated by commas.
  4. Now you can run maven doSomething to execute the ant goal, and you can also call maven java:compile to compile the classes, maven site to generate a good looking website,…
  5. A common complain of Ant users is speed. You can call maven doSomething java:compile to run both goals or use maven console, where ypu can type any goal without the startup penalty time.
  6. Now you should be ready to check maven reference docs to explore the lots of features that maven can offer.

To sum up you can do with Maven everything you can do with Ant and many more things, so why don’t give it a try?

J2SE 5 Tiger final version released

J2SE 5 (Tiger) has been finally released as General Availability.
Download it
Check the new features and enhancements
Tiger Roars: The Release of Java 2 Platform, Standard Edition (J2SE) 5.0, an Interview with Sun Fellow Graham Hamilton

Want to hire an Apache developer?

The Apache Software Foundation, the most respected organization in the open source world, has opened a mailing list to allow posting job offers to the members of the community. Offers can be sent the to jobs (at) apache (dot) org.

Avoid duplicated info in the Spring application context

Some configuration is shared across many projects and duplication must be avoided, e.g. for hibernate configuration this can be achieved using ArrayList for properties of type List and a custom class HibernateProperties for properties of type Properties.

    
        
            
        
        
            
        
        
            
        
    

    
        
            
                ${hibernate.dialect}
                ${hibernate.show_sql}
                ${hibernate.hbm2ddl.auto}
            
        
    

    
        
            
                net/sf/oness/party/model/party/bo/Party.hbm.xml
            
        
    

This way mappingResources can be overriden in each project for their set of classes and hibernateProperties can also be overriden to get values from JNDI as I do in the ONess project.

Avoiding OutOfMemory errors in Maven (part 2)

As Rick Hightower stated in a previous comment, another way to avoid running out of memory is forking the virtual machine. When doing so the MAVEN_OPTS doesn’t affect the new vm, you have to use the maven.junit.jvmargs property.

maven.junit.fork=true
maven.junit.jvmargs=-Xmx256m

Avoiding OutOfMemory errors in Maven

If you are getting out of memory errors when running Maven you can increase the amount of memory used.
You only need to set the environment variable MAVEN_OPTS, e.g. MAVEN_OPTS=-Xmx512m if you want to use 512 MB of memory.

JDO and EJB join forces

Dion wrote an interesting entry about the joining of JDO and EJB. A new spec is being created to provide an unified persistence API for both standard and enterprise java editions using POJOs.