Struts2 needs better Sitemesh Support

Written on November 11, 2008 – 12:26 pm | by mpayne

If one looks at the sitemesh 2.3 example application, an application can have both Freemarker and JSP decorators.

In addition, one has very flexible usage of sitemesh tag libs from with a decorator.

For instance one can reference freemarker’s taglib within freemarker via.
<#assign page=JspTaglibs["/WEB-INF/sitemesh-page.tld"]>

This allows one to include decorated external content from within a freemarker decorator.
e.g. <@page.applyDecorator page=”/google.html” name=”panel” />

—> more complete example that works in Sitemesh “Vanilla”.

<#assign page=JspTaglibs["/WEB-INF/sitemesh-page.tld"]>
<title>Freemarker Decorator - ${title}</title>
<link href=”${base}/decorators/main.css” rel=”stylesheet” type=”text/css”>

<div id=”pageTitle”>${title}</div>

<@page.applyDecorator page=”/google.html” name=”panel” />
<div id=”footer”>
<b>Disclaimer:</b> This site is an example site to demonstrate SiteMesh. It serves no other purpose.

There does not have look like there has been much movement one these issues.  Once fixed I am hoping we can

Common maven Archetypes to keep a reference to

Written on September 27, 2008 – 2:20 pm | by mpayne

Need to keep this site handy.

Eclipse P2= Epic Fail

Written on August 11, 2008 – 9:51 am | by mpayne

On both my windows work machine and Linux Desktop at home the p2 update manager was not able to cut it.  Halfway though re-installing various I used to, I used to linked in via external folder(using the .link method), P2 got into an unresolvable dependency problem that pretty much locked it from doing any further updates.  This happened twice(e.g. both machines).

By looking p2 removal note, I used the script provided in this bug issue.
You pretty much put downloaded eclipse archive(s) in the “source” folder and let the script do its thing.

With the p2 deleted from my eclipse installations, updates seem to go smooth again.

Given the popularity of the bug, I am going to guess that I am not the only one who is having a problem.

Dealing with unmapped fields in XStream

Written on July 30, 2008 – 11:10 am | by mpayne

There is a jira patch proposed here for handling XML.  However that patch still needs to mature(see limitations) and gain acceptances. 

