Archive for the 'Knowledge Base' Category

January 8th, 2010
4:21 pm
Comparing files in Eclipse

Posted under Eclipse
Tags ,

Eclipse can compare  a pair of open files. It is not obvious how to do this however.

All you need to do is select the first file in the project/package explorer, then ctrl/select the second file so that both are selected. If you then right click, you can select Compare To/Each Other and a new differences view is created.

You can also select a single file, and compare to another version in CVS by selecting Compare To/Another Branch or Version

This post also discusses the technique.

No Comments »

January 8th, 2010
4:20 pm
Adding jsf view state to domain objects

Posted under JSF
Tags , ,

A classic example of this is the need to add a selected flag to each row if you are using row selection with an ICEfaces table.

The fundamental point here is that you do not want to pollute the domain objects with state that is specific to the view – in this case, the flag is merely a convenience for supporting the user interface.

This post details a good solution to this problem, which is to use the decorator pattern to decorate the row domain objects with a decorator containing the row selected flag. Often, a decorator has an identical interface to its decorated object, but that is not always the case, and in this case we actually need to add a property. This does however mean that you have to be aware of which class you are using at a given point in the code, and you cannot use a generic decorator interface to declare the concrete decorators if they have new properties added. Also, if you have a number of such decorators, you cannot wrap the domain objects with them transparently in turn for the same reason. You can get around some of this with some clever use of reflection, but it does complicate matters and impact performance.

The ICEfaces page on row selection may be found here.

No Comments »

January 8th, 2010
3:25 pm
JSF: #{…} is not allowed in template text

Posted under JSF
Tags , , , , ,

This error can occur when loading a JSF/Facelets page :-

org.apache.jasper.JasperException: /home.jspx(16,17)
   #{...} is not allowed in template text

One reason for this, described on JavaRanch here, is that the JSF page is not being routed properly via the servlet mapping in web.xml. The following extract from web.xml shows a mapping for .iface, to route urls ending in .iface to the ICEfaces Persistent Faces Servlet:-

    <servlet-mapping>
        <servlet-name>Persistent Faces Servlet</servlet-name>
        <url-pattern>*.iface</url-pattern>
    </servlet-mapping>

Even though the page itself may be a .jspx file, using .jspx rather than .iface in the url would not route the page to the above servlet, causing it to be parsed incorrectly. This post describes the cause in more detail – the #{…} syntax is unified EL, which is not allowed in template text in a JSP (hence the error). In the above case the page was being treated as a JSP rather than a Facelet due to the incorrect routing.

No Comments »

January 8th, 2010
11:28 am
Using Logging with the Eclipse Tomcat Server Adapter

Posted under Eclipse
Tags , , ,

This post continues from my previous post on using the Tomcat Server Adapter, and details how to enable and configure logging. Configuring this is not obvious, and there are a number of issues :-

  1. Eclipse does not fully enable logging for the Tomcat Adapter by default, so you will likely find application log calls not going anywhere, and any logging.properties file you set up won’t have any effect.
  2. The Tomcat Adapter ignores any logging.properties file placed in WEB-INF\lib completely. You can only use one in the <Cataline  Base>\conf directory. More is said on the actual location of this directory later.
  3. The configuration instructions are somewhat buried in the Eclipse WTP FAQ.

To enable logging for the Server Adapter, see here in the Eclipse WTP FAQ, details copied as follows. Note that the same procedure works on Eclipse Galileo 3.5.1 and Tomcat 6.0.24 :-

How do I enable the JULI logging in a Tomcat 5.5 Server instance?

Tomcat 5.5 comes with an enhanced implementation of java.util.logging, called JULI, which is configured by default in a standard Tomcat 5.5 installation. This JULI logging configuration is not picked up automatically when creating a new Tomcat 5.5 server in WTP. Some manual steps are necessary to add this configuration to your WTP Tomcat 5.5 server.

  1. Open the server editor for the Tomcat server and note the folder specified by the Configuration path field.
  2. Import the logging.properties file from the conf directory of your Tomcat 5.5 installation into this folder in your workspace.
  3. In the server editor, click on the Open launch configuration link and in the launch configuration Properties dialog, switch to the Arguments tab.
  4. In the VM Arguments field, add the following two system properties substituting the catalina.base path where noted:
    -Djava.util.logging.config.file=”<put catalina.base path here>/conf/logging.properties”
    -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager

The imported logging.properties file can be used to control the JULI logging configuration for the Tomcat server.

Note that in my case I had set up the adapter to use the Workspace Metatdata so that the installed Tomcat server could also be used independently. Therefore, the cataline base path used was the configuration directory used by eclipse, listed as the configuration path on the Server Overview screen (double click the server to see this screen). My settings for the VM arguments were therefore as follows :-

-Djava.util.logging.config.file=
   "E:\Java\Dev\Workspaces\Sentry1\Servers\
     Tomcat v6.0 Server at localhost-config\logging.properties"
-Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager

