Monday, November 26, 2012

Stripes Hello World Example in Eclipse


Introduction 


Stripes is a simple, fast and lightweight open source framework for building web applications using the latest Java technologies. It is a very friendly framework in a manner that makes the third party integration so easy and quite. It offers low learning curve especially for any java web developer. With Stripes you can write lean and mean code without a bunch of XML configuration files. It is designed to do a lot of the common work for you, but it is less well known framework.














Figure shows Stripes application normal flow of components and events.

                                        
To create Hello World Stripes application you need following files and jars
1.  HelloActionBean
2.  hello.jsp
3.  StripesResources.properties
4.  commons-logging-1.1.1.jar and
5.  stripes-1.5.7.jar
The application file structure will look like this.



Initial Configuration for web.xml

Before getting to start interesting work, we have to configure little boring work of minimal configuration, which stripes need to get up and running. We need to add Stripes dispatcher servlet, and the Stripes filter in our web.xml
  
  
    StripesFilter
    
      net.sourceforge.stripes.controller.StripesFilter
    
    
    
      ActionResolver.Packages
      com.stripes.action
    
  
   
  
    DispatcherServlet
    
      net.sourceforge.stripes.controller.DispatcherServlet
    
    1
  

  
    StripesFilter
    DispatcherServlet
    REQUEST
    FORWARD
  


  
    DispatcherServlet
    *.action
  
  
  
    index.jsp
  

StripesResources.properties

###############################################################################
## Default Resource Bundle file for the Stripes Framework. Values should be
## placed in here for the following:
##   - Form Field Labels, unless you want users to see a non-localized, non-
##     pretty name from the form field declaration (prettied up a little).
##   - Error messages for:
##       - Standard validation error messages
##       - Converter error messages
##       - Error messages used in your custom ActionBean classes
###############################################################################
# Resource strings used by the  tag when there are no nested tags
stripes.errors.header=
Please fix the following errors:
    stripes.errors.beforeError=
  1. stripes.errors.afterError=
  2. stripes.errors.footer=
# Resource strings used by the tag when displaying errors for a # specific field (e.g. ). If not supplied the # values above will be used instead. stripes.fieldErrors.header= stripes.fieldErrors.beforeError= stripes.fieldErrors.afterError= stripes.fieldErrors.footer= # Resource strings used by the stripes:messages tag stripes.messages.header=
    stripes.messages.beforeMessage=
  • stripes.messages.afterMessage=
  • stripes.messages.footer=
# Validation error messages produced by Stripes' built-in converter classes. These # are default error messages and can be overridden on per-field and per-form levels. # Using the 'invalidNumber' error for a field 'age' of a form posting to # '/user/Profile.action', the keys looked for (in order) would be: # 1: /user/Profile.action.age.invalidNumber # 2: /user/Profile.action.age.errorMessage # 3: age.errorMessage # 4: /user/Profile.action.invalidNumber # 5: converter.number.invalidNumber converter.number.invalidNumber=The value ({1}) entered in field {0} must be a valid number converter.byte.outOfRange=The value ({1}) entered in field {0} was out of the range {2} to {3} converter.short.outOfRange=The value ({1}) entered in field {0} was out of the range {2} to {3} converter.integer.outOfRange=The value ({1}) entered in field {0} was out of the range {2} to {3} converter.float.outOfRange=The value ({1}) entered in field {0} was out of the range {2} to {3} converter.enum.notAnEnumeratedValue=The value "{1}" is not a valid value for field {0} converter.date.invalidDate=The value ({1}) entered in field {0} must be a valid date converter.email.invalidEmail=The value ({1}) entered is not a valid email address converter.creditCard.invalidCreditCard=The value ({1}) entered is not a valid credit card number # Validation error messages produced by Stripes' annotation based validations. These # are default error messages and can be overridden on per-field and per-form levels. # Using the 'valueNotPresent' required field error for a field 'username' of a form # posting to '/user/Register.action', the keys looked for (in order) would be: # 1: /user/Register.action.username.valueNotPresent # 2: /user/Register.action.username.errorMessage # 3: username.errorMessage # 4: /user/Register.action.valueNotPresent # 5: validation.required.valueNotPresent validation.required.valueNotPresent={0} is a required field validation.minlength.valueTooShort={0} must be at least {2} characters long validation.maxlength.valueTooLong={0} must be no more than {2} characters long validation.minvalue.valueBelowMinimum=The minimum allowed value for {0} is {2} validation.maxvalue.valueAboveMaximum=The maximum allowed value for {0} is {2} validation.mask.valueDoesNotMatch={1} is not a valid {0} validation.expression.valueFailedExpression=The value supplied ({1}) for field {0} is invalid validation.file.postBodyTooBig=Total upload size of {3} KB exceeds the maximum size of {2} KB

