ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.

advertisement

AddThis Social Bookmark Button

Java Web Development with Stripes
Pages: 1, 2, 3

There is also an Integer field called age, which is a property of the Person object. Stripes will first perform type conversion on the request parameter value for person.age for the Integer field and bind the value in the Person object. After the value is bound to the age field in the Person object, Stripes will validate that the Integer value is less than 13. If the user enters a string instead of an integer the user will see this message:

The value (Mark) entered in field Person Age must be a valid number.

If the user enters an integer but the value is less than 13 the user will see this message:

The minimum allowed value for Age is 13.

Again, we did not need to provide any external configuration for these error messages. The annotation that provided this validation is inline with your field which makes it easy for developers to locate the validation, understand what validation will occur, and make maintenance changes to the validation.



There are also two methods (called events) in this Stripes action that can be invoked. An event is represented by a method in an ActionBean class with the following signature:

public Resolution eventName

Notice that the index method is marked with the @DefaultHandler annotation. Since there are multiple events in this action one of them needs to be specified as the default event. If the URL that calls this action does not specify an event then Stripes will look for an event that is annotated with the @DefaultHandler annotation and execute it.

The View

Now let's add our view logic to the Hello World example. By default, Stripes supports JSP as the standard view technology, however, you can also use other view technologies such as FreeMarker. There is nothing really new to learn here except for the Stripes tag library. The initial view, called Hello.jsp, will allow the user to enter and submit their first name.

        
<%@ taglib prefix="stripes" 
          uri="http://stripes.sourceforge.net/stripes.tld" %>
<html>
  <head>
    <title>Stripes Hello World</title>
  </head>
  <body>
    <stripes:errors/>
    <stripes:form 
        beanclass="com.
                   myco.
                   web.
                   stripes.
                   action.
                   example.
                   HelloWorldAction">
    Say hello to: <br>
    First name: <stripes:text name="person.firstName"/>
    <br>
    Age:<stripes:text name="person.age"/><br>
    <stripes:submit name="hello" value="Say Hello"/>
    </stripes:form>
  </body>
</html>

This JSP is simple to read and maintain. The Stripes tags used for the form and input fields are very close to their HTML counterparts. The stripes:form tag contains an attribute called beanclass which is the fully qualified name of the controller class we previously defined. We could have substituted the action attribute in the stripes:form tag instead of the beanclass attribute. However, the beanclass attribute makes things easier if you need to perform refactoring on a Stripes action. Here is what the stripes:form tag would look like if you use the action attribute:

<stripes:form action="/example/HelloWorld.action">

One of the stripes:input tags specifies a name attribute of person.firstName which will be used to store the value of the input field into the firstName field of the Person object in the controller. Finally, the stripes:submit tag specifies a name attribute which will be used to instruct the Stripes HelloWorldAction class to use the hello event.

Now we are setup to submit a first and last name value to the HelloWorldAction. All we need to do is echo it back out in a separate view.

        
<%@ taglib prefix="stripes" 
       uri="http://stripes.sourceforge.net/stripes.tld" %>
<html>
  <body>
    <stripes:errors/>
    <h2>Hello ${actionBean.person.firstName} your age is 
              ${actionBean.person.age} </h2>
    <p/>
    <stripes:link beanclass="com.myco.web.stripes.action.
                                   example.HelloWorldAction">
      Say Hello Again
    </stripes:link>
  </body>
</html>

This JSP will echo out the person's first and last name field contents by accessing a reference to the action itself. Stripes automatically includes an actionBean request attribute for this purpose which can be accessed with JSTL. Finally, we use a stripes:link tag to create a link back to the HelloWorldAction class so we can enter a different name to echo. We could have also created the stripes:link like this to explicitly reference the index event:

  
<stripes:link 
  beanclass="com.myco.web.stripes.action.
                 example.HelloWorldAction" 
  event="index">Say Hello Again</stripes:link>

Since we annotated the index method with @DefaultHandler, Stripes knows which method to execute without the event attribute.

Convention Over Configuration

Now that we have our Java components in place, we will configure the action mapping to a URL and link it to our two views. Wait a minute this is Stripes, we do not need any external configuration!

While this might sound too good to be true, it is one of the most productive features in Stripes. Stripes uses convention over configuration to map actions to URLs. We also do not need to use an external configuration file to map a symbolic name to the actual views. This means that developers do not have to flip back and forth between configuration files to determine how to navigate between symbolic names, like SUCCESS, to the actual path of the view. There is no need for external wiring of Java and view components. This leads to better maintainability and more productivity.

How does Stripes provide implicit URL mappings to Java action classes without having to configure each action externally or another annotation? This can be explained with the configuration of Stripes in the web.xml file and how it uses sensible defaults to create the URL mappings. First, we need to discuss the Servlet filter called StripesFilter. Here is the default configuration of the StripesFilter in the web.xml file:

        
<filter>
  <display-name>Stripes Filter</display-name>
  <filter-name>StripesFilter</filter-name>
  <filter-class>
    net.sourceforge.stripes.controller.StripesFilter
  </filter-class>
    <init-param>
    <param-name>ActionResolver.UrlFilters</param-name>
    <param-value>/WEB-INF/classes</param-value>
  </init-param>
</filter>

When the Servlet container is started, the StripesFilter performs initialization of its init-param elements. One of the most important init-param elements is the ActionResolver.UrlFilters parameter. This tells Stripes where to look for Stripes relates classes. In this case, Stripes will look for all classes that implement the ActionBean interface in the default /WEB-INF/classes path. Each ActionBean class located is added to a Map along with the default binding URL for that class.

Pages: 1, 2, 3

Next Pagearrow