Java Notes From My Desktop

Tags - Categories : All | Articles | Book Reviews | CSS | Java | Javaranch | Javascript | News | Opengl | Opinions | Personal | php

There has been a pretty good discussion on JavaRanch about JSF recently and the original poster was a bit dismayed at JSF's current state. Several alternatives were suggested as well as proponents for JSF jumping in claiming that JSF is "good enough". It's Interesting that a lot of the comments from JSF proponents in several threads I've been keeping an eye on were things like "...it has it's issues but..." and "...a workaround for that is...".

I try and suggest Stripes as much as I can. Since I stumbled on Stripes, I've found it to be the best all around framework for my purposes. Yes, my purposes. I don't pretend to think that Stripes is the end all solution in the saturated domain of JEE Frameworks, but if I see someone asking for a solution to a problem, and I feel Stripes solves that problem, I'll recommend it.

As I have been recommending Stripes for several issues related to JSF, I've found myself comparing the two frameworks in my mind more than on paper and that is what I intend to do here. Obviously this will be a bit biased since JSF lost its luster for me a long time ago, but I'll try to be as fair as I can be. If I state anything that is utterly untrue about JSF or Stripes, I invite anyone to correct me.

I'll spare the history of JSF and Stripes for now. I'll assume you can do your own homework if you want to know when and where both got started. And I am going to jump right into some content. The first thing I would like to do is look at the library requirements for both frameworks. I've left out anything that is optional such as commons-fileupload.jar as well as only including the bare minimum to get simple pages displaying. No Spring, Hibernate, iBatis, or anything like that.

(Note: I am going to be assuming MyFaces as the JSF implementation of choice. If I had to develop a JSF application today, I would choose MyFaces.)

JSF

  • commons-beanutils.jar
  • commons-codec.jar
  • commons-collection.jar
  • commons-digester.jar
  • commons-lang.jar
  • commons-logging.jar
  • commons-validator.jar
  • tomahawk.1.x.x.jar
  • myfaces-impl.jar
  • myfaces-api.jar

Stripes

  • commons-logging.jar
  • cos.jar
  • stripes.jar

I don't think any explanation of the above dependencies is necessary. Obviously including JAR files in a web application isn't a big deal nor is it a defining factor in which framework to choose, but interesting none-the-less.

Interestingly Stripes ActionBeans and JSF Backing Beans are quite similar. It's interesting because Stripes is an action based framework (ie: Struts, WebWork, etc) where JSF is considered a component oriented framework (ie: Tapestry, Wicket, etc). There are several differences between the Stripes ActionBean and the JSF Backing Bean which I'll cover as we come to them. Let's begin with a very simple page, but not quite as simple as Hello World. Let's consider a User Registration page. There will be 2 buttons; Submit and Cancel. Submit will save the page and Cancel will simply reset the form fields.