This allowed me to expand the Server node for the server in the package explorer, and open the logging.properties file directly from there.

The following example logging.properties file illustrates the configuration of the logging. Tomcat uses JULI, an extended version of java.util.logging which allows extra features. It has the abilility to support per-application logging  – note that as above this is not supported in the Eclipse Server Adapter version, although for development time debugging this is not an issue, as you can have separate Tomcat configuration per Eclipse workspace if you store the Tomcat configuration in the Workspace metadata as above. JULI also allows multiple handlers to be loaded from the same class instance. See here for the official Tomcat documentation for configuring Tomcat logging.

The following example logging.properties file illustrates some configuration points :-

A custom console handler has been added to the handlers property at the top of the file, to allow uk.co.salientsoft to have finer grained logging than the default console logger. The default handler has a level of FINE, which would prevent other lower level loggers from using FINER or FINEST. Adding the additional handler allows all the existing logging to remain as is, but gives full flexibility to uk.co.salientsoft logging :-

handlers = 1catalina.org.apache.juli.FileHandler, \
           2localhost.org.apache.juli.FileHandler, \
           3manager.org.apache.juli.FileHandler, \
           4host-manager.org.apache.juli.FileHandler, \
           java.util.logging.ConsoleHandler, \
           5salientsoft.java.util.logging.ConsoleHandler

The new console handler is used to configure logging at FINEST level at the bottom of the file :-

5salientsoft.java.util.logging.ConsoleHandler.level = FINEST
5salientsoft.java.util.logging.ConsoleHandler.formatter = \
   java.util.logging.SimpleFormatter
uk.co.salientsoft.handlers = \
   1catalina.org.apache.juli.FileHandler, \
   5salientsoft.java.util.logging.ConsoleHandler
uk.co.salientsoft.level = FINEST

The following is a complete listing of the sample logging.properties file :-

# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License.  You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

handlers = 1catalina.org.apache.juli.FileHandler, \
           2localhost.org.apache.juli.FileHandler, \
           3manager.org.apache.juli.FileHandler, \
           4host-manager.org.apache.juli.FileHandler, \
           java.util.logging.ConsoleHandler, \
           5salientsoft.java.util.logging.ConsoleHandler

.handlers = 1catalina.org.apache.juli.FileHandler, \
            java.util.logging.ConsoleHandler

############################################################
# Handler specific properties.
# Describes specific configuration info for Handlers.
############################################################

1catalina.org.apache.juli.FileHandler.level = FINE
1catalina.org.apache.juli.FileHandler.directory = ${catalina.base}/logs
1catalina.org.apache.juli.FileHandler.prefix = catalina.

2localhost.org.apache.juli.FileHandler.level = FINE
2localhost.org.apache.juli.FileHandler.directory = ${catalina.base}/logs
2localhost.org.apache.juli.FileHandler.prefix = localhost.

3manager.org.apache.juli.FileHandler.level = FINE
3manager.org.apache.juli.FileHandler.directory = ${catalina.base}/logs
3manager.org.apache.juli.FileHandler.prefix = manager.

4host-manager.org.apache.juli.FileHandler.level = FINE
4host-manager.org.apache.juli.FileHandler.directory = ${catalina.base}/logs
4host-manager.org.apache.juli.FileHandler.prefix = host-manager.

java.util.logging.ConsoleHandler.level = FINE
java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter

############################################################
# Facility specific properties.
# Provides extra control for each logger.
############################################################

org.apache.catalina.core.ContainerBase.[Catalina].[localhost].level = INFO
org.apache.catalina.core.ContainerBase.[Catalina].[localhost].handlers = \
   2localhost.org.apache.juli.FileHandler

org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/manager].level = INFO
org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/manager].handlers = \
   3manager.org.apache.juli.FileHandler

org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/host-manager].level = \
   INFO
org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/host-manager].handlers = \
   4host-manager.org.apache.juli.FileHandler

5salientsoft.java.util.logging.ConsoleHandler.level = FINEST
5salientsoft.java.util.logging.ConsoleHandler.formatter = \
   java.util.logging.SimpleFormatter
uk.co.salientsoft.handlers = 1catalina.org.apache.juli.FileHandler, \
                             5salientsoft.java.util.logging.ConsoleHandler
uk.co.salientsoft.level = FINEST

No Comments »

January 8th, 2010
10:42 am
ICEfaces facelets strips DOCTYPE, puts IE in quirks mode

Posted under JSF
Tags , , , , ,

By default when you create a new ICEfaces Facelets project with a sample page, it codes the DOCTYPE directive directly. Unfortunately, this is stripped out during processing/rendering and does not  appear in the final HTML for the page. When running IE without a suitable DOCTYPE directive, it switches into Quirks Mode and is not fully standards compliant, which may cause pages to display incorrectly.

the DOCTYPE can be added back by using the <ice:outputDeclaration> tag. To do this, you also need to add an <f:view> tag as you would in JSP mode, as in the following example :-

