Monday, January 19, 2009

Struts2 Pain Points & Remedies

Recently, I worked on Struts2 based web application and discovered some pain points. Below are the pain points & remedies:

a) Performance:

Problem:
Struts2 comes with lot of goodies but at the same time slows down performance especially page response times. If you closely look at Struts2 architecture, there are many layers and that's why Struts2 is able to give lot of flexibility & control at all levels since there is ‘loose coupling’ across all layers including page rendering. Struts2 internally uses freemarker templates (.ftl) with OGNL EL for page rendering. Each web component such as textfield, label, form, checkbox etc have separate ftls grouped under ‘themes’ (Struts2 has themes – simple, xhtml (default), css html and Ajax)

While OGNL is more 'powerful' and 'robust' but consumes more processing time due to its inherent nature - Object traversal for indexed properties. While OGNL is very good where there are complex object structures such as table, Tree etc (where traversal is complicated), but for applications with simple properties, it turns out to be expensive.

Remedy:
i) If you have more text fields & labels on the form, better write a separate tag library similar to Struts1X and modify tag class in struts-tags.tld.
ii) Try to use freemarker template cache which caches page processing for a time interval specified in the freemarker.properties.
For other optimization techniques visit Struts2 site -
http://struts.apache.org/2.x/docs/performance-tuning.html

b) Presentation Component Placing:

Problem:
In Struts2, if default theme – XHTML is used, you cannot put two text fields or any other components side by side or adjacent to each other. Reason is – Struts2 uses freemarker templates internally to render the components and these components have extra html table row - tr , which makes components appear on next line instead on same.
Remedy:
To solve this, you need to customize all freemarker templates ( text.ftl,form.ftl,label.ftl etc). You need have your own ‘theme’ and put all the customized templates under theme. Specify the customized theme in your struts.xml against ‘struts.ui.theme’ property.

c) Decorate Field & Action Error Messages:

Problem:
Struts2 has built-in field & action errors for displaying form validation & action validation errors.Howeever, doesn’t give more control over decorating (put your own border around message, put an error image etc) or customizing error messages through any of its tag libraries.
Remedy:
If you need to customize your error messages, you need to modify fielderror.ftl & actionerror.ftl templates. Add your own presentation or display logic. Of course, you need to know bit of freemarker syntax.

1 comment:

  1. as Struts2 is still being adopted, this gives good pointed solution to common problems.

    ReplyDelete