Working with Action Bean 


HelloActionBean is simple POJO class; it implements the ActionBean interface just with a getter and a setter method for the ActionBeanContext. HelloActionBean have two instance level properties msg and userName and three event handler methods sayHello(),doSayHello() and sayHelloWorld() methods.
 
package com.stripes.action;

import net.sourceforge.stripes.action.ActionBean;
import net.sourceforge.stripes.action.ActionBeanContext;
import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.ForwardResolution;
import net.sourceforge.stripes.action.Resolution;

public class HelloActionBean implements ActionBean {
	 private ActionBeanContext ctx;
	 public ActionBeanContext getContext() { return ctx; }
	 public void setContext(ActionBeanContext ctx) { this.ctx = ctx; }

	 private String msg;
	 private String userName;

	 @DefaultHandler
	 public Resolution sayHello() {
	  this.msg = "Hello, Stripes!";
	  return new ForwardResolution(VIEW);
	 }

	 public Resolution doSayHello() {
	  this.userName = "Hello, " + userName + "!";
	  return new ForwardResolution(VIEW);
	 }

	 public Resolution sayHelloWorld() {
		 this.msg = " Hello, World!!!";
		 return new ForwardResolution(VIEW);
	}
	 public String getMsg() { return msg; }
	 public void setMsg(String msg) { this.msg = msg; }
	 public String getUserName() {  return userName; }
	 public void setUserName(String userName) {  this.userName = userName; }
	 
	 private static final String VIEW = "/WEB-INF/jsp/hello.jsp";
}
                   Little bit closer look at the Event Handlers and Resolution
Event Handlers
Note: Event handler methods are public, rerun Resolution object and takes no any argument. There is one DefaultHandler per action bean. The DefaultHandler event  is annotated with @DefaultHandler annotation, In our example Default Handler is on the sayHello().If there more than one DefaultHandler annotation on method, you will get an exception. The doSayHello() and sayHelloWorld() methods are also event handler methods.







Resolutions
A resolution tells Stripes what to do next in response to the current request.
Note: we are returning the instances of the Resolution interface directly from event handler method of ActionBean Class instead of returning "SUCCESS" and "FAILURE".  You can add/send arbitrary parameters to the client, as like this :
return new ForwardResolution("/abc.jsp" ).addParameter("param_name" , “param_value”);




Working With JSP

index.jsp


index.jsp as a default file when the user accesses the application with the base URL, such as http://localhost:8080/StripesExample

hello.jsp

<%@page contentType="text/html;charset=ISO-8859-1" language="java"%>
<%@taglib prefix="s" uri="http://stripes.sourceforge.net/stripes.tld"%>


Hello World Stripes Application!


Hello, World Stripes Application

Result: ${actionBean.msg} ${actionBean.userName}
Enter Name:
sayHello sayHelloWorld
Following Figure is an illustration of the JSP to ActionBean Binding














 

 Run it

You can access the application directly as below
http://localhost:9081/StripesExample

The index page will be display as like this.














Enter the name and to submit the form click on submit button, the message will be display as like this.














finally click on the sayHelloWorld hyper link the message will be like this.















Summary:  Stripes is a simple and lightweight open source framework for building web applications using the latest Java technologies.It makes the integration so easy with third party libraries and defacto standard orm frameworks  and DI libraries, such as it providing Guice Integration with Stripes-Guice, Stripes Spring JPA, Stripes with WebSphere,JQuery's Uploadify with Stripes, Hibernate configuration,Hibernate Infrastructure, even AJAX is so easier. It do a lot common work for you. give a try


4 comments:

  1. Nice article. Can you please tell me from where can I learn more about Stripe, like some of the advanced features?

    ReplyDelete
    Replies
    1. you can find more advance things from stripes site.
      http://www.stripesframework.org/display/stripes/Home

      Delete
  2. Hello,
    I am running your app and deploy it on tomcat 6.0 and I am constantly getting and HTTP Status 500 error. The first line says "The absolute uri: http://stripes.sourceforge.net/stripes.tld cannot be resolved in either web.xml or the jar files deployed with this application" Could you help me with that?

    ReplyDelete
  3. can give this example to download

    ReplyDelete