The Hidden Gem of Web Frameworks

Tutorial - Stripes: a lean, mean Java web framework - Part 3

 

More about forms and binding

I said that parameter binding was one of Stripes' strongest features. Combined with Stripes' form tags, it's a powerful one-two punch. So let's explore that further. Suppose you have an enum, Gender, as follows:

 

package com.jaxenter.stripes.model;
  
  public enum Gender {
    FEMALE("Female"),
    MALE("Male");
  
    private String description;
  
    private Gender(String description) {
      this.description = description;
    }
  
    public String getDescription() {
      return description;
    }
  }

 

 

Here is how you could create a form to select one gender with radio buttons, any number of genders with checkboxes, and one gender with a select box:  

 

  <%-- formControls.jsp --%>
  <stripes:form beanclass="com.jaxenter.stripes.action.FormControlsActionBean">
    <div>
      Radio buttons:
      <c:forEach var="gender"
        items="<%= com.jaxenter.stripes.model.Gender.values() %>">
        <stripes:radio name="radioChoice" value="${gender}"/>${gender.description}
      </c:forEach>
    </div>
    <div>
      Checkboxes:
      <c:forEach var="gender"
        items="<%= com.jaxenter.stripes.model.Gender.values() %>">
        <stripes:checkbox name="checkboxChoices" value="${gender}"/>
        ${gender.description}
      </c:forEach>
    </div>
    <div>
      Select box:
      <stripes:select name="selectChoice">
        <stripes:option value="">Select...</stripes:option>
        <stripes:options-enumeration enum="com.jaxenter.stripes.model.Gender"
          label="description"/>
      </stripes:select>
    </div>
    <div><stripes:submit name="view" value="Send"/></div>
  </stripes:form>

 

As you can see, the Stripes tag library makes it easy to create forms and use form controls. Remember that the form controls automatically repopulate from previous values. Also, Stripes will automatically handle the conversion between String and our Gender enum type. Finally, notice the <stripes:options-enumeration> tag that takes the class name of an enum and automatically creates a list of options from its values. It renders the enumeration's constant by default, but we can also use another property as we have done here by specifying the label attribute. Stripes also has a <stripes:options-collection> and a <stripes:options-map> tag to render a list of options from any Collection or Map, respectively.

One more very nice feature is that we can include a "Select..." option as the first in the list to prompt the user to select an option, instead of selecting one by default. By doing this, we can make sure that the user makes a selection. Indeed, because the value of the "Select..." option is a blank string, the corresponding property on the action bean will be null if the user does not make a selection. Using @Validate(required=true), we can enforce the requirement of selecting an option. This is much cleaner than using -1 or some other magic value and then manually checking for that value as meaning "no selection was made"! We are ready to look at the action bean:  

 

  package com.jaxenter.stripes.action;
  
  import java.util.List;
  import net.sourceforge.stripes.action.ForwardResolution;
  import net.sourceforge.stripes.action.Resolution;
  import com.jaxenter.stripes.model.Gender;
  
  public class FormControlsActionBean extends BaseActionBean {
    private Gender radioChoice;
    private List<Gender> checkboxChoices;
    private Gender selectChoice;
  
    public Resolution view() {
      return new ForwardResolution("/WEB-INF/jsp/formControls.jsp");
    }
  
    public Gender getRadioChoice() {
      return radioChoice;
    }
    public void setRadioChoice(Gender radioChoice) {
      this.radioChoice = radioChoice;
    }
    public List<Gender> getCheckboxChoices() {
      return checkboxChoices;
    }
    public void setCheckboxChoices(List<Gender> checkboxChoices) {
      this.checkboxChoices = checkboxChoices;
    }
    public Gender getSelectChoice() {
      return selectChoice;
    }
    public void setSelectChoice(Gender selectChoice) {
      this.selectChoice = selectChoice;
    }
  }

 

The properties correspond to the name attributes that we have in the JSP. As I mentioned, Stripes converts from the incoming String to the corresponding Gender. What's more, for the checkboxes, we have a List<Gender> property. This list will automatically contain the genders that the user has selected. We don't even have to create a new ArrayList; Stripes will do that for us, too!

Feature-packed, but without excess

Stripes offers many useful features while staying focused on its raison d'être: it is a server-side web framework, not a full-stack framework nor a client-side graphical user interface. Its philosophy is that there are many excellent solutions for those tiers of an application, so it does not reinvent the wheel and does not prevent you from choosing your favorite solution for things like persistence, JavaScript widgets, and so on. Here is a summary of more features offered by Stripes:

  • Automatic discovery of your action beans simply by mentioning, once and for all, the root package of your action beans. No need to adjust configuration every time you add, modify, or remove an action bean.
  • Similarly, automatic discovery of your extensions: these are custom type converters, formatters, and other Stripes artifacts that you extend or implement to suit your needs.
  • A layout system that is both simple and powerful. The recurring theme of not requiring configuration holds here as well.
  • Annotations to easily create wizards, forms that span multiple pages.
  • Interceptors, which allow you to tap into the Stripes lifecycle and do virtually anything to customize the framework to meet your requirements.
  • Exception handling to define, in one place, how to handle exceptions, from specific to general.
  • Testing facilities so that you can write automated tests for your Stripes web application.
  • Localization to easily make your application available in multiple languages.

Conclusion

Stripes strikes a perfect balance of powerful features, simplicity, and tight focus to give you a framework that does a lot for you without getting in your way nor suffering from feature bloat. It is easy to learn and intuitive, so you quickly become productive. It is customizable and extendable, so you can tweak it to suit your more advanced or particular requirements. Last but not least, it has one of the friendliest communities in the open source framework space, so if you need help, write a message to the mailing list.

Author Bio

Fred Daoud is the author of Stripes and Java Web Development is Fun Again and currently uses Stripes at Modernizing Medicine, Inc. He has been using Java since 1997 and loves web frameworks. He also likes using other JVM-based languages such as Clojure, JRuby, and Groovy.

 

This article previously appears in Java Tech Journal - Java Web Frameworks. Find more articles on web frameworks here.

Pages

Fred Daoud
Fred Daoud

What do you think?

JAX Magazine - 2014 - 05 Exclucively for iPad users JAX Magazine on Android

Comments

Latest opinions