Struts User manual translation (1)

Source: Internet
Author: User
Tags define contains error handling http request requires domain java web access

1.1 Go back to the past! (A brief history of struts)

When the Java servlet appeared, many programmers quickly realized that it was a very good thing. It's faster and more powerful than standard CGI, and it's compact and infinitely extensible. But using the endless println () statement to send HTML to the browser is really tired and easy to trouble, so JSP appears, JSP allows the "outside" to write a servlet. As a result, programmers can simply mix Java code with HTML and have all the benefits of a servlet! The sky was the limit!

Soon, Java Web application has become a JSP-centric development. This is not a bad thing in itself, but it does little to solve the problem of process control and other problems peculiar to Web applications.

So it's clear that we need another model.

Many smart programmers realize that they can combine JSP and servlet to publish Web apps. The servlet can be used to handle problems with controls and processes, and the JSP can focus on annoying HTML. As a result, combining the JSP with the servlet became known as Model 2 (probably because JSP was used separately as Model 1).

Of course, there is nothing new here, and JSP Model 2 is quickly pointed out to follow the traditional model-view-control design approach abstracted from the old Smarttalk MVC framework. Java Web Developers now tend to think that the concept of Model 2 and MVC is interoperable. In this manual, we use an example of MVC to describe the architecture of struts, which is named Model 2/MVC design is the most appropriate.

The Struts project was first released by Craig R. McClanahan in May 2000. It provides a standard MVC framework for Java Society. In July 2001, Struts 1.0 was released at the same time as Ioho. The development of Java Model 2 is never very similar.

1.2 MVC Design Method

In the MVC design approach, the process of the application is scheduled by the central controller. The controller transfers the request (HTTP request) to the appropriate processing. Processing is combined with the model, and each processing is like an adapter between a request and a model. The model represents, or encapsulates, the logic or state of an application. The controller controls the process to the appropriate view. Control generally determines the process by querying a series of correspondences (typically from a database or a configuration file). This provides a loose link between the view and the model, making it easy for applications to develop and maintain.

1.2.1 Model: The JavaBeans of System state and system logic

A model based on MVC can usually be divided into two subsystems-the internal state of the system and the behavior that can change the state. Analogy to grammar, we can imagine that state information is a noun and that behavior is a verb.

Many applications use a series of JavaBean to represent the state of the system, and these bean properties represent details of the system state. Depending on the complexity of the system, these beans can be self contained (i.e. they know how to maintain their state), or they know how to get the state of the system from other components, such as databases, search engines, Entity EJB,LDAP services, or whatever.

Large applications typically use a series of possible operations as bean methods and invoke them to maintain state information. For example, a shopping cart bean, which stores session information for each current user, contains information about what the user has chosen to buy. This bean should have a checkout () method that validates the user's credit card and delivers the user's order to the warehouse for packaging and shipping. There are also systems that represent possible operations separately, such as as a session EJB.

On the other hand, in small-scale applications, operations will be embedded into the action class, which is part of the struts control layer. This is ideal when logic is very simple or does not require logic to be reused in other environments.

The Struts framework system is flexible enough to support model access, but we strongly recommend that you separate the logic (what to do) with the role of the action class.

1.2.2 View: JSP and Presentation components

Views based on struts are typically used in JSP technology. JSP pages can consist of static HTML (or XML) that is called "templates" plus dynamic content. These dynamic content is based on the translation of special tags. The JSP environment contains a series of standard tags, such as <jsp:useBean>. In addition, Java EE provides a standard way to define the programmer's own tags, which are organized into custom tag libraries. Struts contains a series of custom tag libraries that can be used to build a multilingual User interface and are perfectly integrated with actionform beans. Actionform Get and verify any information the system needs.

1.2.3 controllers: Actionservlet and Actionmapping

The controller receives the user's request (usually from the browser) and then decides what logic to use to handle it, and then uses a suitable view to react. In struts, the main part of the controller is a servlet that belongs to the Actionservlet class. This servlet is set by defining a series of actionmapping. A actionmapping defines a path to the request URI and provides the full name of an action class. All action is a subclass of Org.apachestruts.action.Action. The action encapsulates the invocation of the logical class, explains the result, and eventually turns the program control into a suitable view component to respond to the request.

Struts, on the basis of standard actionmapping classes, also supports the use of more attributes to manipulate the framework. This allows you to save special information in specific applications and still use the other features of struts. In addition, struts allows you to define "logical names" for logic, for example, an action method that requires the contents of a "main menu" without knowing the specific JSP location. These features can effectively help you separate the control logic (what to do) and the view logic (how to represent it).