<?xml version="1.0" encoding="UTF-8"?>
<f:view xmlns="http://www.w3.org/1999/xhtml"
 xmlns:ui="http://java.sun.com/jsf/facelets"
 xmlns:f="http://java.sun.com/jsf/core"
 xmlns:h="http://java.sun.com/jsf/html"
 xmlns:ice="http://www.icesoft.com/icefaces/component">
 <ice:outputDeclaration
  doctypeRoot="HTML" doctypePublic="-//W3C//DTD XHTML 1.0 Transitional//EN"
  doctypeSystem="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" />
 <html>
    <head>
    ...
    </head>
    <body>
    ...
    </body>
 </html>
</f:view>

The issue is discussed and solved in this ICEfaces forum post. Interestingly, a page created by ICEfaces for a  JSP (non-facelet) project also uses the <ice:outputDeclaration> tag, presumably for the same reason.

Note that you cannot omit the <f:view> tag and add the xmlns namespace declarations to the <ice:outputDeclaration> tag as this fails – the <f:view> tag needs to be present.

Another post about Quirks mode and DOCTYPE switching, which also contains a useful Bookmarklet (Javascript link) which will tell you directly what mode a page is running in, may be found here.

No Comments »

January 8th, 2010
10:22 am
Useful reference/tutorial sites on Facelets

Posted under JSF
Tags , , , ,

I have found the following sites useful :-

Tutorials

Reference Information

No Comments »

January 8th, 2010
9:19 am
Quirks mode & DOCTYPE switching issues with IE

Posted under HTML
Tags , , , ,

The perceived wisdom is that adding a valid DOCTYPE switches a browser from quirks mode into standards mode, but the truth is that it depends on the DOCTYPE and the browser, and also can depend on how the browser validates the page against that DOCTYPE – the browser can potentially change its mind part way through parsing a page.

I was coding an html page which had the following DOCTYPE (which was auto generated by Topstyle 4), and which still threw IE8 into quirks mode :-

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

The following DOCTYPE corrected the problem and put IE8 into standards mode :-

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
 "http://www.w3.org/TR/html4/loose.dtd">

When using Facelets which is xhtml based, as detailed in this post here re ICEfaces, the actual DOCTYPE outputted by ICEfaces is as follows – this also works well when using standards mode with xhtml/facelets :-

<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

I found the following sites useful when looking for information on this:-

Note re the last link – a Bookmarklet is a link containing javascript which performs some action directly in the current page. I linked to the site article as a courtesy, and the site contains a number of other useful Bookmarklets. The actual bookmarklet (link) which will display the browser mode is here. Just copy and paste the link into your address bar.

No Comments »

January 7th, 2010
7:33 pm
Some useful Sites / Tutorials about JSF & Facelets

Posted under JSF
Tags ,

Helpful simple post about how facelets actually works.
http://www.theserverside.com/discussions/thread.tss?thread_id=41855

JSF central Inside Facelets tutorial series :-
http://www.jsfcentral.com/facelets/

IBM Developerworks articles (can also be downloaded as PDFs from the sites)

Facelets :-
http://www.ibm.com/developerworks/java/library/j-facelets/

JSF for nonbelievers articles – good introduction to JSF:-
http://www.ibm.com/developerworks/java/library/j-jsf1
http://www.ibm.com/developerworks/java/library/j-jsf2/
http://www.ibm.com/developerworks/java/library/j-jsf3/
http://www.ibm.com/developerworks/java/library/j-jsf4/

No Comments »

January 7th, 2010
7:28 pm
Styling tables with CSS

Posted under CSS
Tags , , ,

Rather than use the border keyword in HTML, it is better to style tables in CSS as this localises all the styling in the style sheet and promotes automatic re-use across different tables.

This page from Somacon is an easy-to-use ‘what if’ tool for tweaking all the properties for tables and instantly seeing the effect.

The w3schools reference page for tables may be found here.

This page also from w3schools describes the use of the border-collapse property, which causes the border-spacing and empty-cells properties to be ignored, and for all borders to be collapsed to a single border. This is very useful when you want complete control yourself and to prevent some of these other properties getting in the way, for example when you want a minimal flat solid border.

Note that as is often the case, incorrect HTML can give rise to strange behaviour. For example if you mistakenly set colspan to greater than the number of columns (as I did when I subsequently deleted a column after creating a table and forgot to reduce colspan) you may get a partial double border effect on the right hand side.

Here is an example page with a table illustrating these points, together with all its CSS and HTML.

No Comments »

January 7th, 2010
5:55 pm
Using the ICEfaces Component Showcase CSS and icons as a template

Posted under JSF
Tags , , ,

This tutorial describes how to use the Component showcase css and icons as the template for your own project.

I’ve not looked into it in detail, but it certainly makes sense to use the same look and feel and borrow from ICEfaces where necessary.

One of the comments is less than complementary about some of the CSS code in the example.

No Comments »