Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Stripes to realize Java Web Development

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces how to use Stripes to achieve Java Web development, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let Xiaobian take you to understand.

Stripes is an open source action-based Java web framework designed to make programmers' web development simple and efficient. This article will introduce the differences between Stripes and other action-based frameworks such as Struts and some of the simplicity it provides that exist in Ruby on Rails, and use Stripes to implement Java Web development.

Stripes is an open source action-based Java web framework designed to make programmers' web development simple and efficient. Traditional Java web development focuses on Decoupling to achieve its flexibility, but leads to the dispersion of multiple profiles, additional objects, and other resources. These difficulties result in higher learning time and inefficiency for a considerable number of programmers. As a result, some Java programmers are attracted to non-Java frameworks: Ruby on Rails or Django. Some Java web frameworks, such as Stripes, are beginning to learn from the success of these non-Java frameworks: simple and efficient development. This article will introduce the differences between Stripes and other action-based frameworks such as Struts and some of the simplicity it provides that exist in Ruby on Rails.

Figure 1 shows the normal event flow and components in a typical application written in Stripes.

Figure 1 typical Stripes process

As you can see, the process of implementing Java Web development with Stripes is basically a MVC framework. One of the main differences between Stripes and other action-based frameworks is that there is no external configuration file. As we will see later, Stripes uses annotation and conventions rather than configurations to increase output and reduce clutter.

Write your Stripe actions (Action)

Let's start now to understand the Stripes framework and understand its operation by creating Hello World routines. The HelloWorldAction class will prompt the user for a last name and first name and then display it in another View. First let's 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. All Stripes action classes implement this interface to have StripesDispatcher servlet inject an ActionBeanContext object into the service when it is running. ActionBeanContext objects allow you to access objects such as request, response, and servlet context servlet API. Most of the time, you don't have to read these underlying API objects in Stripes applications.

The ActionBeanContext class also provides the status of the current action and can add informational and error messages to the current action. ActionBeanContext variables and their read and write methods can be placed in a base class because all Stripes actions are implemented.

The rest of the Controller class looks familiar to any Java programmer. There is a Person object and its read-write method that reads and writes the user's name to view. Although this is only a simple nested object, Stripes can achieve more complex and sophisticated data binding through Java collections, generic support, and subscript properties. Because Stripes can handle complex data bundles, your domain objects (Domain Object) can be reused in other layers that need them. For example, you can easily gather information about a domain object through Stripes and persist it with other POJO frameworks, such as Hibernate or EJB3.

There is a Stripes validation annotation on the Person object variable to ensure that the user has entered a name when the hello method is activated. If the user does not enter these two required variables, the original page is returned with a related error message. This validation is activated only when the hello event is requested, because it is specified in the property of annotation (on = {"hello"}). Stripes also uses a practical default rule to generate an error message based on the validation method and variable name. For example, if the firstName variable of the Person class is not provided at the time of submission, the user will see:

Person First Name is a required field.

This message is obtained by engraving the Person.firstName. If necessary, these error messages can be overloaded to provide more customer customization capabilities.

There is also a variable of type Integer, age, which is a property of the Person object. Stripes first attempts to convert the parameter that is called person.age in request to Integer and bind it to the Person object. After the age variable of the Person object is paid, Stripes verifies that the Integer value is less than 13. If the user enters a string instead of an integer, the user gets this message:

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

If the user enters an integer less than 13, the user will see this message:

The minimum allowed value for Age is 13.

Similarly, there is no need to provide any external configuration files for these error messages. The validation provided by Annotation is in the same place as your variables, making it easier for programmers to locate validation, understand the content of validation, and make maintenance changes to validation.

There are also two methods (called events) that can be activated for this Stripes action. Events are methods in the ActionBean class that have the following characteristics:

Public Resolution eventName

Notice that the index method is marked @ DefaultHandler annotation. Because there are multiple events in this action, one of them must be specified as the default event. If the URL that calls this action does not specify which event, Stripes looks for the event marked @ DefaultHandler annotation and executes it.

Display layer (View)

Now let's add the logic of the display layer to the Hello World routine. Stripes supports JSP as the standard display layer technology by default, but you can also use other display layer technologies, such as FreeMaker. There is nothing new to learn except for Stripes's tag library. Hello.jsp is the initial display that allows users to enter and submit names.

< @ 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 is easy to read and maintain. Stripes's tag for form and input is very similar to the corresponding HTML code. Stripes:form tag contains a beanclass attribute whose value is the full class name of the controller class we defined earlier. We can replace the beanclass property with the action attribute in stripes:form, but the beanclass attribute makes it easier for you to ReFactor Stripes actions later. If you want to use the action property in stripes:form tag, the method is as follows:

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

One stripes:input tag specifies a property called person.firstName, which is used to pay its stored input value to the firstName variable of the Person object of the controller. * stripes:submit tag specifies a name attribute to tell the HelloWorldAction class of Stripes which event to use.

We have now finished submitting the value of the name to HelloWorldAction, and all that is left is to give it back to the user in another view.

< @ taglib prefix= "stripes" uri= "http://stripes.sourceforge.net/stripes.tld" >

.

< stripes:errors/ >

< h3 > Hello ${actionBean.person.firstName} your age is ${actionBean.person.age} < / h3 > < p / >

< stripes:link beanclass= "com.myco.web.stripes.action. Example.HelloWorldAction" > Say Hello Again

< / stripes:link >

.

This JSP will read the name information of the person and display it through a reference to the action. To achieve this, Stripes automatically adds an action object called actionBean to the property of request for JSTL access. *, we used a strips:link tag to set up a link to return HelloWorldAction so that we could enter different names. We can explicitly create a stripes:link that points to the index event by:

< stripes:link beanclass= "com.myco.web.stripes.action. Example.HelloWorldAction" event= "index" >

Say Hello Again

< / stripes:link >

Because we have marked the index method @ DefaultHandler,Stripes with annotation, we know which method to execute without the event attribute, thus completing Java Web development using Stripes.

Thank you for reading this article carefully. I hope the article "how to use Stripes to achieve Java Web development" shared by the editor will be helpful to everyone. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report