Process Control of 1.3 struts

The Struts framework provides several components to implement the control components of MVC, including: Controlling the servlet, developer-defined request processing, and other support objects.

The custom tag library for struts provides direct support for the View section in MVC. Some of them can access the control part of the object, others in the development of the time is very useful. Other tags, including jstl, can also be used in struts. Other performance techniques, such as velocity templates and XSLT, can also be used in struts.

Models in MVC are usually related to specific projects. Struts makes it easy to access the specifics of the application, but gives specific programming to other technologies, such as Jdbc,ejb,object Relational Bridge, or simpler, and so on.

Let's see how all of this is combined.

When initialized, the controller reads the configuration file (Struts-config.xml) and uses it to start the control object, which forms the configuration of struts. The struts configuration (and others) defines the actionmapping[org.apache.struts.action.actionmappings used in the application.

Struts's controller servlet queries the actionmapping, transferring HTTP requests to other components. The request may be transferred to a JSP or a subclass of a developer-written action[org.apache.struts.action.action]. These mapping enable the controller to transfer HTTP requests to the application-related action.

A actionmapping usually consists of some of the following properties:
A request path (or "URI") performs other required attributes of the object type (action subclass) of this request
The action object can process the request and respond to the user (usually the browser) or transfer control to another location. For example, if a user logs on successfully, the login action will transfer the request to the main Menu page.

The Action object can access the applied controller servlet and its methods. When the control is transferred, the action object can indirectly transfer one or more shared objects simultaneously, including JavaBean, as long as they are placed in a standard context shared by the servlet.

For example, the action object can create a bean for a shopping cart, add the item to the cart, then place the Bean in the session, and finally transfer control to the JSP page that displays the contents of the shopping cart. Because each user has his or her own session, they all have their own shopping carts.

In a struts application, most of the logic can be written using JavaBean. An action can invoke JavaBean's properties without knowing how JavaBean works. This encapsulates logic so that the action can focus on error handling and process control.

JavaBean can also be used to manage the forms entered. A key problem of Web application is to get and verify the input information of users. With struts, you can define your own input bean class by inheriting actionform. The Actionform class makes it easy to store and validate input forms in applications. Actionform beans can be automatically stored in the same standard and shared context, so that it can be accessed by other objects, such as action objects or other JSPs.

The form bean can be used by the JSP to collect data entered by the user, an action object to validate the data, and then reproduce the form's contents with a JSP. When validating errors, struts uses a shared mechanism to throw and display error messages.

Another element of struts configuration is Actionformbean. This is a collection of descriptions of objects that are used to establish actionform instances at run time. When a mapping needs a actionform, the servlet uses the name to look for the description object and use it to create an instance of the specified type of actionform.

This is an order of events using the Actionform request:
The controller servlet Gets or establishes the instance controller servlet of the Actionform bean to send the bean to the Action object if the request is to submit data, the action object verifies the data. When necessary, data can be sent back into the form and displayed on the page with some information. Otherwise, the data will be routed to the logic. If the request is to create an input page, the action object fills in the bean with the data required for the input page.
The struts framework includes custom tags that can automatically populate JavaBean fields. The JSP needs to know only the name of the domain and where to submit the form. Other struts tags can automatically display action or actionform information, which only requires a simple combination of page tags. This information is multilingual and can automatically choose the appropriate language according to the area where the user is located.

The struts framework and its custom tag libraries are combined to support multi-language functionality under the Java platform. All domain names and information can be obtained from a single message resource. You need to support other languages simply by adding a resource bundle.

In addition to being multilingual, the benefit of another message resource is to keep the form name consistent. And you can change all the labels and message text in one place.

For the simplest application, the action object usually includes the logic required for the request. However, in many cases, the action object should invoke another object (usually JavaBean) to execute the logic. This allows the action to focus only on errors and control processes, rather than on specific logical processes. In order to be reusable on another platform, logical javabean should not invoke any object that the Web page applies to. The action object should translate the required information from the HTTP request and transfer it to the logical bean through standard Java variables.

For example, for a database application:
Logical Bean joins and queries the database logic bean returns the result to the action Object Action object displays the data in the form bean that the result is requested in HTML
Neither the action nor the JSP need to know where the data comes from, they just need to know how to package and display them.




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.