Stripes is one lets programmer's web develop simple and opens source Java highly effective based on the movement which designs for the criterion the web frame. This article will introduce that Stripes and so on some which and it provides based on the movement frame's difference exists with other like Struts in Ruby on Rails the simplicity.
Stripes is one lets programmer's web develop simple and opens source Java highly effective based on the movement which designs for the criterion the web frame. Traditional Java the web development focuses in borrows the decoupling (Decoupling) to realize its flexibility, but causes many configuration files, extra object, with other resources dispersion. These difficulties create work as many programmer's higher study time and low efficiency. Its result was some Java programmer is attracted by some non-Java frame: Ruby on Rails or Django. Some Java web frame, if Stripes, is starting from these non-Java frame to derive its successful experience: Simple and highly effective development. This article will introduce that Stripes and so on some which and it provides based on the movement frame's difference exists with other like Struts in Ruby on Rails the simplicity.
Figure 1 is the standard in the application procedure normal event flow which and the module writes with Stripes.
Figure 1 standard Stripes flow
|
If you see, its flow basically is a MVC frame. Stripes and other does not have exterior configuration files based on a movement frame's main difference. We will see afterward, Stripes with the annotation peace treaty decides, but the non-disposition enhances delivers is disorderly with the reduction.
Compiles your first Stripe movement (Action)
Let us start through to found Hello now the World routine to understand that the Stripes frame and understands its operation. The HelloWorldAction kind will prompt the user input surname and the name then demonstrated inside another View that first we compile the controller kind.
public class HelloWorldAction implements ActionBean
{
@ValidateNestedProperties (
{
@Validate (field = “firstName”, required = true, on = {“hello”}),
@Validate (field = “age”, required = true, minvalue = 13, on = {“hello”})
})
private 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 kind was one has realized Stripes unique connection ActionBean POJO (Plain Old Java Object, translated the note: Makes an error in pronunciation the gruel). All Stripes movement class must realize this connection lets StripesDispatcher servlet when the movement service for it pours into a ActionBeanContext object. The ActionBeanContext object may let object like request which, response you deposit and withdraw, and so on servlet context servlet API. When majority does not need in the Stripes application to read these first floor API object.
The ActionBeanContext kind also provides the current movement the condition and may increase the information news and the wrong news to the current movement. The ActionBeanContext variable and its read-write method may place inside a base class, because all Stripes movement must realize it.
A Controller kind of other part is looks familiar very much regarding any Java programmer. Some Person object and its read-write method uses for the read-write user the name to give view. Although this is a simple nesting object merely, Stripes may through the Java set, the pan-support, and the subscript attribute realizes the more complex consummation data bundle. Because Stripes may process the complex data bundle, your domain object (Domain Object) may their level entrust with heavy responsibility in other needs. For example: Through Stripes you may a very easy collection domain object information, then with other POJO frame, if Hibernate or EJB3 come to carry on lastingly.
In the Person object variable has Stripes to confirm annotation to use for to guarantee the user in activates the hello method time had already input the name. If the user has not input these two essential variables, the primitive page will be returned, and demonstrated will close wrong news. This confirmation only then, in the hello event is applied for time only will then be activated, because in the annotation attribute has assigned (on = {“hello”}). Stripes will also use the practical default principle, will have an error message according to the proof technique and the variable name. For example, if Person kind of firstName variable in submission time has not provided, the user will see:
Person First Name is a required field.
This news is after carries on Person.firstName engraves reads processing obtains. If has the necessity, these wrong news may provide by the heavy load more customers from the definition function.
In addition has Integer type variable age, is a Person object attribute. Stripes first attempts to request in the life is the person.age parameter transformation is the Integer type, and ties up it to the Person object on. After Person the object age variable is paid the value, Stripes will confirm this Integer value whether to be smaller than 13. If the user has input a string of character, but the non-integer, the user obtains this news:
The value (Mark) entered in field Person Age must be a valid number.
If the user has input one is smaller than 13 integers, the user will see this news:
The minimum allowed value for Age is 13.
Similarly, we are not unnecessary to provide any exterior configuration files for these wrong news. Annotation provides the confirmation with yours variable in the identical position, causes the programmer to locate the confirmation, the understanding confirmation content, and to confirms carries on the maintenance change to be easier.
This Stripes movement also has two to be possible the method which activates (to be called event). The event is in the ActionBean kind has the following characteristic method:
public Resolution eventName
Please note the index method by labelling is @DefaultHandler annotation. Because has many events in this movement, must assign for the default event. Which event if transfers this movement URL not to assign, Stripes searches labelling to have the @DefaultHandler annotation event, and carries out.
Demonstrates the level (View)
Now lets us give Hello the World routine to add on the demonstration level logic. The Stripes default supports JSP is the demonstration level standard technology, but you may also use other demonstration level technology, for instance FreeMaker. Besides the Stripes tag storehouse, has not been assorted the new thing to study. Hello.jsp is the initial demonstration, may let the user input and the submission name.
<%@ taglib prefix= " stripes " uri= " http://stripes.sourceforge.net/stripes.tld " %>
......
<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>
...... |
This JSP legible easy to maintain. But Stripes uses in form and input tag resembles with the corresponding HTML code. stripes:form tag contains a beanclass attribute, its value a controller kind of complete kind of name which defines as us in front. We may use in stripes:form the action attribute to replace the beanclass attribute, but the beanclass attribute may let you carry on restructuring in later to the Stripes movement time is more convenient. If you must use in stripes:form tag use the action attribute, the method is as follows:
| <stripes:form action= " /example/HelloWorld.action " > |
Some stripes:input tag has assigned a named person.firstName attribute, its function is pays controller its storage input value Person the object in the firstName variable. Finally, which event stripes:submit does tag assign a name attribute to tell Stripes the HelloWorldAction kind to use.
We already completed the submission name value to give HelloWorldAction now, was left over is gives in another view its feedback the user.
<%@ taglib prefix= " stripes " uri= " http://stripes.sourceforge.net/stripes.tld " %>
......
<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>
...... |
This JSP reads through one to the movement quotation person the name information and demonstrated. In order to serve this purpose, Stripes increases a named actionBean movement object automatically in the request attribute, supplies JSTL to deposit and withdraw. Finally, we used strips:link tag to establish one to return to the HelloWorldAction link, thus might let us input the different name. We by may explicit found one through the following means to aim at index event's stripes:link:
<stripes:link beanclass= " com.myco.web.stripes.action. example.HelloWorldAction " event= " index " >
Say Hello Again
</stripes:link> |
Because we already used annotation were @DefaultHandler, Stripes do not need the event attribute the index method mark also to know that which method can carry out.
|