Blog Archives

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
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 7th, 2010
5:14 pm
Hover and other effects in CSS using Pseudo Classes

Posted under CSS
Tags , , ,

There are plenty of sites about showing how to do hover and click (=active) effects on an element (typically a link or image/image button) using Javascript, but with modern browsers (including IE7 and above but not IE6) this is completely unnecessary as it can be done entirely with CSS. The advantages are clear – no Javascript is needed, the CSS is straightforward, and no round trip to the server is needed. The typical effects used are to swap images or highlight using a different border or background.

These effects can now be performed on any element, not just a link, but check out compatibility as IE has had some issues with this. I have performed this on image buttons as well as links and had no trouble.

The W3schools article on Pseudo Classes is here. An example of using the hover effect to switch a background image via CSS may be found here. (Note the importance of setting an explicit width and height in the CSS – required because the example uses background images).

The pseudo class can be added to an existing style class. The following simple example shows an ICEfaces Image button with a style class, with modified pseudo classes used in the CSS to highlight the border differently on hover and active (active in CSS terms means click).

CSS

.ImageButton {
	margin: 1px;
	padding: 1px;
}
.ImageButton:hover {
	padding: 0px;
	border: 1px solid #ffa600;
}
.ImageButton:active {
	padding: 0px;
	border: 1px solid #ffd800;
}

JSF

<ice:commandButton image="images/arrow-up.gif"
                   styleClass="ImageButton">
</ice:commandButton>

No Comments »

January 7th, 2010
11:55 am
Using Unicode Characters in HTML

Posted under HTML
Tags , ,

Numeric Unicode references can be added using the format :-

  • &#N; (for decimal unicode value N)
  • &#xN; (for hexadecimal unicode value N)

The use of Webdings and Wingdings font families is not universally compatible and is considered a hack – use unicode characters instead as above.
Wikipedia has a good article on this here.
Full unicode character charts may be found on unicode.org here.

No Comments »

January 5th, 2010
5:34 pm
Java Bean Introspection and dynamic Sorting/Comparison

Posted under Java
Tags , , , , , ,

Whilst this can be done with the Reflection API, there are standard APIs that do this for Beans and which are therefore a better choice.

java.beans.beaninfo is a standard API for bean introspection. Better still for many applications is the Apache Commons BeanUtils library.

This provides a really simple interface for many operations. For example, get/set of simple bean properties dynamically is a single call as in this example :-

Employee employee = ...;
    String firstName = (String)
      PropertyUtils.getSimpleProperty(employee, "firstName");
    String lastName = (String)
      PropertyUtils.getSimpleProperty(employee, "lastName");
    ... manipulate the values ...
    PropertyUtils.setSimpleProperty(employee, "firstName", firstName);
    PropertyUtils.setSimpleProperty(employee, "lastName", lastName);

One of my requirements was a dynamic sort comparator for use in sorting JSF tables by any column. A static comparator is fast, but messy, as you need to hard code switch statements etc. for the property to be compared. BeanUtils has a better option and can create a dynamic comparator for you as in the following code fragment from here :-

List tvShows = new ArrayList< WorldsGreatestTVShow>();
    //group (sort) by actor
    BeanComparator actorComparator = new BeanComparator("actorName");
    Collections.sort(tvShows, actorComparator);
    //group (sort) by producer
    BeanComparator producerComparator = new BeanComparator("producerName");
    Collections.sort(tvShows, producerComparator);

However, there is a performance hit with using this – one comparison cited 16ms using a hard coded static comparator, compared to 400ms using the BeanUtils BeanComparator. Always one to strive for perfection, I said to myself “why can’t I have both” – i.e. a dynamically created comparator for no effort but which performs as well as a static one? Well, after a bit of searching I found one in the form of the Cojen Project. This is a project aimed at supporting dynamic bytecode generation and disassembly. However, the project also provides a number of powerful utility classes which use the bytecode generation to create fast dynamic Bean Comparators with the same peformance as a statically coded comparator, perform fast Bean Property Introspection, and fast Pattern Matching.

Here is an example comparator which orders threads by name, thread group name, and reverse priority :-

 Comparator c = BeanComparator.forClass(Thread.class)
     .orderBy("name")
     .orderBy("threadGroup.name")
     .orderBy("-priority");

I particularly love the fact that the customising methods e.g. orderBy return the comparator instance so that you can chain them together as above. Sorting by multiple properties is supported as the above example shows. All the generated objects are fully serializable. I  also love the fact that you can prefix a property name with a minus sign to reverse the comparison order for that orderBy, which is great for toggling column sort orders with JSF etc.  A brief test had it working correctly in no time, and it does indeed appear to be just as fast as a static comparator and very flexible. In future, I would make Cojen my first port of call for bean pattern matching / introspection / comparison without hesitation, and then look at Apache Commons BeanUtils only if Cojen couldn’t do what I needed, then failing that java.beans.beaninfo or finally the raw reflection API.

