Using Stripes with Codenvy

Factory URL:


Stripes is a presentation framework for building web applications using the latest Java technologies. The main driver behind Stripes is that web application development in Java is just too much work! It seems like every existing framework requires gobs of configuration. Struts is pretty feature-light and has some serious architectural issues (see Stripes vs. Struts for details). Others, like WebWork 2 and Spring-MVC are much better, but still require a lot of configuration, and seem to require you to learn a whole new language just to get started.

Key Features

  1.  Zero external configuration per page/action (ActionBeans are auto-discovered, and configured using annotations)
  2.  Powerful binding engine that will build complex object webs out of the request parameters
  3.  Easy to use (and localized) validation and type conversion system
  4.  Localization system that works even when you use direct JSP->JSP links
  5.  Ability to re-use ActionBeans as view helpers
  6.  Ridiculously easy to use indexed property support
  7.  Built in support for multiple events per form
  8.  Transparent file upload capabilities
  9.  Support for incremental development (e.g. you can build and test your JSP before even thinking about your ActionBean)
  10.  And a lot of built in flexibility that you only have to be aware of when you need to use it

Create a Project

Log in to your Codenvy workspace and create a New WAR Project say “Stripes”. Project file structure is shown below :

Capture_13

Create pom.xml File and Specify Dependencies

Specify the below dependencies in pom.xml which are prerequisite for the Sripes implementation. Make sure these jars are included under Maven Dependencies.

Capture_14

Pom.xml

<dependencies>
<dependency>
<groupId>net.sourceforge.stripes</groupId>
<artifactId>stripes</artifactId>
<version>1.5</version>
</dependency>
</dependencies>

Create HelloActionBean class

An ActionBean is the object that receives the data submitted in requests and processes the user’s input. It both defines the properties of the form, and the processing logic for the form. To compare to Struts, the ActionBean is like the ActionForm and the Action put together in one class.

It should be mentioned at this point that there is no need for any external configuration to let Stripes know about the ActionBean implementations in an application, nor to tie together the JSP page and ActionBean. All of the information necessary is in the ActionBean itself.

package com.codenvy.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;

private static final String VIEW = "index.jsp";

@DefaultHandler
public Resolution greeting() {
this.msg ="Hello, " + userName + "!";
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; }

}

Create StripesResources.properties and web.xml

###############################################################################
## 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 <;stripes:errors>; tag when there are no nested tags
stripes.errors.header=<;div style="color: #b72222; font-weight: bold;">;Please fix the following errors:<;/div>;<;ol>;
stripes.errors.beforeError=<;li style="color: #b72222;">;
stripes.errors.afterError=<;/li>;
stripes.errors.footer=<;/ol>;
# Resource strings used by the <;stripes:errors>; tag when displaying errors for a
# specific field (e.g. <;stripes:errors field="password">;). If not supplied the
# values above will be used instead.
stripes.fieldErrors.header=
stripes.fieldErrors.beforeError=<;span style="color: #b72222;">;
stripes.fieldErrors.afterError=<;/span>;
stripes.fieldErrors.footer=
# Resource strings used by the stripes:messages tag
stripes.messages.header=<;ul class="messages">;
stripes.messages.beforeMessage=<;li>;
stripes.messages.afterMessage=<;/li>;
stripes.messages.footer=<;/ul>;
# 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=<;i>;{1}<;/i>; 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
<;/stripes:errors>;<;/stripes:errors>;<;/stripes:errors>;

web.xml

Stripes is designed to require as little configuration as possible. To get it up and running you simply need to configure the Stripes Filter with a parameter and the Stripes Dispatcher Servlet in your web application’s web.xml. A pretty standard configuration would look like this:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee

http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"

version="2.4">
<display-name>Stripes HelloWorld Tutorial</display-name>
<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.Packages</param-name>
<param-value>com.codenvy.action</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>StripesFilter</filter-name>
<url-pattern>*.jsp</url-pattern>
<dispatcher>REQUEST</dispatcher>
</filter-mapping>
<filter-mapping>
<filter-name>StripesFilter</filter-name>
<servlet-name>StripesDispatcher</servlet-name>
<dispatcher>REQUEST</dispatcher>
</filter-mapping>
<servlet>
<servlet-name>StripesDispatcher</servlet-name>
<servlet-class>net.sourceforge.stripes.controller.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>StripesDispatcher</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
The ActionResolver.Packages parameter
Stripes auto-discovers your ActionBeans at deployment time by scanning your web application’s classpath. This saves you from having to enumerate all your ActionBeans somewhere, but you do have to give Stripes some starting points. To do so, use theActionResolver.Packages init-param of the Stripes Filter to indicate one or more package roots. Do not use .* at the end of a package; subpackages are automatically included. Use commas to separate multiple package roots.The ActionResolver.Packages is the only required init-param for the Stripes Filter.

Create index.jsp

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head><title></title></head>
<body>
<h1>Stripes Tutorial</h1>
<stripes:form beanclass="com.codenvy.action.HelloActionBean" focus="">
<table>
<tr>
<td>User:</td>
<td><stripes:text name="userName"/></td>
</tr>
<tr>
<td colspan="2">
<stripes:submit name="greeting" value="Greet"/>
</td>
</tr>
<tr>
<td>Result:</td>
<td>${actionBean.msg}</td>
</tr>
</table>
</stripes:form>
</body>
</html>

Build and Run the Application

Upon successful building the application and executing, it processes index.jsp page, where we input a Name, upon clicking the Greet button request again processed by filter and displays the Name in the same form which has been passed as a string.

Capture_11

Capture_12