(Note: I don't plan on this being a tutorial or how-to for either framework so you'll notice quite a bit of missing information and code. I will only show enough for a fair comparison. There are tons of JSF tutorials and the Stripes documentation is superb. I'll have links to all this at the end of this article)

Let's begin with the server side code. In the case of JSF, it would be the Backing Bean.

public class RegistrationBean
{
  private User user;

  // assume getters and setters for User
  
  public String register()
  {
    // registration code here
    if (good)
    {
      return "success";
    }
    else
    {
      // add some error messages and what not
      return "failure";
    }
  }

  public String cancel()
  {
    return "cancel"
  }
}
Java2html

Nothing too fancy here. I assume a User object that contains things like username, password, firstname, lastname, etc. I also realize that as the Backing Bean gets more complex many commonly used methods might be refactored into a Base class that all Backing Beans extend. JSF uses string messages to determine page flow and those messages are duplicated in the faces-config.xml file. Now lets look at the Stripes ActionBean for the same code.

public class RegistrationActionBean implements ActionBean
{
    private ActionBeanContext context;

    @NestedPropertyValidator( {
       @Validator(field="username", required=true},
       @Validator(field="password", required=true},
       @Validator(field="firstName", required=true},
       @Validator(field="lastName", required=true}
    });
    private User user;
 
    public void setContext(ActionBeanContext context)
    {
          this.context = context;
    }

    // assume getters and setters for User

    public Resolution register()
    {
          // registration code here
          if (good)
          {
             return new RedirectResolution("/welcomePage.jsp");
          }
          else
          {
             // add some error messages and what not
             return getContext().getSourcePageResolution();
          }
    }
   
    @DontValidate
    public Resolution cancel()
    {
          return new RedirectResolution("/register.jsp");
    }
   
}
Java2html

Initially, the JSF server side code is a lot cleaner, shorter, and simpler. As far as lines of code, it is. The difference is that much of the code you see in the Stripes server side code is scattered across a couple of places for JSF. For example, all the validation for simple form fields will be specified in the JSP and the page flow or navigation is in the faces-config.xml. Stripes has no external configuration for such things. It's all encapsulated in one place. This is largely due to annotations.

(Note: Stripes relies heavily on features of Java5. Using Retrotranslator you can get Stripes to work with Java 1.4 since Java5's enhancements are just syntax sugar and the bytecode is pretty much the same. JSF, however, runs just fine on 1.4 without any extra work, at least until the next JSF spec comes out.)

Let's move on to the JSP files. I'll be leaving out much of the HTML required code as well as the TAGLIB requirements for each framework. Both JSF and Stripes use custom tags to help render the page. So they both need these inclusions. Let's begin with Stripes this time.

<stripes:errrors />
<stripes:form action="/Registration.action">
<table>
    <tr>
       <td><stripes:label class="commonLabel" for="user.username">Username</stripes:label></td>
       <td><stripes:text class="commonText" name="user.username" id="user.username" /></td>
    </tr>
    <tr>
       <td><stripes:label for="user.password">Password</stripes:label></td>
       <td><stripes:password name="user.password" id="user.password" /></td>
    </tr>
    <tr>
       <td><stripes:label for="user.firstName">First Name</stripes:label></td>
       <td><stripes:text name="user.firstName" id="user.firstName" /></td>
    </tr>
    <tr>
       <td><stripes:label for="user.lastName">Last Name</stripes:label></td>
       <td><stripes:text name="user.lastName" id="user.lastName" /></td>
    </tr>
    <tr>
       <td><stripes:submit name="register" value="Register" /></td>
       <td><stripes:submit name="cancel" value="Cancel" /></td>
    </tr>
</table>

This requires a bit of explination. First, the form action. Notice that we named our ActionBean RegistrationActionBean. When a stripes web application starts up, Stripes inspects all the classes in the classpath and looks for implementations of ActionBean. It then strips off any package name up to and including the name action and removes ActionBean from the class name. Therefore, the action automagically becomes Registration.action. The .action is mapped in the web.xml via the StripesFilter. The stripes:label is used to look up text from a resource bundle much the same way that JSF does. Notice I only placed a class="" on username field and label. I did this to prove a point in that notice how Stripes uses the same attribute names as standard HTML. You'll see the difference when I show the JSF version. The stripes form tags use the name attribute to map values back to the domain model. In our case, User. This is also how Stripes will redisplay the data in the case of a validation errors and general model persistence to and from the server. Basic MVC stuff here. Stripes also uses the name attribute for submit buttons to map them to methods in the ActionBean. So when you click the Register button, the register method is called. This is also quite similar to JSF, which I'll move to now.

<f:view>
    <t:messages />
       <h:form >
          <h:panelGrid columns="2">
             <h:outputLabel styleClass="commonLabel" for="username" value="#{messages.email}" />
             <h:inputText styleClass="commonText" id="username" value="#{registrationForm.user.username}" required="true" />
             <h:outputLabel for="password" value="#{messages.password}" />
             <h:inputSecret id="password" value="#{registrationForm.user.password}" required="true" />
             <h:outputLabel id="firstName" value="#{messages.firstName}" />
             <h:inputText id="firstName" value="#{registrationForm.user.firstName}" required="true" />
             <h:outputLabel id="lastName" value="#{messages.lastName}" />
             <h:inputText id="lastName" value="#{registrationForm.user.lastName}" required="true" />
             <h:commandButton id="registrationButton" value="#{messages.registrationButton}" action="${registrationForm.register}" />
             <h:commandButton id="cancelButton" value="#{messages.cancelButton}" action="#{registrationForm.cancel}" immediate="true" />
          </h:panelGroup>
       </h:form>
</f:view>

JSF requires all pages to be placed in a "view" for the component tree. General and validation messages are displayed pretty much the same way through a tag. JSF has tags to spit out HTML components. For example, the panelGrid will create a table. Currently, JSF requires tags to be enclosed in tags. If I were to simply use a TABLE as I did in the Stripes example, the component tree could become disordered and your JSF components might render in unexpected places. I believe this is going to be fixed in the next JSF release (1.2) and hopefully someone can confirm this. Notice also that instead of simply using the word class, JSF changes it to styleClass. Struts also does this. But it's weird. JSF uses EL syntax for pulling values out of the backing bean as well as from the resource bundle. Notice that in JSF we use the # rather than $. I believe that the next JSP spec is more in line with JSF style EL so that the 2 work together better.

I'd challenge you to change the extension of these files to .html and open both in a web browser on your local machine. Looking at this from a designer's POV, I know which one I'd rather work with.

The good news is that aside from the web.xml the Stripes code is complete. The bad news is that for JSF, we still need another file. We need the faces-config.xml which loads the backing beans and configures the page flow or navigation.

<managed-bean>
    <managed-bean-name>registrationForm</managed-bean-name>
    <managed-bean-class>com.test.beans.RegistrationBean</managed-bean-class>
    <managed-bean-scope>request</managed-bean-scope>
</managed-bean>

<navigation-rule>
    <from-view-id>/register.jsp</from-view-id>
    <navigation-case>
       <from-outcome>success</from-outcome>
       <to-view-id>welcome.jsp</to-view-id>
       <redirect />
    </navigation-case>
    <navigation-case>
       <from-outcome>failure</from-outcome>
       <to-view-id>register.jsp</to-view-id>
    </navigation-case>
</navigation-rule>

Nothing too fancy or complicated there so I won't go into much detail. Even if you have never used JSF, it's pretty easy to determine what is going on.

Up to this point the frameworks don't seem that different. Both use a Java Bean of sorts for the backing code. Both use custom tags for forms. Both have a solid validation engine. Beyond that, and internally, there are significant differences. And I can't very well go into them in detail without really showing my bias towards Stripes. With this article, I just wanted to show everyone a bit of each and let you start making your own conclusions which will be easier for you as you develop with each framework.

Depending on the response to this article, I may write another one that is geared more at where I believe Stripes is superior to JSF. I just didn't feel like getting all the hate mail this morning.

Resources:

  • http://stripes.mc4j.org/confluence/display/stripes/Home
  • http://myfaces.apache.org/
  • http://java.sun.com/javaee/javaserverfaces/
  • http://www.jsftutorials.net/


First, a small correction on Stripes and JDK1.4. See: this page in the docs

Second, it has been a while since I have looked at JSF, but when I did it was code that a fellow developer had done and was the worst stuff I have ever seen outside of a swing UI builder. I'm sure it was because it was done with a builder tool, but still. I've heard many people say that if you don't use a UI builder it is a total pain. I can't confirm that myself however.

I think the biggest benefit of Stripes is that it pretty much stays out of your way. It doesn't try to autobuild your UI or abstract you away from the HTML or any of that crap that some frameworks try to do. It is simple, clean and self configuring.

You make some comparisons between the tags that each uses. I think it would be beneficial to describe how you can use each without the tag libraries which to many of us is actually a benefit.

Good post :-)

I know about the 1.4 compatibility for Stripes and I probably should have mentioned it. It feels like a hack to me and I don't like it. I probably edit my article to include that link. Thanks.
An important difference in these two examples is that the stripes one is accessible (would work with javascript turned off, and ctrl+click works in Firefox) and JSF isn't. You also don't need to specify the id in the stripes text fields.
Why is that the JSF is any less accessible than the Stripes example? None of them use javascript that I can see. Both are therefore processed on the server in any case. Accessibility therefore - if an issue at all - would fall under the purvue of the browser in question. Not the application - as all application logic is executed on the server. Note that this might be different if you combine AJAX in your JSF component. But the example doesn't do that.
You're right, the example does not use any javascript at all, and indeed the generated HTML should not include any javascript according to the available books (JSF in action and the O'Reilly Java Server Faces are the ones in my shelf right now), but it does. MyFaces at least, and that includes shale, (can't say for ICEFaces or Seam) does not work if you try to disable javascript because every form submit is done via javascript: they remove disabled fields, override the default form caching mechanism, and other crap.

If you disable javascript in the browser your MyFaces app will stop working: there are two switches in web.xml that supposedly disable javascript, but none of them work.

My full rant is here

Well, yes plain JSF is a pain, but luckily it has many extension points for frameworks like facelets and Seam to hook in.

Facelets is a powerfull view definition framework that let's you write xhtml and use templating. It let you write designer friendly code.

Seam on the other hand, solves many of other JSF problems you don't mention, like redirect after post, scope, and some more.

JM2C.
Check out Stripe's layout features. They are nearly identical to those offered by Facelets (http://www.stripesframework.org/display/stripes/Layout+Reuse). I too was looking into Seam to help mitigate vanilla JSF hassles, but I've found that Stripes addresses our specific problems - without requiring a new container.
I appreciate ease of development as everyone does and I too like the way stripes goes (a bit convention over configuration). What makes JSF a favourite to me is the availability of complex components. Maybe I missed something, but to implement a editable table, a tab or treemap I would have less support in stripes than in JSF.

For those looking for "convention over configuration" ease of use improvements to the basic JSF framework, consider a couple of the features of Shale (http://apache.shale.org/):

The "view controller" capability provides application callbacks, based on a convention that the view identifier (the name of the JSP or Facelets or Clay or whatever) page can be transformed into the name of a JSF managed bean. The default algorithm covers many common cases, but you are welcome to plug your own mapping in as well. Of course, you still have to tell JSF about your managed bean, but see the next point ...

If you'd like to declare JSF managed beans (or your custom components, converters, renderers, and validators for that matter) with annotations instead of XML configuration, check out the Tiger extensions module of Shale ( http://shale.apache.org/tiger/).

As others have pointed out, a key feature of JSF has nothing to do with UI components -- it is the fact that you extend the capabilities of the basic JSF framework by utilizing the extension points that are provided. This is *normal* use of JSF, not a hack.

A completely separate consideration for some developers is, can I have a development tool that gives me some assistance in creating my UI? For component oriented frameworks like JSF, it is technically feasible to produce such tools. For frameworks that rely on Java code to produce the UI, this is much more challenging -- even if someone building tools is willing to bear the costs for supporting the relatively small market share of most web applicatgion frameworks today.

Even Struts (arguably the most popular Java based webapp framework ever, and I should know :-) has not been supported by visual development tools for the user interface. You generally get pretty nice support for the navigation part of your site (due primarily to the use of an XML based file to persist the configuration choices, but I digress :-), but not for visual WSYWIG type development.

Why? Because Struts wasn't developed with that goal in mind. Seven years ago, when I started Struts, that was a good decision. And, the fact that it used an XML format for persisting configuration information was mission critical to being able to create tools around it.

Stripes is definitely cool if the people developing your application are Java savvy. There are loads of good ideas here. But ... it turns out that there are roughly 5-20 times as many web developers in the world (depending on whose numbers you trust) that are *not* Java savvy.

If Stripes does not want to compete for those developers, that's fine with me :-). I'll pitch JSF based solutions (with advanced frameworks like Shale and Seam where needed) without giving up the ability for mere mortals to create viable web applications.

It would be helpful if you provided the purposes for which you decided to stick with Stripes. As any other open source project, it actually maps its developers preferences and their way of dealing with common tasks. Besides Stripes, there are too many other framework (which is unfortunate) for the same purpose of creating web applications. What are pros and cons of using one over others? If Stripes does not solve your task or allow you to stick exclusively with it, that means your choice was not good.
The reason that I like JSF is that I can manipulate JSF components inside my Java code. For example, to change the lable on a button using JSF is much eaiser than to do it in JSP using any kind of tags
Looks like a great framework. Sadly, Apache is not emracing it in Struts Action 2 Framework. And, do you think Gregg, action-based frameworks (Struts, WebWork or Stripes) still have a room to play in ? It seems to me it is the time for component-based frameworks (i.e JSF) and scripting-based ones (i.e Rails) ?
this may be a stupid question but how does stripes preform in a j2EE environment
"this may be a stupid question but how does stripes preform in a j2EE environment"

Well, great actually. Take a look at www.HomeGlut.com It uses Stripes, along with Spring, and Ibatis.
Ok - so you compare how to do a bean. Now lets see you do a Tree component or tabbed pane in JSF, vs in Stripes. JSF will win hands down with all rich interfaces, drag and drop, you name it. Stripes cant do nothing.... great, so it uses a couple lines less code to do basic things - So what ? What happens when it comes to hardcore UI development it cant deliver. JSF Forever!!!!!!
Dean, is anything wrong with using YUI components for tabbed panes and trees?

Stripes does not provide components for these tasks, but that does not make JSF any better. It's like comparing displaytag with JSF (hint: displaytag is much better).

I have tried JSF myself, but it's just like ASP.Net, very heavyweight. Plus JSF and ASP.Net have obscured HTML so much, that it is hard to maintain a one-to-one relationship with HTML tags. On the other hand, Stripes maintains that relationship, and is very lightweight, yet very powerful. It's really a matter of knowledge and preference. I have tried most web application development frameworks, and I can honestly say that I am very happy with Stripes. So you need Tabbed Panes, Tree Components, try http://www.dynamicdrive.com
Good article, Greg.
I am evaluating Stripes at the moment, might soon be bugging you at JavaRanch with lots of Stripes noob questions :)
A couple of things in the code did not work though, may be because of difference in Stripes versions. I had to change the validation annotations to -
    @ValidateNestedProperties ( {
       @Validate(field="username", required=true),
       @Validate(field="password", required=true),
       @Validate(field="firstName", required=true),
       @Validate(field="lastName", required=true)
    })
Also, the opening and closing parentheses are not matched in - @Validator(field="username", required=true}
Cheers.
Reading the TheServerSide.COM news I’ve found a comparison’s article between Stripes and JSF frameworks. I can’t agree more with the author Gregg Bolinger when he says Since I stumbled on Stripes, I’ve found it to be the best all around fra...

Read more...
gyswunnb

Read more...
Aggregator of 1967 mercury cougar sites

Read more...
Description of instep stroller twin.

Read more...


Add a comment

Title
Body
HTML : b, i, blockquote, br, p, pre, a href="", ul, ol, li
Math Quiz 4 + 6 = (Helps stop blog spam)
Name
E-mail address
Website
Remember me Yes  No 

E-mail addresses are not publicly displayed, so please only leave your e-mail address if you would like to be notified when new comments are added to this blog entry (you can opt-out later).