I really can have my cake and eat it! Sex on a stick!

No Comments »

January 5th, 2010
4:56 pm
@ManyToMany issues with Eclipselink 1.1.2

Posted under JPA
Tags , , , ,

I found a number of issues when configuring a many to many relationship, but eventually found a working solution.

1/  This example for a many to many uses referencedColumnName when it does not need to – it was a hangover from an example using multiple join columns. If you do this with Eclipselink 1.1.2 and Oracle (in my case XE 10g), the columns are created with data types of varchar2(255) instead of the default of number(19)  :-

      @ManyToMany
      @JoinTable(name="AppUserRole",
                 joinColumns =@JoinColumn(name="AppUserID",
                                          referencedColumnName="AppUserID"),
                 inverseJoinColumns=@JoinColumn(name="AppRoleID",
                                          referencedColumnName="AppRoleID”))

The referencedColumnName attribute is the cause of this issue. Leaving it out causes correct column types. It is only needed for multiple column joins (which break anyway see 2/), and so should not be used. This therefore works correctly and is the recommended format to use  :-

      @ManyToMany
      @JoinTable(name="AppUserRole",
                 joinColumns =@JoinColumn(name="AppUserID"),
                 inverseJoinColumns=@JoinColumn(name="AppRoleID"))

2/ Using a many to many as in 1/ but with multiple join columns causes eclipselink bug 300485. (Although listed as a one to many bug it also happens with many to many). This is not due to be fixed until eclipselink 2.1. The bug gives a query parameter not found error for an internally generated query used when eclipselink lazily loads a relationship collection.

3/ Leaving out the @JoinTable and only having the @ManyToMany annotation works ok, but gives an XML column name resolution error from eclipse for one of the join columns. This is purely an ‘invalid validation’ however, as the code runs fine against a database created from it.

4/ using the xml annotations in orm.xml along with just an @ManyToMany annotation in the code works fine, but you do get some validation errors from Eclipse as the validation does not appear to merge the annotations and xml correctly when validating :-

<entity>
  <attributes>
   <many-to-many name="appRoles">
    <join-table name="AppUserRole">
     <join-column name="AppUserID" column-definition="number(19)"/>
     <inverse-join-column name="AppRoleID" column-definition="number(19)"/>
    </join-table>
   </many-to-many>
  </attributes>
 </entity>

This would be the preferred route if anything database specific  was needed, such as the column-definition attributes for Oracle in the example.  The “@ManyToMany” annotation on the entity may be superfluous in this case but is a helpful label. A comment in the code that there are overrides in orm.xml would be helpful. The above fragment was tested but without the column-definition attributes on the columns – these are shown as examples of how to add database specific column definitions without having to pollute the code with them via annotations.

The intention here would be to use xml in conjunction with annotations, with annotations used for all the standard metadata. Different versions of orm.xml and persistence.xml could then be swapped in and out for different back end databases, keeping the code standard. The same approach has been advocated to permit using Oracle sequences here.

No Comments »

December 23rd, 2009
4:34 pm
How to set the Date and Time in Linux

Posted under Linux
Tags ,

Enter one or more of the following from a terminal (shell) prompt :-

# date set="2 OCT 2006 18:00:00"
# date -s "2 OCT 2006 18:00:00"
# date +%T -s "10:13:13"

 

See here for more examples

Comments Off on How to set the Date and Time in Linux

December 23rd, 2009
4:01 pm
How to list files with full path in Linux

Posted under Linux
Tags ,

Here is one way which also illustrates the use of the double backslash command substitution mechanism, whereby an embedded shell command in backquotes is replaced by the output of executing it :-

ls -lR `pwd`/* | grep 'epdfview'

 

The pwd command is expanded and replaced with the current working folder as the ls command is executed. In this example, the output is piped through the grep command to search for a file – this is for illustration, as you would normally do this with the find command as shown here.

Comments Off on How to list files with full path in Linux

December 23rd, 2009
3:30 pm
How to search for files in Linux

Posted under Linux
Tags ,

You can use the find command to do a wildcard search for files.
For example, the following will do a wild card name search :-

lynx / # find -name 'epdfview'
./home/mms/.config/epdfview
./usr/bin/epdfview
./usr/share/epdfview
lynx / #

 

The default is to search the current folder and all subfolders. You can search for a number of other fields as well as name, and there are lots of other options, see here

Comments Off on How to search for files in Linux