Using Stripes to do Java Web development

Source: Internet
Author: User
Tags java web ruby on rails

Summary:
Stripes is an action-based open source Java Web Framework designed to make the programmer's web development simple and efficient. This article will introduce the difference between stripes and other role-based frameworks such as struts and the simplicity that it provides in the presence of Ruby on rails. Stripes is an action-based open source Java Web Framework designed to make the programmer's web development simple and efficient. Traditional Java Web Development focuses on the use of decoupling (decoupling) to achieve its flexibility, but results in multiple configuration files, additional objects, and other resources scattered. These difficulties result in a considerable amount of programmer's higher learning time and low efficiency. As a result, some Java programmers are attracted to some non-Java frameworks: Ruby on Rails or Django. Some Java web frameworks, such as stripes, are starting to draw on their successful experience from these non-Java frameworks: simple and efficient development. This article will introduce the difference between stripes and other role-based frameworks such as struts and the simplicity that it provides in the presence of Ruby on rails.

Figure 1 is a typical normal event flow and component in an application written with stripes.

Figure 1 Typical Stripes process

As you can see, the process is basically an MVC framework. One of the main differences between stripes and other role-based frameworks is that there is no external configuration file. We will then see that stripes uses annotation and conventions rather than configurations to increase output and reduce clutter.

Write your first stripe action (action)

Let's start by creating the Hello World routines to understand the stripes framework and understand its workings. The Helloworldaction class prompts the user for a last name and first name and then displays it in another view, first we write the controller class.

public class Helloworldaction implements Actionbean {

@ValidateNestedProperties ({
@Validate (field = "FirstName", required = true,
on = {"Hello"}),
@Validate (field = "age", required = true, MinValue = 13,
on = {"Hello"})
})
private person person;
Private Actionbeancontext context;

@DefaultHandler
Public resolution index () {
return new Forwardresolution ("hello.jsp");
}

Public resolution hello () {
return new Forwardresolution ("sayhello.jsp");
}

public void Setperson (String person) {This.person = person;}
Public String Getperson () {return person;}
public void SetContext (Actionbeancontext c) {this.context = C;}
Public Actionbeancontext GetContext () {return context;}
}

The controller class is a pojo that implements the stripes-specific interface Actionbean (Plain old Java Object, "lip porridge"). All Stripes Action classes implement this interface so that the Stripesdispatcher servlet injects a Actionbeancontext object into the service when it runs. The Actionbeancontext object allows you to access the Servlet APIs such as request, response, and servlet context. Most of the time in stripes applications it is not necessary to read these underlying API objects. The Actionbeancontext class also provides the state of the current action and can add informational messages and error messages to the current action. Actionbeancontext variables and their read-write methods can be placed in a base class because all stripes actions are implemented.

Other parts of the controller class are familiar to any Java programmer. There is a person object and its read and write methods are used to read and write the user's name to view. Although this is just a simple nested object, stripes can achieve more sophisticated data binding through Java collections, generic support, and subscript attributes. Because stripes can handle complex data bundles, your domain object (domain object) can be reused in other layers that require them. For example, by stripes you can easily gather information about a domain object and then use other Pojo frameworks, such as Hibernate or EJB3, to persist them.

A Stripes validation annotation is used on the person object variable to ensure that the user has entered the name when activating the Hello method. If the user does not enter these two required variables, the original page is returned and a related error message is displayed. This validation is only activated when the Hello event is applied, because the annotation attribute specifies (on = {"Hello"}). Stripes also uses the utility default law to generate an error message based on the validation method and the variable name. For example, if the FirstName variable for the person class is not provided at the time of submission, the user will see:

Person-Name is a required field.

This message is obtained by carving the person.firstname into the process. If necessary, these error messages can be overloaded to provide more custom functionality for customers.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.