What might not be obvious(a.k.a poorly documented), is that stock XStream already has the
capability to handling this for many use cases.
The test class com.thoughtworks.acceptance.CustomMapperTest
Contains a method that demonstrates such an example.

   public void testCanBeUsedToOmitUnexpectedElements() {
        String expectedXml = “” +
                “<software>\n” +
                “  <version>1.0</version>\n” +
                “  <vendor>Joe</vendor>\n” +
                “  <name>XStream</name>\n” +

        xstream = new XStream() {

            protected MapperWrapper wrapMapper(MapperWrapper next) {
                return new MapperWrapper(next) {

                    public boolean shouldSerializeMember(Class definedIn, String fieldName) {
                        return definedIn != Object.class ? super.shouldSerializeMember(definedIn, fieldName) : false;
        xstream.alias(”software”, Software.class);

        Software out = (Software) xstream.fromXML(expectedXml);
        assertEquals(”Joe”, out.vendor);

In the example above the Software class consists of properties.

    public String vendor;
    public String name;

Without the “MapperWrapper” the xml node “<version>1.0</version>” would generate an exception.

Several years later and NetBeans still doesn’t do it for me

Written on May 27, 2008 – 10:48 pm | by mpayne

I was hoping it would.  I would like to use it to play around with some ruby development. I still may.  However, for Java development it seems like its missing some fairly basic functionality that I have become use to in eclipse. 

One of the basic things I was trying to do is setup more than one run profile for a project.  In this particular project there are several main class that take command line arguments.  In eclipse setting such profiles is fairly trivial.  However, netbeans seems like it only wants one per project or right click on a file “Run” with no arguments. 

What makes jQuery a Good choice?

Written on May 1, 2008 – 4:21 pm | by mpayne

Found this blog post on What Makes jQuery a Good Choice?
Thought this provided a pretty good summary.

There is a response here. I don’t have a problem with prototype, though I have latched on to jQuery(probably aided by its better documentation).
My bad experiences root from Dojo, its bloat and the way its was bundled with Struts2

Changing plugs on my 2001 Xterra

Written on April 21, 2008 – 11:03 am | by mpayne

@78800 miles I changes 5 of the six plugs.  With daylight quickly disappearing, I decided to punt on doing the six plug till I found out some more information about it.

I found this guide, for changing plugs.  I will have to take another crack at it when the weather is nice.

There is also this guide for changing the diff fuild.

Struts2 needs a new AliasInterceptor

Written on April 16, 2008 – 11:00 am | by mpayne

The original aliasInteceptor works fine for gluing together actions with different parameter names that need to be clued together.  The limitation with the existing solution is that properties for both name an alias need to exist in the stack. 

The world keeps evolving though.  Developers make frequent use of richer javascript widgets such as jQuery’s flexgrid or jqGrid. These widgets can consume a specific JSON response and dish out their own unique set of named parameters to the server side.  This presents a challenge when trying to leverage existing back end code that works great, it just doesn’t match up name wise to the parameters names coming in for a specific view.

Assume one has a some abstract PagingQueryAction(children just set query/queryCount implementations) that can accept incoming parameters of:
    int pageSize = 10;  // results per page, default 10
    int currentPage;
    String filter;
    String sortorder = “asc”;
    String sortname = “”;

This works fine and dandy for some existing web views, but if one wanted to make use of FlexGrid, they would create setters named.

page 3
qtype name
rp 15
sortname iso
sortorder asc

jqGrid has yet another set of name parameters that its submits.

Making new implementations of things because some UI widgets is hardcoded to respond with a specfic set can be a PITA. 

If there existed an AliasInterceptor that would allow one to adapt to these widget requirements people would have another solution at their disposal.

example of a mapping:

<!– usaged
  <param name=”aliases”>#{ ‘incomingName’ : ‘actionPropertyName’}</param>
    <package name=”json” extends=”inventoryDefault” namespace=”/json”>
            <action name=”summaryPagedJSON” class=”stateSummary”>
                 <param name=”pageSize”>15</param> <!– change default here –>
                 <param name=”aliases”>#{ ‘query’ : ‘filter’, ‘rp’:'pageSize’,'page’:'currentPage’ }</param>

                <result name=”success” type=”freemarker”>
                <result name=”input” type=”freemarker”>
If one wanted to adapt the paging action for jqGrid, they’d simply need to
supply an alias parameter of:
  <param name=”aliases”>#{ ’searchString’ : ‘filter’,'page’:'currentPage’, ’sord’:’sortorder’, ’sidx’:’sortname’ }</param>

(Note the freemarker template producing the JSON response may need to differ slighty as well)

This proposed alias interceptor looks for the alias within “invocationParameters” instead of the stack.
Perhaps we need to rename things.  The old AliasInterceptor is name ChainingAliasInterceptor
and the new one is ParameterAliasInterceptor.
Or perhaps the alias functionality should just be built in to the existing ParameterInterceptor.

snippet below
    public String intercept(ActionInvocation invocation) throws Exception {

    ActionConfig config = invocation.getProxy().getConfig();
    ActionContext ac = invocation.getInvocationContext();

    // get the action’s parameters
    final Map configParameters = config.getParams();
    final Map invocationParameters = ac.getParameters();

    if (configParameters.containsKey(aliasesKey)) {

        String aliasExpression = (String) configParameters.get(aliasesKey);
        OgnlValueStack stack = (OgnlValueStack) ac.getValueStack();
        Object obj = stack.findValue(aliasExpression);

        if (obj != null && obj instanceof Map) {
        // override
        Map aliases = (Map) obj;
        Iterator itr = aliases.entrySet().iterator();
        while (itr.hasNext()) {
            Map.Entry entry = (Map.Entry);
            String alias = entry.getKey().toString();
            String setterName = (String) entry.getValue();
            Object value = invocationParameters.get(alias);
            if (null != value) {
            stack.setValue(setterName, value);
        } else {
        log.debug(”invalid alias expression:” + aliasesKey);

    return invocation.invoke();

Use jquery remote validation with struts2

Written on April 15, 2008 – 3:39 pm | by mpayne

I feel that jquery’s validation plugin gives a much better UI treatment than the struts client side validation.  In addition, there are case where one does not want to pay any Dojo penality.

To make a remote (server side) validation call, the server must return a true/false JSON response.  There is a Struts 2 JSON plugin , however in this then response needs to be a plain “true”/”false” answer, not attribute name = “{ “response”: true}.  Because of this, the Struts2 built-in “stream” result seems to make the most sense.

The xml configuration for such an action may look something like:

<package name=”ajax”  extends=”default” namespace=”/ajax“>

<interceptor-stack name=”slimStack”>
<interceptor-ref name=”exception” />
<interceptor-ref name=”alias” />
<interceptor-ref name=”servletConfig” />
<interceptor-ref name=”prepare” />
<interceptor-ref name=”chain” />
<interceptor-ref name=”currentUserAware”/>
<interceptor-ref name=”staticParams” />
<interceptor-ref name=”params”/>

<default-interceptor-ref name=”slimStack” />
<action name=”validPassword” class=””>
<result type=”stream“>
<param name=”contentType”>text/html</param>
<param name=”inputName”>inputStream</param>


Note that if you are using Sitemesh, you will want to make sure that these type of responses do not get decorated, because of this I put this action in own namespace (/ajax) and Sitemesh is configured to exclude

For the action to produce a stream reponse, it simply needs to return an InputStream.
public class RemotePasswordValidation {

public RemotePasswordValidation() {
uberPattern = Pattern.compile(”^(?=.*(\\d|[^a-zA-Z\\d]))(?=.*([a-z]|[^a-zA-Z\\d]))(?=.*([A-Z]|[^a-zA-Z\\d]))(?!.*\\s).{6,12}$”);

private InputStream inputStream;
private Pattern uberPattern;
private String password;
private Boolean valid = false;

public Boolean isValid() {
return valid;

public InputStream getInputStream() {
return inputStream;

public String execute() {
valid = validPassword(getPassword());
inputStream = new;
return Action.SUCCESS;

public boolean validPassword(String password) {
boolean result = false;

if (password != null) {
if (password.trim().length() >= 6) {
if (uberPattern.matcher(password).find()) result=true;

return result;

public String getPassword() {
return password;

public void setPassword(String password) {
this.password = password;

The actual definition in javascript might look something like the following:

<script type=”text/javascript”>
$().ready(function() {


rules: {
password: {
required: true,
minlength: 6,
remote: “${base}/ajax/validPassword.action”
confirmPassword: {
required: true,
minlength: 6,
equalTo: “#password”
messages: {
password: {
required: “Provide a password”,
rangelength: jQuery.format(”Enter at least {0} characters”),
remote: jQuery.format(”{0} does not meet password policy requirements.”)
confirmPassword: {
required: “Repeat your password”,
minlength: jQuery.format(”Enter at least {0} characters”),
equalTo: “Enter the same password as above”
Screen shot of running example

Other thoughts?

  • No I didn’t have to resort to server side validation for just testing a password against a regex pattern.  There are certainly many ways to skin the cat, but this provides an example of remote jquery validation that could be extended for more complex and needs, like testing against some type of password history in ActiveDirectory.

Looks like Jetty is gaining in popularity for production deployment

Written on April 14, 2008 – 9:59 pm | by mpayne

Found the following post -&amp;gt; Jetty improves in Netscraft survey again.

Got to remember to share this, due to it being a recent converation at work