Struts Quick Start (2)

zhaozj2021-02-16  72

Use an actionforward navigation

The ActionForward object is a configuration object. These configuration objects have unique identifiers to allow them to retrieve them according to meaningful names such as "Success", "failure". The ActionForward object encapsulates the forward URL path and is requested by the processor to identify the target view. The ActionForward object is built from element located in Struts-Config.xml. Below is an example of element examples of Struts, belonging to the range of element.

Type = "packagename.editcustomerProfileAction"

Name = "CustomerProfileform" Scope = "Request">

Based on the result of executing the executor's EXECUTE (...) method, when a value matching the value specified in the element, the next view can be used in the Execute (...) method for convenient Method org.apache.struts.Action.ActionMapping.FindForward (...). The ActionMapping.FindForward (...) method provides an ActionForward object from its local range and from the global scope, which returns to the RequestProcessor to call the next view in RequestDispatcher.Forward (...) or response.sendredirect (...). When the element has the redirect = "false" property or the redirect property does not exist, RequestDispatcher.Forward (...) is executed; when redirect = "true" is, the sendRedirect (...) method will be called. The following example exemplifies the usage of the redirect property:

In Struts-Config.xml element also provides another feature to control the Name property of the element to be interpreted. This element is used in the INPUT property in the combination element, as shown below:

Type = "packagename.editcustomerProfileAction"

Name = "CustomerProfileform"

Scope = "request"

INPUT = "Profile">

The above element contains an input attribute and a forward name; the forward name is the same as . In the configuration described above, when ActionForm.Validate (...) returns a non-empty or non-NULL ActionerRRORS object, requestProcessor will select elements, its name attribute and element INPUT attribute have the same value This behavior is a standard behavior that is confirmed by confirming errors unless the subclass replaces RequestProcessor. Declare the element below, when ActionForm.Validate returns a non-empty or non-NULL ActionerRORS object, the Input property provides a forward URL instead of the name of ActionForward. This is the default configuration when the InputForward property does not exist.

Progressive points to a specific path, if there is no "/" in the path. For advanced or redirects, the URL in Struts is established internally by RequestProcessor with the following structure.

If redirect = true, the URL establishes such as / contextPath / path because the URL is explained in httpservletresponse.sendredirect (...) with "/" starting relative to the root directory of the servlet container.

If redirect = false, URI established, such as / path, because servletContext.getRequestDisptacher (...) uses virtual directory related URLs.

Capture form data

The JSP specification provides a standard approach to extract and save form data from JavaBean when requested, using and . However, this method has led to a strong coupling between the representation layer and Javabeans; in addition, HTML document creators have to understand such components and know that they correctly usage in the page. Because JavaBean can be used by the tab or is established and placed in another server component, it is likely to share lifecycle management issues in JavaBeans in different components. Struts provides a mechanism to extract, save, and verify form data; at the same time it overcomes the shortcomings of and . Here is a new and element.

the above example of mapping a JavaBean packageName.customerProfileForm type name = customerProfileForm (unique identifier) ​​to a element Name = CustomerProfileform; requests the processor to be submitted to the requested path / EditCustomerProfile uniquely identifies. This form is established and used in semantics to use the following static model:

First, we will browse these forms to process semantics until you use simple JavaBeans objects. These objects are implemented as an Actionform object. We will then discuss form processing using the DynaActionForm object, which can support the dynamic collection of attributes when requested. Initialize an actionform object with formtag

This section mentioned earlier, the action URL in the HTML form is mapped to a configuration, and is mapped to a configuration. The URL made by the action attribute in the FormTag is converted to a path structure URL determined in in the deployment descriptor. For expanded mappings, this means that resource extension is the same as the specified. So, a form URL EDITCUSTOMERPROFILE? Customertype = preferred is converted to /EditCustomerProfile.do?customertype=preferred. FORMTAG Call the RequestUtils.createActionform (...) method, find an actionformbean configuration object (ActionformBean is the runtime performance of the element), its name matches the specified name of the corresponding . A new ActionForm instance is built using the element Type property; when the ActionForm instance is not found within the specified activity range, a new instance is established, otherwise formtag calls the existing form bean's actionform.reset ( ...) method to clear and prepare form data to accept the next request. This activity range is specified by the scope attribute in the element; the new ActionForm instance or the existing reinitialization instance is saved within the specified activity range using the Name property.

Save form data with ActionForm

Actionform derived objects are used to save the parameters of the request object, so they are closely linked to the user. A subclass of an ActionForm is a method for accessing the HTTPSERVLETREQUEST object parameter attribute JavaBean. If the ActionForm object is established by FormTag, the RequestProcessor will access the form from the developed activity range in the later FormTAG form translation request; the form you want to obtain is identified by the relevant action mapping. RequestProcessor will then rearrange the form attribute, populate the form with the request, and then call the Validate (...) method of the form object to perform the server-side user input verification. The validate (...) method is called only when the Validate property is set to TRUE only in the ActionMapping object; this is the default behavior. Request.getParameterValues ​​(Parameters) is used to get a string [] object, which is used to form a form fill; the result of the verification should be an ActionerRors object, with org.apache.struts.taglib.html.errostag to display the verification error to the user. ActionForm can also be used to save the current user will be referenced by an intermediate model state that references referenced by a view. An ActionForm class can also be established by RequestProcessor. This occurs in a form that is completed forward to a URL, which is mapped to the controller servlet rather than the form properties specified by JSP and corresponding action mapping. In this case, if you don't find it within the specified activity range, the RequestProcessor will try to find a form bean that could result in a new ActionForm object. The ActionForm object is found in the specified activity range of the name property of the element; when a form object is found by RequestProcessor, it is passed to the execute (...) method of the requesting processor. An ActionForm object can also be established by requesting processors. The form object establishment is to provide an intermediate model state to use the request range JSP; which will ensure that the object does not still exist after the validity expires. By default, all forms are saved as sessions. The presence of the form object in the session may result in waste memory, the same, the request processor must track the lifecycle of the form object saved in the session. A good capture form data is to use a separate form bean for related forms across multi-user interactions. Form beans can also be used to store intermediate model states that can be changed by custom tags during feedback. The label usage is avoided in the view, so it is a good task division, the web production group mainly handles the flag, and the application development group mainly handles the Java code. Label factors exit logic accessed the intermediate model status; when accessing nested objects or when this logic can be complex when listed by aggregate.

Establish an actionform with dynamic properties

A DynaActionForm object is an object with a dynamic property collection. DynaActionForm extends ActionForm, which allows you to create a form object by declaring a struts-config.xml, as follows:

Type = "org.apache.struts.action.dynaactionform">

RequestProcessor is established in the same way as ActionForm, populates and verifies DynaActionForm, for example, the parameters of the request object are filled into the DynaActionform in the element; other parameters are simple. to sum up

Implementing the MVC Semantics (Semantics) for an HTTP protocol based on request / response requires significant time and effort. Select a suitable architecture to solve this problem with a leading project, allowing architects and developers to focus on transaction semantics rather than integration. Struts also provides adequate models such as Struts Validator to declare form validation, TILES to gather a view; these models enhance architecture and greatly simplify design and development tasks. Struts more information and related configurations and installations can be found at http://jakarta.apache.org/struts/userguide/index.html. Because Struts development is an ongoing development, some implementations will also be changed when you read this article, so it is the best version of the release of http://jakarta.apache.org/struts And update to make up this chapter.

转载请注明原文地址:https://www.9cbs.com/read-14591.html

New Post(0)