Struts1.1 introduction

xiaoxiao2021-03-06  117

For a few more time, I would like to ask about Struts, I have a note. Please have interest to prevent research.

(1)

1 Introduction

1.1 Model-View-Controller (MVC) design mode

FixME - requires a general introduction to this pattern. (Translation: You can refer to the "design pattern" of Machinery Industry Press.)

1.2 Map MVC Concept into Struts Components

The Struts architecture implements the concept of Model-View-Controller design mode, which maps these concepts to the components and concepts of the web application.

Each of the main components in this architecture will be discussed in detail below.

1.3 Model: System Status and Business Logic Javabeans

The MODEL portion in the MVC-based system can be subdivided into two concept-system internal states, and the behavior of the state can be changed. In terms of syntax terms, we can use status information as named (things), regard behavior as verbs (changes in things).

In general, your application represents the state inside the system as a set of one or more JavaBeans, using the property (Properties) to represent the details of the status. Depending on your application's complexity, these Beans can be self-contained (in some way to know how to keep their status information permanently), or can be positive (Facades), know how to be from outside when being requested Information in data sources (such as databases). Entity EJBS is often also used to represent internal states.

Large applications often represent system possible business logic behavior as a method called Beans that can be maintained. For example, you have a shopping cart bean saved in the session for each current user, which means that the current user decides the properties of the item. This bean has a checkout () method to verify the user's credit card, which will be ordered to the warehouse to select the goods and shipments. Other systems represent the same behavior, perhaps use Session EJBS.

In some small applications, the same behavior may be embedded in the Action class as part of the Controller. This is appropriate when logic is very simple or does not want to reuse these business logic in other environments. The STRUTS framework supports all these methods, but it is recommended to separate business logic ("what") and Action class ("what to do") separate.

1.4 View: JSP page and representation of components

The VIEW portion in the Struts-based application is usually built using JSP technology. The JSP page contains a static HTML (or XML) text called "Mode Text", plus the dynamic content of the inserted interpretation of special behavior markers. The JSP environment includes its use of a set of standard behaviors that are described by JSP specifications, such as . In addition, there is a standard mechanism for defining your own tag, these customized tag organizations in the Custom Tag Library.

The Struts includes a broad easy to create a user interface, and fully international custom tag libraries that work with an actionform beans as part of the system model part of the system MODEL. The use of these tags will be discussed in detail later.

In addition to the JSP page and its behavior and custom tags, business objects often need to be able to process themselves into HTML (or XML) based on their current state when they are requested. The output from these objects can be easily used to use the standard behavior, including the JSP page in the result.

1.5 Controller: ActionServlet and ActionMapping

The Controller section of the application is concentrated from the client receiving request (typically a user running a browser), which determines what business logic function is executed, and then generates the responsibility of the next user interface to give an appropriate View component. In Struts, the basic component of Controller is a servlet for an ActionServlet class. This servlet is configured by defining a set of mappings (described by Java Interface ActionMApping). Each map defines a path with the requested URI and an Action class (a class that implements an Action interface), which is responsible for performing the expected business logic, then assigns control to the appropriate View component. Create a response. The Struts also supports the ability to use an ActionMApping class that includes additional properties other than standard properties necessary to run the framework. This allows you to save additional information specific to your application, while still using the rest of the frame. In addition, Struts allows you to define controls to redirect logical names, such a behavior method can request the Main Menu page (for example) without knowing the actual name of the corresponding JSP page. This feature greatly helps you separate control logic (what to do next) and display logic (what is the name of the corresponding page).

2. Create a Model component

2.1 Overview

The demand document you use for the application is likely to focus on the creation user interface. However, you should guarantee that the processes needed for each submitted request must be clearly defined. In general, developers of Model components focus on creating JavaBeans classes that support all feature requirements. The precise characteristics of Beans, which requires special application requirements, dependent on specific demand changes, but they can usually be divided into several types discussed below. However, it is useful to be a short review for the "range" concept because it is related to Beans.

2.2 JavaBeans and scope

In a web-based application, JavaBeans can be saved in (and access) some of the collection of "properties". Each collection has a set of rules for the surplus and the Save Beans visibility. In general, these rules that define lifetime and visibility are called these beans. The following terms are used in the JSP specification (the equivalents defined in a servlet API in parentheses):

Page - Beans visible in a separate JSP page, survival is limited to the current request. (Partial variables in the service () method) Request - Beans visible in a separate JSP page, including all page or servlets that are included in this page or redirected from this page. (Request property)

Session - Participated in all JSPs and servlets of a specific user session, beans visible, across one or more requests. (Session attribute)

Application - All JSP pages of a web application and Beans visible from servlets. (Servlet context attribute)

Remembging the JSP page with the same web application and servlets sharing the same set of bean collections. For example, a bean is saved as a request attribute in a servlet, just like this:

Mycart mycart = new mycart (...);

Request.setttribute ("Cart", MyCart);

The JSP page that will be immediately redirected immediately by this servlet to see a standard behavior tag, like this:

2.3 actionform beans

The Struts framework typically assumes an actionform bean that you have created for each of your applications (a class that implements an Actionform interface). If you define such beans in your an actionMapping profile (see "Creating Controller Components"), Struts's Controller Servlet will automatically perform the following services before calling the appropriate action method:

Check if the user's session beans have an instance of the Bean in the SESSION of the user with the appropriate keyword.

If there is no such session range, a new bean is automatically created and added to the user's session.

The corresponding set method is called for each name corresponding to a attribute in the bean. This operation is similar to when you select all attributes using standard JSP behaviors .

Updated ActionForm Bean will be passed to the actOn class's Perform () method to make these values ​​to take effect immediately.

When you write your Actionform Beans, remember the following principles:

The ActionForm interface itself does not require special implementation methods. It is used to identify the role of these specific Beans throughout the architecture. Typically, an actionform bean includes only attribute GET methods and SET methods, no business logic.

Typically there is only a little input verification logic in an ActionForm Bean. The main reason for such beans is to save most of the recent value entered by the user-related forms - even when the error is detected - the same page can be rebuilt, accompanied by an error message, so that the user only needs Correct the wrong field. The authentication of the user input should be executed in the Action class (if it is very simple), or executes in the appropriate business logic beans.

Define an attribute (with the associated getxxxx () and setxxxx () method) for each form. The field name and attribute name must match the JavaBeans agreement. For example, an input field called UserName will cause the setUserName () method being called.

You should pay attention to the meaning of "form" as discussed here and should not have to correspond to a separate JSP page in the user interface. It is also common to extend to a "form" in many applications to multiple pages in many applications. Think about it, for example, the user interface of the navigation installer usually used when installing a new application. Struts encourages you to define a separate Actionform Bean that contains all field properties. Regardless of the fields actually display on which page is displayed. Similarly, different pages of the same form should be submitted to the same Action class. If you follow this suggestion, in most cases, the page designer can reorganize fields in different pages without changing the processing logic.

2.4 System status beans

The actual state of the system is usually represented as a set of one or more JavaBeans classes, which defines the current state. For example, a shopping cart system includes a bean that represents a shopping cart, which is maintained for each individual shopper, including (in other things), a group of shoppers currently choose to purchase items. Divided, the system also includes saving user information (including their credit cards and delivery addresses), which can get the directory of the project and their current inventory levels.

For small-scale systems, or for status information that does not require long-term saving, a set of system status beans can contain information for specific details that all systems have experienced. Alternatively, the system status beans represents information that is permanently saved in some external databases (for example, the CustomerBean object corresponds to a specific line in Table Customers), and creates or clears from the server's memory when needed. In large-scale applications, Entity EJBS is also used in this purpose. 2.5 Business Logic Beans

You should encapsulate functional logic in your application into a method called for JavaBeans designed for this purpose. These methods may be part of the same class as the system status beans, or may be in a separate class specifically performing business logic. In the latter case, you usually need to pass system status beans to these methods as parameter processing.

For the maximum reusability of code, business logic Beans should be designed and implemented to do not know that they are executed in a web application environment. If you find that you must Import a Javax.Servlet in your bean, you will bundle this business logic in a web application environment. Consider reorganizing things to make your Action class (part of the Controller task) Translate all information requested from the HTTP request to be called to your business logic Beans property set method, then you can issue an Execute () Call. Such a business logic class can be reused in an environment other than the Web application that is initially constructed.

Depending on your application's complexity and scope, commercial logic Beans can be a normal JavaBeans interacting with system status beans that is passed as parameters, or use JDBC to invoke ordinary JavaBeans for accessing the database. For larger applications, these Beans are often state or stateless EJBs.

2.6 Question out: Accessing Relationship Database

Many web applications use a relational database (accessed through a JDBC Driver) to save the application-related permanent data. Other applications use Entity EJBS to implement this purpose, they delegate EJBS to determine how to maintain permanent status. If you are using EJBS to implement this purpose, follow the client design patterns described in the EJB specification.

For web applications accessible based on direct database, a normal design problem is how to generate an appropriate JDBC connection object when accessing the low-level database. There are several ways to solve this problem - the following principles describe a method of recommendation:

Create or get a CONNECTIONPOOL class that allows a set of database connections to be shared by multiple users. Struts does not include such a class, but there are many such classes to get.

When the application is initialized, a servlet with a "Launch" value is defined in the application deployment descriptor. We will call this servlet to start servlet. In most cases, this servlet does not need to handle any request, so there is no will point to it.

In the init () method that starts the servlet () method, the instance of a ConnectionPool class is configured and saved as a servlet context property (from the JSP's point of view, the bean is equivalent to an Application range). It is convenient to configure the connection buffer pool based on delivery to the start-up servlet initialization parameter.

In the destroy () method of the servlet, the logic of the connection opened by the released buffer pool is included. This method will be called when the Servlet container ends this application.

When the Action class needs to call a method in the business logical bean that requires database coupons (for example, "INSERT A New Customer"), the following steps are required: Get a join buffer pool object from the ServelT Context property for this web application.

Call the open () method of the coupling pool object to get a join in the Action class call.

Call the appropriate method in the business logic bean, passed the database access object as a parameter to it.

Call the Close () method in the allocated join, which will cause this connection to reuse the reuse of other requests to be returned to the buffer pool.

A usual programming error is to forget to return the database to the buffer pool, which will eventually lead to all the coupling. Be sure to be sure that the logic of the Action class always returns the join, even when a business logic bean throws a violation.

Follow the above recommended design patterns that you can write your business logic, don't need to worry about how they get a JDBC join - simply contains a Connection parameter in any way to access the database. When your business logic class is used in a web application, allocation and release the appropriate join is the responsibility of the Action class. When you use the same business logic class, for example, in a batch work, provide an appropriate join is the responsibility of that application (this does not need to be obtained from the buffer pool, because most batch work runs on a single single Thread environment).

3. Create a View component

3.1 Overview

This chapter focuses on the task of creating the VIEW component in the application, mainly using JSP technology. In addition, Struts provides support for the establishment of an international application in addition to the interaction with the input form. Several other related topics related to View are also simply discussed.

3.2 International Message

A few years ago, application developers can consider only one language that only uses only one language (or sometimes two) and usually only one quantity of expressions (such as dates, digital, currency values). However, the explosive growth of web technology-based applications, as well as to expand these applications over Internet or other widely accessed networks, have made the country's boundaries to immune. This change is converted into an internationalization of the application (often referred to as "I18n" because 18 is the number of letters between the letters "I" and the letters "N") and localization needs.

Struts is built on the Java platform to build international and localized applications. The key concept that needs familiarity is:

Locale - Basic support Internationalized Java class is java.util.locale. Each Locale represents a special country and language selection (plus an optional language variable), and a set of formats assume, such as numbers and dates, and more.

ResourceBundle - Java.util.resourceBundle class provides basic tools that support multiple language messages. To view more about internationalization about the ResourceBundle class and your JDK version of the documentation.

PropertyResourceBundle - A standard implementation of a RESOURCEBUNDLE class allows you to define resources using the same "name = value" syntax with the initialization Properties file. This is very convenient for preparation resource packages that use for a web application because these messages are usually for text.

The MessageFormat - Java.Text.MessageFormat class allows you to replace a message in a message string using the parameters (in which case is a message obtained from a resource package). This is useful in the occasion you create a sentence, but the word will appear in a different order in different languages. The placeholder string {0} in the message is replaced with the first runtime parameter, and {1} is replaced with the second runtime parameter to push. MessageResources - Struts class org.apache.struts.util.MessageResources enables you to make a resource package to make a database and allow you to request a specific message for a specific Locale (usually corresponding to the current user), Instead of running the default Locale request message in which the server is run.

For an international application, create an attribute file containing messages for each language in accordance with the steps described in the JDK document package. The following example will be given.

Suppose your source code is built in the package com.mycompany.mypackage, so it is saved in a directory called (relative to your source directory) COM / MyCompany / mypackage. To create a resource package called com.mycompany.mypackage.myResources, you should create the following files in the directory COM / MyCompany / MyPackage:

MyResources.properties - Message containing the default language of your server. If your default language is English, you may have a such entry:

Prompt.hello = Hello

MyResources_xx.properties - The same message containing the ISO language programming to "XX" (see the Java Document page for the ResourceBundle gets a current list). For the French version of the above news, you can have this entry:

Prompt.hello = Bonjour

You can have any of the resource package files you need.

When you configure the Controller Servlet in the web application deployment, you need one thing that you need to define in an initialization parameter is the base name of the application's resource package. In the above case, this should be com.mycompany.mypackage.myResources.

3.3 Forms and FORMBEAN interaction

Most web developers have used HTML standard performance to establish a form, such as using the tag. The user wants the interactive program to have a certain behavior. One of these expectations is related to the error handling - if the user has an error, the application should allow them to only modify the part you need to modify - without the need to retrieve the current page or form Any other information.

Using standard HTML and JSP programming, this expectation is monotonous and heavy. For example, an input element of a username is like this (in JSP)

Value = "<% = loginbean.getusername ()%>>>

This is hard to knock your pair, you will make HTML developers without programming concepts, and will cause problems in the HTML editor. Instead, Struts provides a comprehensive mechanism based on JSP 1.1 custom tag library function to establish a form. The above situation uses Struts to handle the like this:

There is no need to explicitly involve JavaBean from which the initial value is obtained. This will be automatically processed by the frame.

3.3.1 Establishing a Find a complete registration form using the Struts How to demonstrate how Struts greatly reduces the pain of processing forms relative to the JSP functionality directly using HTML and standard. Consider the page called Logon.JSP (from Struts):

<% @ Page language = "java"%>

<% @ Taglib Uri = "/ Web-INF / STRUTS.TLD" prefix = "struts"%>

<struts: message key = "logon.title" /> </ title></p> <p><body bgcolor = "white"></p> <p><struts: errors /></p> <p><struts: form action = "logon.do" Name = "logonform"</p> <p>TYPE = "org.apache.struts.example.logonform" /></p> <p><Table Border = "0" width = "100%"></p> <p><tr></p> <p><TH align = "Right"></p> <p><struts: message key = "prompt.username" /></p> <p></ tH></p> <p><TD align = "left"></p> <p><struts: text name = "username" size = "16" /></p> <p></ td></p> <p></ TR></p> <p><tr></p> <p><TH align = "Right"></p> <p><struts: message key = "prompt.password" /></p> <p></ tH></p> <p><TD align = "left"></p> <p><struts: password name = "password" size = "16" /></p> <p></ td></p> <p></ TR></p> <p><tr></p> <p><TD align = "right"></p> <p><struts: submit></p> <p><struts: message key = "button.submit" /></p> <p></ Struts: Submit></p> <p></ td></p> <p><TD align = "right"></p> <p><struts: reset></p> <p><struts: message key = "button.reset" /></p> <p></ struts: reset></p> <p></ td></p> <p></ TR></p> <p></ table></p> <p></ struts: form></p> <p></ body></p> <p></ html></p> <p>The following entry is based on this example of the key features of the formation of the form in Struts:</p> <p>Taglib instructions tell the JSP page compiler where to find the tag library descriptor for the Struts tag library. In this case, we use Struts as a prefix to identify tags from this library, but you can use any prefix you want to use.</p> <p>This page uses several Message tags to find an international message string from a MessageResources object containing all resources of this application. In order to make this page work, the following message keyword must be defined in these resources: logon.title - Title of the registration page</p> <p>Prompt.username - a "UserName:" Tips String</p> <p>Prompt.password - a "password:" prompt string</p> <p>Button.submit - Tags of "Submit" button</p> <p>Button.Reset - Tags of "reset" button</p> <p>When the user registers, the application can save a Locale object in the user's session. This Locale will be used to select a message in the appropriate language. This makes it easy for the user to switch the user's option - only change the saved locale object, and all messages are automatically switched.</p> <p>Errors tag Displays any error message saved by a business logic component, or if there is no error message, it doesn't have anything. This marker will be described in depth below.</p> <p>The Form marker is processed by an HTML <form> element based on the specified property. It also associates all the fields in this form with a session range that holds the session range under the keyword LogonForm. This bean is used to provide an initial value for all input fields with names that match the attribute names in the bean. If the appropriate bean is not found, a new bean will be automatically created, using the specified Java class name.</p> <p>The Text tag is processed for an HTML <input> element of "Text". In this case, the number of character locations occupying the browser screen is also specified. When the page is executed, it is the current value of the corresponding bean's UserName property (that is, the return value of the getUserName ()).</p> <p>A Password tag is similar. The difference is that the browser will respond to the asterisk character when the user is knocked into their password instead of the input value.</p> <p>Submit and RESET marks the corresponding buttons in the form low. The text tag of each button is built using the Message tag, and there is a prompt, so these values ​​are international.</p> <p>3.3.2 Enter field type support</p> <p>Struts defines tags for all of the following types of input fields with an overlink with its corresponding reference information.</p> <p>Checkboxes</p> <p>Hidden field</p> <p>Password input field</p> <p>Radio button</p> <p>RESET button</p> <p>SELECT list and embedded</p> <p>Options</p> <p>Submit button</p> <p>TEXT input field</p> <p>Textareas</p> <p>In all cases, a field tag must be nested in an Form tag, which knows which bean is used to initialize the displayed value.</p> <p>3.3.3 Other useful representations</p> <p>Several other tags in the Struts tag library are helpful for establishing a user interface:</p> <p>Enumerate repeats a marker for each element of a specified collection (which can be an enumeration, a HashTable, a vector or an object array).</p> <p>GetProperty Gets the specified property from the specified bean, and the rest of this page exists as a bean in a PAGE range. This is a convenient way to access a collection of enumerate.</p> <p>IfAttributeExists evaluates the marker only when a specified property is present in a specified range.</p> <p>IfttributeMissing is only available when a specified property does not exist in a specified range.</p> <p>Ifparameterequals evaluates the marker only when a specified request parameter has a specified value.</p> <p>IfParameterNotequals delivers the marker only if the specified request parameter does not have a specified value or does not exist.</p> <p>IfParameterNotnull is only available to the marker in this request in this request in one specified request parameter and the length is greater than 0. IfParameternull delivers the marker only if the specified request parameter is not included in this request or the length is equal to 0.</p> <p>Iteerate repeats a marker for each element in a specified collection (can be a Collection, an Iterator, a MAP, or an object array). This tag replaces the Enumerate tag in the Java2 environment.</p> <p>Link generates a super connections that automatically applies the URL programming when there is no cookie support to maintain the SESSION state.</p> <p>Parameter Processing the value of the specified request parameter, properly filtering the character in HTML.</p> <p>Property displays the bean property named in a form - use this tag when the property should be read-only instead of the Text tag.</p> <p>3.3.4 Automatic form verification</p> <p>In addition to the forms of forms and beans described above, if your bean knows how to verify that it receives the input fields, Struts also provides an additional mechanism. To take advantage of this feature, your bean class implements the ValidatingActionform interface instead of an Actionform interface. A ValidatingActionForm adds an additional method signature:</p> <p>Public String [] Validate ()</p> <p>For a method called by the Controller Servlet's Assembly, the bean property is already assembled but before the corresponding behavior class is called, the validate () method has the following options:</p> <p>Perform appropriate verification found No error - Returns NULL or a non-0 length string array, and the Controller Servlet will continue to call the appropriate Action class Perform () method.</p> <p>Perform appropriate verification found There is an error - return a string array of the error message key (enter the application's MessageResources package) that should be displayed. The Controller Servlet saves this array as the request attribute suitable for the <struts: ERRORS> tag and the control redirects back the input form (identified by this actionMApping INPUTFORM property).</p> <p>As mentioned earlier, this feature is completely optional. If your Form bean only implements an ActionForm interface, the Controller Servlet will assume that any request is complete by the Action class.</p> <p>3.4 Other representations</p> <p>Although your application's appearance and feel can be built based on standard JSP capabilities and Struts custom tag library, you should also consider expanding other improvement components reuse, reduce management burden, or decreased technologies. Several optional techniques are discussed below.</p> <p>3.4.1 Applicable custom tags</p> <p>Outside the custom tag provided with the Struts library, it is easy to establish tags specific to the application you created to help create a user interface. Struts included examples with the establishment of the following only to implement this application's tag demonstrate this principle:</p> <p>Checklogon - Check the existence of a special session object, if there is no need to redirect to the registration page. This is used to capture such a situation, and the user makes a page on your application to make a bookmark and try to skip the registration, or the user's session timeout.</p> <p>LinkSubscription - Generates an overlink for a detailed order page, which will be passed as a request attribute as a request attribute. This is used when listing the order associated with a user and provides an editing or deleting a set of order.</p> <p>Linkuser - Generates an overlink for a specific page of a user, which will pass it as a primary keyword value as a request attribute.</p> <p>These tagged source code in the SRC / EXAMPE directory, in the package org.apache.struts.example, there are some other Java classes in this application. 3.4.2 Page components with files</p> <p>Creating a full representation in a JSP file (including Dynamic Data for access requests for Beans) is a very ordinary design method, in the example program included in Struts. However, many applications require a plurality of logical independent parts of your application in a single page.</p> <p>For example, an inlet application can have some or all of the following functions on the main page of the entrance:</p> <p>Visit a search engine for this entry.</p> <p>One or more "providing news" is displayed, including the title of interested in accordance with the user's registration information.</p> <p>Access the topic of discussions related to this entrance.</p> <p>If your entrance provides free mail account, there must be a "mail waiting" tip.</p> <p>If you can divide your work, allocate different developers to do different fragments, then the development of different fragments of this site will be simpler. Then, you can use the INCLUDE capabilities of JSP technology to combine these clips into a separate page. There are two types of include, depending on how time you want to output, what time:</p> <p>The include directive (<% @ include file = "xxxxx"%>) is handled at the JSP page. It is used to include HTML code that does not require changes in request. It puts the text included as static text, very like C or C #include instructions.</p> <p>Include Behavior (<JSP: Include Page = XXXXX "Flush =" />) is processed during the request and is transparently processed by the server. This means you can perform include Include by nested it in a mark similar to ifparameterequals.</p> <p>3.4.3 Picture Processing Components</p> <p>Some applications require dynamic generation pictures, just like a price chart at a stock market report site. There are usually two different ways to achieve this requirement:</p> <p>Treat a URL of a URL that performs a servlet request. This servlet will use an image library to generate a picture, set the appropriate Content type (for example, image / gif), and send the image stream to the browser. The browser will display pictures like it is received from a static file.</p> <p>Processing an HTML code requires downloading a Java Applet for an image that creates a request. You can receive this image by setting the appropriate initialization parameter for this applet in the process of processing, or you can make this applet to establish your own join to receive these parameters.</p> <p>4. Create a Controller component</p> <p>4.1 Overview</p> <p>Now we understand how to construct the Model and View components of your application, and now it is when it is concentrated to the Controller component. The Struts includes a servlet that implements the main functionality of a request URI to a behavior class. So your main responsibility related to Controller is:</p> <p>Write an Action class for each of the possible received logic (that is, an ACTION interface implementation)</p> <p>Write a ActionMApping class defining class names and other information related to each possible mapping (that is, an implementation of an ActionMApping interface)</p> <p>Write behavioral mapping profile (using XML) to configure the Controller Servlet.</p> <p>Updating your application Update Web Application Deployment File (XML) is used to include the required Struts component.</p> <p>Add appropriate Struts components to your app.</p> <p>4.2 Action class</p> <p>The Action interface defines a single method that must be implemented by an Action class, like this: Public ActionForward Perform (ActionServlet Servlet,</p> <p>ActionMapping mapping,</p> <p>Actionform Form,</p> <p>HTTPSERVLETREQUEST REQUEST,</p> <p>Httpservletresponse response</p> <p>THROWS IOEXCEPTION, SERVLETEXCEPTION;</p> <p>The goal of an Action class is to process this request, then return an ActionForward object that identifies the JSP page, and the control should redirect this JSP page to generate a corresponding response. In Model 2 design mode, a typical Action class will implement the following logic in its Perform () method:</p> <p>Verify the current state of the user session (for example, check the user has successfully registered). If the Action class discovery is not registered, the request should be redirected to the JSP page that displays the username and password for registration. It should be done because the user may try to enter your application from "middle" (that is, from a bookmark), or because the session has timed, a servlet container creates a new session.</p> <p>If the verification has not occurred (because the FORM BEAN that implements the ValidActionActionform interface, verify the properties of this form bean are required. If a problem is found, as a request attribute saves the right error message keyword, then the control redirection back the input form such that the error can be corrected.</p> <p>Execute the required processing to process this request (for example, saving a row in the database). This can be done with code embedded in the Action class itself, but a suitable method of a business logic bean should usually be called to execute.</p> <p>The update will be used to create a server-side objects for the next user interface page (typically the Request range or session range beans, definition you need to keep these items available).</p> <p>Returns an appropriate ActionForward object that identifies the JSP page that generates a response, based on a newly updated Beans. Typically, you will pass the ActionMapping object in your reception (if you use a local logical name on this map) or in the Controller Servlet itself (if you use a global logical name) to call Findforward () Get a reference to such an object.</p> <p>The design points to remember when programming for the Action class include the following:</p> <p>Controller servlet only creates an instance of your Action class for all requests. This way you need to write your Action class to run correctly in a multi-threaded environment, just like a service () method you must safely write a servlet.</p> <p>The most important principle for helping thread security program is to use only local variables in your Action class rather than instance variables. Local variables are created in a stack assigned (by your JVM), so there is no need to worry about sharing them.</p> <p>Although it should not, Beans that represent the Model section in your system may still throw a violation. You should capture all such violations in the logic of your Perform () method, and record them in the application's log file by performing the following statement:</p> <p>Servlet.log ("Error Message Text", Exception;</p> <p>As a generic rule, there are few resources that are rare and maintain them in requests from the same user (in the user's session), which will cause scalability. You should work hard to release such resources (e.g., database coupling) before you want to redirect to the appropriate View component - even when a BEAN you calls thrown a violation. In addition, you will want to prevent a very large Action class. The simplest implementation is to embed your functional logic into the Action class itself, rather than writing it in a separate commercial logic beans. In addition to making the Action class is difficult to understand and maintain, this method also makes it difficult to reuse these business logic code because the code is embedded in a component (Action class) and is bundled in a web application environment.</p> <p>Included in the example program in Struts extends this design principle because the business logic itself is embedded in the Action class. This should be considered as a bug in this sample application design, rather than a Struts architecture, or a way to be equivalent.</p> <p>4.3 ActionMapping implementation</p> <p>In order to successfully run, Struts's Controller Servlet needs to know how to map to an appropriate Action class for each URI. Knowledge packages that need to be understood are in a Java interface called ActionMapping, which has the following properties:</p> <p>ActionClass - The complete Java class name used for this mapped Action class. The first one-specific mapping is used, and an instance of this class will be created and saved for later reuse.</p> <p>Formattribute - The name of the bean range of the Session range, the current map of ActionForm is saved under this bean. If this property is not defined, there is no ActionForm being used.</p> <p>FormClass - A complete Java class name used for this mapped Actionform class. If you are using support for Form Beans, an instance of this class will be created and saved (in the current user session)</p> <p>PATH - Match the URI path to select the request for this map. Look at how to match the example below.</p> <p>Struts included an ActionMApping interface in a class called ActionMappingBase. If you don't need to define any additional properties for your own mapping, although this class is described as your ActionMApping class, you can configure it to the bottom. However, defining an actionMApping implementation (mostly the extended actionMAppingBase class) is also possible to include additional properties. The Controller Servlet knows how to automatically configure these custom properties because it reads the configuration file using the Struts Digester module.</p> <p>Included in the example program of Struts, this feature is used to define two additional properties:</p> <p>Failure - If the Action class detects some of the issues of the input field it receive, the control should be redirected to the context-related URI. Typically, it is a request swivel JSP page name, which will cause a form to be reset (including an error message for the Action class setting, and most of the most recent input values ​​from Actionform Beans).</p> <p>SUCCESS - If the Action class successfully executes the request, the control should be redirected to the context-related URI. Typically, the JSP page name of the next page of the session stream of this application is prepared.</p> <p>With these two additional properties, the Action class in the example program is almost completely independent of the actual JSP page name used by the page designer. This page can be renamed when redesigned, but hardly affect the Action class itself. If the name of the "next" JSP page is hardcoded into the Action class, all these classes need to be modified. 4.4 Action Mapping Profile</p> <p>How does the controller servlet know what you want to get? Write a simple initialization of new an actionMApping instance and calling all the small Java classes for all appropriate SET methods (but very troublesome). In order to make this processing simple, Struts includes a Digester module to handle an XML-based description of an idealed map while creating an appropriate object. Look at the API documentation to get more information about Digester.</p> <p>The developer's responsibility is to create an XML file called Action.xml and put it in your application's web-inf directory. (Note that this file does not require DTD, because the actual use of the attributes can be different from the outermost XML element must be <action-mapings> in this element, 0 or more in this element. <Action> Element - Each corresponds to a mapping you want to define.</p> <p>The action.xml file from the example program includes the following map of the "Register" feature, we use to explain this demand:</p> <p><action-mappings></p> <p><forward name = "logon" path = "/ logon.jsp" /></p> <p><action path = "/ logon"</p> <p>ActionClass = "org.apache.struts.example.logonaction"</p> <p>Formattribute = "logonform"</p> <p>Formclass = "org.apache.struts.example.logonform"</p> <p>InputForm = "/ logon.jsp"></p> <p><forward name = "success" path = "/ mainmenu.jsp" /></p> <p></ action></p> <p></ action-mappings></p> <p>Just like you can see, this mapping matching path / logon (actually, because the example is used to match, you are over /logon.do's URI specified by a JSP page). When a request to match this path is received, an instance of a LogonAction class will be created (only first) and is used. The Controller Servlet will find a session range under the keyword logonform, if you need to create and save a bean for the specified class.</p> <p>This Action element also defines a logical name "Success" that is used in the LogonAction class to identify the page that is used when a user is successfully registered. This is like this to use a logical name to allow the Action class to isolate the page name change that may occur due to redesign positions.</p> <p>This is the Forward element that the second declared outside any action so that it can be obtained in all the Action. In this case, it defines a logical name for the registration page. When you call mapping.forward () In your Action code, Struts first looks for the logical name of this Action local definition. If you are not found, Struts will automatically find the logical name for global definitions.</p> <p>4.5 Web Application Expand Descriptor Settings The final steps of the application are configured to expand descriptors (saved in file web-inf / web.xml) to include all necessary Struts components. As an expanded descriptor of an example of a guide, we see the following entry needs to be created or modified.</p> <p>4.5.1 Configuring an Action Servlet instance</p> <p>Add an entry definition Action servlet itself while including the appropriate initialization parameters. Such an entry looks like this:</p> <p><servlet></p> <p><servlet-name> Action </ servlet-name></p> <p><servlet-class> org.apache.struts.Action.ActionServlet </ servlet-class></p> <p><init-p</p> <p>ARAM></p> <p><param-name> Application </ param-name></p> <p><param-value> Org.apache.struts.example.ApplicationResources </ param-value></p> <p></ init-param></p> <p><init-param></p> <p><param-name> config </ param-name></p> <p><param-value> /web-inc/action.xml </ param-value></p> <p></ init-param></p> <p><init-param></p> <p><param-name> debug </ param-name></p> <p><param-value> 2 </ param-value></p> <p></ init-param></p> <p><init-param></p> <p><param-name> mapping </ param-name></p> <p><param-value> Org.apache.struts.example.ApplicationMapping </ param-value></p> <p></ init-param></p> <p><Load-on-Startup> 2 </ loading-on-startup></p> <p></ servlet></p> <p>The initialization parameter supported by the Controller Servlet is described below, copies Javadocs from the ActionServlet class. Square bracket description If you don't provide a value assumed by the initialization parameter, the default value assumed.</p> <p>Application - Application resource package base class's Java class name. [NONE].</p> <p>Config - Context-related paths of XML resources containing configuration information. [/Web-inf/Action.xml]</p> <p>Debug - This servlet debug level, which controls how much information is available to the log. [0]</p> <p>Digester - We use the Digester's debug level in initmapping (), which is recorded in System.out instead of the servlet's log. [0]</p> <p>Forward - the Java class name implemented using the ActionForward. [org.apache.struts.action.Actionforward]</p> <p>Mapping - Java class name implemented using an actionMapping. [Org.apache.struts.Action.ActionMappingBase]</p> <p>Nocache - If set to true, add HTTP header information to all responses to make the browser to generate or redirect any responses to a buffer. [false]</p> <p>NULL - If set to True, set the application resource to return null if the unknown message keyword is used. Otherwise, an error message including unwelcome message key will be returned. [True]</p> <p>4.5.2 Configuring Action Servlet mapping There are two usual ways to define the URL-prefix matching and extension matching that will be processed by the Controller Servlet. One suitable mapping entry for each method will be described below.</p> <p>Prefix match means you want to pass all URLs at the beginning of a special value (after the context section). Such an entry looks like this:</p> <p><servlet-mapping></p> <p><servlet-name> Action </ servlet-name></p> <p><url-pattern> / execute / * </ url-pattern></p> <p></ servlet-maping></p> <p>It means that a URL that matches the previously described / logon path looks like this:</p> <p>http://www.mycompany.com/myapplication/execute/logon</p> <p>Here / myApplicationis is the context path where your application expands.</p> <p>On the other hand, the extended mapping matches the URL to the Action Servlet based on the fact that the URL ends with a set of sentences that follow the definition of a set of characters. For example, JSP processing servlet is mapped to * .jsp mode, which is called when requested by each JSP page. In order to use * .do expansion (it means "do something") mapping entry should look like this:</p> <p><servlet-mapping></p> <p><servlet-name> Action </ servlet-name></p> <p><url-pattern> *. do </ url-pattern></p> <p></ servlet-maping></p> <p>And a URI that matches the request previously described / logon path can look like this:</p> <p>http://www.mycompany.com/myapplication/logon.do</p> <p>4.5.3 Configuring the Struts Tag Library</p> <p>Next, you have to add an entry that defines the Struts tag library. This entry looks like this:</p> <p><taglib></p> <p><taglib-uri> /web-inf/struts.tld </ taglib-uri></p> <p><taglib-location> /Web-inf/struts.tld </ taglib-location></p> <p></ taglib></p> <p>It tells the JSP system where to find this library's tag profile descriptor (on your application's web-inferon, not some place on the Internet).</p> <p>4.5.4 Adding Struts Components into your application</p> <p>In order to use Struts while your application is run, you must copy the struts.tld file to your web-inf directory, copy the struts.jar file to your web-inf / lib. ?</p> <p>(2)</p> <p>Learn about MVC</p> <p>MVC is the abbreviament of Model, View, Controller, MVC is the design pattern developed by Application, that is, everyone</p> <p>Model2 you know. In the MVC design mode, you request your business logic, interface in Application development.</p> <p>Display, data separation. That is, in Model, View, Controller: Data, Control (Business Logic)</p> <p>Display (page display).</p> <p>In the previous or traditional Web Application development methods, such as ASP, PHP, JSP (Model 1), we implement everything in ASP (PHP, JSP), such as: From the database to the data we need, and Depending on the association and actual needs of the data, in some way, the data is displayed in the page and submitted from the form submitted to query the relevant data from the database, or write the data to the database. That is to say we implement everything in ASP (PHP, JSP): interface display, business logic, data access. The consequence of this is that the ASP (PHP, JSP) you are writing is not level, and HTML and Script (JavaScript, JSCript, ASP, PHP, JSP source code) are nesting each other. The maintenance is poor, the most interesting is in Web Application usually shows a piece of art, many times, you are also writing ASP, PHP, JSP and then beautifying, it is very likely that you have found that your code has been faced with it. You have to reorganize your code. The solution to this problem in the MVC mode is: VIEW is responsible for displaying, and view usually gets the data that has been processed from the Controller, and then displayed in the page, it should be said to nested very few Script in HTML. Basic artist Modifying that you will abolish your labor results.</p> <p>There are several developments that meet the MVC design model to make you choose from using Java Development Web Application.</p> <p>1: JSP Servlet Javabean (EJB)</p> <p>2: JSP Javabean (Controller) Javabean (EJB) (Model)</p> <p>3: TDK (Turbine, Velocity ...)</p> <p>4: XSP</p> <p>5: JSP STRUTS JAVABEAN (EJB)</p> <p>I personally think that there are two more than two kinds, others have any places, especially using TDK because there is a better</p> <p>The tool can automatically generate a lot of code, as for its shortcomings in the comparison of several developments in the latter development, I will introduce.</p> <p>Struts1.1 new features</p> <p>Struts1.1 has some very good features compared to 1.0. The most important is that the function enhancement is enhanced. Struts1.1</p> <p>The verification of the data is not implemented in the Action before the action, but is implemented through the Validation.xml.</p> <p>The advantage of this is that the reuse is reinforced.</p> <p>The main composition of struts1.1 implementation</p> <p>Mainly include: action, actionform, actionmapping, actionforward, and development is an action</p> <p>ActionForm writes or not written as needed. Below I will introduce a specific introduction.</p> <p>Action</p> <p>An action is an adapter Between the contents of an incoming http request</p> <p>And The Corresponding Business Logic That Should Be Executed to Process this</p> <p>REQUEST.</p> <p>The above is a description of the Struts development team to Action, saying Action is actually Request and Business Logic</p> <p>The middle adapter. It is a series of operations to take data from the form and wear a series of operations to business logic operations.</p> <p>Then return the corresponding operation information.</p> <p>Actionform</p> <p>An actionform is a javabean optionally associated with one or more</p> <p>ActionMappings. Such a Bean Will Have Had Its Properties Initialized from</p> <p>The Corresponding Request Parameters Before The Corresonding Action's Execute () Method Is Called.</p> <p>The ActionForm is actually packaged and verped from the data taken from the request, and then gives the legal data to</p> <p>Action is processed. In fact, ActionForm is more important in addition to data verification, in addition to writing</p> <p>It is very effective. Instead, most of the data verification after 1.1 will be implemented in validation.xml.</p> <p>ActionMapping, ActionForward</p> <p>ActionMapping is mainly used in use with configuration and description related properties. First look at Struts-config.xml</p> <p>A configuration description of the configuration file:</p> <p><action-mappings></p> <p><! - registration action -></p> <p><action path = "/ usereg"</p> <p>TYPE = "com.bingo.finance.action.useregaction"</p> <p>Name = "Usegegform"</p> <p>Scope = "request"</p> <p>Validate = "True"</p> <p>INPUT = "/ usereg.jsp"></p> <p><forward name = "success" path = "/ msg.jsp" /></p> <p></ action></p> <p></ action-mappings></p> <p>ActionMapping is used to describe a URL of an action, a specific implementation file, corresponding ActionForm</p> <p>Data Attributes, whether data checksum is required, and maybe after processing is completed</p> <p>Jump URL.</p> <p>ActionForward you can understand the jump URL after the ACTION operation is completed, and the action is processed after processing.</p> <p>Returning is an actionforward to tell Struts I have finished this step.</p> <p>Build a struts1.1 operating environment</p> <p>My configuration is a discussion of Tomcat 4.0 or later, and other AppServer is roughly the same.</p> <p>1: Get struts1.1</p> <p>http://jakarta.apache.org/builds/jakarta-struts/release/v1.1-b1/jakarta-struts-1.1-b1.zip</p> <p>2: Set</p> <p>Put Struts.jar Copy to $ TOMCAT_HOME / COMMON / LIB or you use the web-inf / lib under the spruts appaction</p> <p>Add the following configuration in Web.xml under Web.xml under the appaction of Struts</p> <p><servlet></p> <p><servlet-name> Action </ servlet-name></p> <p><servlet-class> org.apache.struts.Action.ActionServlet </ servlet-class></p> <p><init-param></p> <p><param-name> config </ param-name></p> <p><param-value> /web-inf/struts-config.xml </ param-value></p> <p></ init-param></p> <p><init-param></p> <p><param-name> debug </ param-name></p> <p><PARAM-VALUE> 3 </ Param-Value></p> <p></ init-param></p> <p><init-param></p> <p><param-name> detail </ param-name></p> <p><param-value> 3 </ param-value> </ init-param></p> <p><Load-on-Startup> 2 </ loading-on-startup></p> <p></ servlet></p> <p><taglib></p> <p><taglib-uri> /web-inf/struts-html.tld </ taglib-uri></p> <p><taglib-location> /web-inf/struts-html.tld </ taglib-location></p> <p></ taglib></p> <p><taglib></p> <p><taglib-uri> /web-inf/struts-logic.tld </ taglib-uri></p> <p><taglib-location> /web-inf/struts-logic.tld </ taglib-location></p> <p></ taglib></p> <p><! - NESTED TAG LIBRARY DESCRIPTOR -></p> <p><taglib></p> <p><taglib-uri> /web-inf/struts-nested.tld </ taglib-uri></p> <p><taglib-location> /web-inf/struts-nested.tld </ taglib-location></p> <p></ taglib></p> <p><! - TEMPLATE TAG LIBRARY DESCRIPTOR -></p> <p><taglib></p> <p><taglib-uri> /web-inf/struts-template.tld </ taglib-uri></p> <p><taglib-location> /web-inf/struts-template.tld </ taglib-location></p> <p></ taglib></p> <p>Struts1.1 provides a very detailed example, you can take a closer look.</p> <p>Next you should configure struts-config.xml as needed, here is a simple example</p> <p><? Xml Version = "1.0" encoding = "ISO-8859-1"?></p> <p><! DocType struts-config public PUBLIC</p> <p>"- // Apache Software Foundation // DTD Struts Configuration 1.1 // en"</p> <p>"http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd"></p> <p><struts-config></p> <p><! - ========================================================= == -></p> <p><form-beneans></p> <p><! - registration form bean -></p> <p><form-bean name = "usegegform"</p> <p>TYPE = "com.bingo.finance.action.userform" /></p> <p></ form-beans></p> <p><! - ================================================= -> <global-forwards></p> <p><forward name = "error" path = "/ error.jsp" /></p> <p></ global-forwards></p> <p><! - ================================================= -></p> <p><action-mappings></p> <p><! - registration action -></p> <p><action path = "/ usereg"</p> <p>TYPE = "com.bingo.finance.action.useregaction"</p> <p>Name = "Usegegform"</p> <p>Scope = "request"</p> <p>Validate = "True"</p> <p>INPUT = "/ usereg.jsp"></p> <p><forward name = "success" path = "/ msg.jsp" /></p> <p></ action></p> <p></ action-mappings></p> <p><! - ==========Message resources definitions ============================ -></p> <p><message-resources</p> <p>Parameter = "com.bingo.finance.common.displaymsg" /></p> <p><! - ========================================= = -></p> <p><! - add multiple validator resource files by setting the pathname property -></p> <p><plug-in classname = "org.apache.struts.validator.validatorplugin"> <set-protety property = "pathname" value = "/ web-inf / validator-rules.xml" /></p> <p><set-protety property = "pathname" value = "/ web-inf / value.xml" /></p> <p></ plug-in></p> <p></ Struts-Config></p> <p>I believe you can understand. I will not explain it. You need to continue to configure validation.xml, see below</p> <p>Simple example.</p> <p><form-validation></p> <p><Formset></p> <p><form name = "usegegform"></p> <p><Field property = "username"</p> <p>Depends = "Required, Mask, MINLENGTH, MAXLENGTH"></p> <p><arg0 key = "common_username" /></p> <p><arg1 name = "minlength" key = "$ {var: minlength}" resource = "false" /></p> <p><arg1 name = "Maxlength" key = "$ {var: maxlength}" resource = "false" /></p> <p><var></p> <p><var-name> Mask </ var-name></p> <p><var-value> ^ / w $ </ var-value></p> <p></ var></p> <p><var></p> <p><var-name> minlength </ var-name></p> <p><VAR-VALUE> 5 </ var-value></p> <p></ var></p> <p><var></p> <p><var-name> maxices> Maxlength </ var-name></p> <p><VAR-VALUE> 20 </ var-value></p> <p></ var></p> <p></ field></p> <p><Field property = "password"</p> <p>Depends = "Required, Mask, MINLENGTH, MAXLENGTH"></p> <p><arg0 key = "common_password" /></p> <p><arg1 name = "minlength" key = "$ {var: minlength}" resource = "false" /></p> <p><arg1 name = "Maxlength" key = "$ {var: maxlength}" resource = "false" /></p> <p><var></p> <p><var-name> Mask </ var-name></p> <p><var-value> ^ / w $ </ var-value></p> <p></ var></p> <p><var></p> <p><var-name> minlength </ var-name></p> <p><VAR-VALUE> 5 </ var-value> </ var></p> <p><var></p> <p><var-name> maxices> Maxlength </ var-name></p> <p><VAR-VALUE> 20 </ var-value></p> <p></ var></p> <p></ field></p> <p><Field property = "nickname"</p> <p>Depends = "Required, Mask, MINLENGTH, MAXLENGTH"></p> <p><arg0 key = "common_nickname" /></p> <p><arg1 name = "minlength" key = "$ {var: minlength}" resource = "false" /></p> <p><arg1 name = "Maxlength" key = "$ {var: maxlength}" resource = "false" /></p> <p><var></p> <p><var-name> Mask </ var-name></p> <p><var-value> ^ / w $ </ var-value></p> <p></ var></p> <p><var></p> <p><var-name> minlength </ var-name></p> <p><VAR-VALUE> 5 </ var-value></p> <p></ var></p> <p><var></p> <p><var-name> maxices> Maxlength </ var-name></p> <p><VAR-VALUE> 20 </ var-value></p> <p></ var></p> <p></ field></p> <p><Field property = "superpass"</p> <p>Depends = "Required, Mask, MINLENGTH, MAXLENGTH"></p> <p><arg0 key = "common_superpass" /></p> <p><arg1 name = "minlength" key = "$ {var: minlength}" resource = "false" /></p> <p><arg1 name = "Maxlength" key = "$ {var: maxlength}" resource = "false" /></p> <p><var></p> <p><var-name> Mask </ var-name></p> <p><var-value> ^ / w $ </ var-value></p> <p></ var></p> <p><var></p> <p><var-name> minlength </ var-name></p> <p><VAR-VALUE> 10 </ var-value></p> <p></ var></p> <p><var></p> <p><var-name> maxices> Maxlength </ var-name></p> <p><VAR-VALUE> 20 </ var-value></p> <p></ var></p> <p></ field></p> <p></ form></p> <p></ formset></p> <p></ form-validation></p> <p>The above validation.xml is to tell Struts that my usegegform takes the following verification.</p> <p>Username must not be empty and the smallest length is 5, the maximum length is 20 ...............</p> <p>Password, nickname, superpass basically I don't do more, the configuration is basically over, we have to start writing</p> <p>The first Struts.</p> <p>Develop struts1.1</p> <p>Usereg.jsp</p> <p>In order to consider the flexibility of the page, all elements displayed in the page are placed in the Properties file and</p> <p>com.bingo.finance.common.htmlmsg This file is taken.</p> <p>============================================================================================================================================================================================================= ==================</p> <p><% @ page contenttype = "text / html; charset = GBK"%></p> <p><% @ Page Import = "java.io. *"%></p> <p><% @ Page Import = "com.bingo.finance.common.htmlmsg"%></p> <p><% @ Taglib URI = "/ Web-INF / STRUTS-HTML.TLD" prefix = "html"%></p> <p><% @ Taglib URI = "/ Web-INF / STRUTS-LOGIC.TLD" prefix = "logic"%></p> <p><% @ Taglib Uri = "/ Web-INF / STRUTS-Bean.tld" prefix = "bean"%></p> <p><html></p> <p><HEAD></p> <p><title> <% = htmlmsg.title_regiseet%> </ title></p> <p><meta http-equiv = "content-type" content = "text / html; charSet = GB2312"></p> <p></ hEAD></p> <p><body bgcolor = "# ffffff" text = "# 000000"></p> <p><form name = "usegeg" method = "post" action = "useeg.esp"></p> <p><! - Devilical suffix in Struts is .do you can modify as needed -></p> <p><table width = "100%" border = "0" cellspacing = "1" cellpadding = "1"></p> <p><tril = "center"></p> <p><TD COLSPAN = "2"> <% = htmlmsg.title_regiseet%> </ td></p> <p></ TR> <TR></p> <p><TD width = "49%" align = "right"> <% = htmlmsg.common_username%>: </ td></p> <p><td width = "51%"></p> <p><! - In order to implement the backcrupration in Struts, it is recommended that you use TAG to use the following way of writing instead of general writing.</p> <p><HTML: Text Property = "UserName" size = "20" maxlength = "30" styleclass = "css" styleId = "userid" /></p> <p>The above code, you don't understand, you need to further understand TAG.</p> <p>-></p> <p><Input Type = "text" name = "username" size = "20" maxlength = "20"></p> <p></ td></p> <p></ TR></p> <p><tr></p> <p><td width = "49%" align = "right"> <% = htmlmsg.common_password%>: </ td></p> <p><td width = "51%"></p> <p><Input Type = "Password" name = "password" size = "20" maxlength = "20"></p> <p></ td></p> <p></ TR></p> <p><tr></p> <p><td width = "49%" align = "right"> <% = htmlmsg.common_nickname%>: </ td></p> <p><td width = "51%"></p> <p><Input Type = "text" name = "nickname" size = "20" maxlength = "20"></p> <p></ td></p> <p></ TR></p> <p><tr></p> <p><TD width = "49%" align = "right"> <% = htmlmsg.common_superpass%>: </ td></p> <p><td width = "51%"></p> <p><Input Type = "Password" name = "superpass" size = "20" maxlength = "20"></p> <p></ td></p> <p></ TR></p> <p><tr></p> <p><td width = "49%" align = "right"></p> <p><input type = "submit" name = "submit" value = "<% = htmlmsg.action_regiseet%>"></p> <p></ td></p> <p><td width = "51%"></p> <p><Input Type = "reset" name = "Submit2" value = "<% = htmlmsg.common_reset%>"> </ td></p> <p></ TR></p> <p></ table></p> <p></ form></p> <p></ body></p> <p></ html></p> <p>Usegactiom.java</p> <p>============================================================================================================================================================================================================= =========</p> <p>Package com.bingo.finance.action;</p> <p>// java import</p> <p>Import java.io.ioException;</p> <p>Import java.util.locale;</p> <p>// servlet import</p> <p>Import javax.servlet.servletException;</p> <p>Import javax.servlet.http.httpsession;</p> <p>Import javax.servlet.http.httpservletRequest;</p> <p>Import javax.servlet.http.httpservletResponse;</p> <p>// Struts import</p> <p>Import org.apache.struts.Action.action;</p> <p>Import org.apache.struts.Action.actionform;</p> <p>Import org.apache.struts.Action.actionForward;</p> <p>Import org.apache.struts.action.actionmapping;</p> <p>Import org.apache.struts.util.MessageResources;</p> <p>// Finance Import</p> <p>Import com.bingo.finance.action.userform;</p> <p>Import com.bingo.finance.manager.userManager;</p> <p>Import com.bingo.finance.entity.user;</p> <p>Public class usegegaction extends action {</p> <p>// Using Perform before Struts1.1</p> <p>//struts1.1 uses EXECUTE replacement Perform</p> <p>Public ActionForward Execute (ActionMapping Mapping,</p> <p>Actionform Form,</p> <p>HTTPSERVLETREQUEST REQUEST,</p> <p>Httpservletresponse response</p> <p>THROWS IOException, servletexception {</p> <p>Try {</p> <p>Userform Userform = (userform) form;</p> <p>UserManager UserManager = New UserManager ();</p> <p>User User = UserManager.formToentity (Userform);</p> <p>UserManager.Add (user);</p> <p>// INSERT INTO DATABASE A UserInfo</p> <p>} catch (exception ex) {</p> <p>Return mapping.findforward ("error");</p> <p>Return mapping.findforward ("Success");</p> <p>// forward is "/msg.jsp"</p> <p>}</p> <p>}</p> <p>Userform.java</p> <p>============================================================================================================================================================================================================= =====================================================================================================================================================</p> <p>Package com.bingo.finance.action;</p> <p>Import java.util. *;</p> <p>Import java.io.serializable;</p> <p>// servlet import</p> <p>Import javax.servlet.servletException;</p> <p>Import javax.servlet.http.httpsession;</p> <p>Import javax.servlet.http.httpservletRequest;</p> <p>Import javax.servlet.http.httpservletResponse;</p> <p>// Struts import</p> <p>Import org.apache.struts.action.actionmapping;</p> <p>Import org.apache.struts.validator.validatorform;</p> <p>Public Class Userform Extends ValidatorForm IMPLEments Serializable {</p> <p>// Usually Form inherited Actionform before Struts1.1</p> <p>// In fact, ValidatorForm is inherited an actionform</p> <p>/ / Why do you have a layer of validatorform to do it?</p> <p>Private string id;</p> <p>PRIVATE STRING UserName;</p> <p>PRIVATE STRING Password;</p> <p>Private string nickname;</p> <p>PRIVATE STRING SUPERPASS;</p> <p>Public userform () {</p> <p>}</p> <p>/ **** /</p> <p>Public string getId () {</p> <p>Return this.id;</p> <p>}</p> <p>/ **** /</p> <p>Public void setid (string _id) {</p> <p>THIS.ID = _ID;</p> <p>}</p> <p>/ **** /</p> <p>Public string getUsername () {</p> <p>Return this.username;</p> <p>}</p> <p>/ **** /</p> <p>Public void setusername (string _username) {</p> <p>THIS.USERNAME = _USERNAME;</p> <p>}</p> <p>/ **** /</p> <p>Public string getpassword () {</p> <p>Return this.password;</p> <p>}</p> <p>/ **** /</p> <p>Public void setpassword (string _password) {</p> <p>this.password = _password;</p> <p>}</p> <p>/ **** /</p> <p>Public string getnickname () {</p> <p>Return this.neickname;</p> <p>}</p> <p>/ **** /</p> <p>Public void setnickname (string _nickname) {this.neickname = _nickname</p> <p>}</p> <p>/ **** /</p> <p>Public string getsuperpass () {</p> <p>Return this.superpass;</p> <p>}</p> <p>/ **** /</p> <p>Public void setsuperpass (String _superpass) {</p> <p>THIS.SUPERPASS = _SUPERPASS;</p> <p>}</p> <p>/ ** Show this class info ** /</p> <p>Public string toString () {</p> <p>StringBuffer info = new stringbuffer ();</p> <p>Info.append (".... ID is:" ID);</p> <p>Info.append (".... Username is:" username);</p> <p>Info.append (".... Password is:" password);</p> <p>Info.append (".... nickname is:" nickname);</p> <p>Info.append (".... superpass is:" superpass);</p> <p>Return info.toString ();</p> <p>}</p> <p>Public void reset (actionMapping mapping, httpservletRequest request) {</p> <p>ID = NULL;</p> <p>Username = NULL;</p> <p>Password = NULL;</p> <p>Nickname = NULL;</p> <p>Superpass = NULL;</p> <p>}</p> <p>}</p> <p>UserManager.java, User.java file I will not provide, this part is based on the actual development as needed.</p> <p>Treatment, that is, insert the data into the database.</p> <p>Now a simple registration user is developed. Very simple. Oh, continue to work hard ... but there are still many</p> <p>Details, you need to know more, I only get you, you must go to study some things, such as, I hope</p> <p>My users do more email fields and an age field I should do, and I hope Validation.xml verification</p> <p>There is an email legal verification, and the age must be greater than 0 and must be an integer. Then how should I do?</p> <p>I can only tell you to add two fields. You need to modify the Form to add fields, and the corresponding file must be modified. In Struts</p> <p>Email's verification is very simple. Look at the example, study Validator-Rules.xml, this I think Struts1.1</p> <p>The best function increases (Struts1.0 does not have this file). A very flexible and highly high verification mechanism is provided in Struts.</p> <p>Comparison of Struts and other development methods</p> <p>The use of Struts development at least the following benefits:</p> <p>1: The hierarchical structure is very clear, which makes it clear.</p> <p>2: High reuse, even data verification can be reused, and there is a better reuse of form, Action is a good reuse.</p> <p>3: Good maintenance. This is the first point.</p> <p>.................</p> <p>Below I said the shortcomings of Struts, if you use or understand TDK, you will feel that there are many TDKs.</p> <p>Java source code can indeed save a lot of workload, and Struts can also do very well. I have developed a tool, function</p> <p>Not strong enough. In my plan, I hope FORM, Javabean (package data is equivalent to EJB ENTITY BEAN),</p> <p>Even Method, including the operational database, is automatically generated, and Validation.xml can also be generated. However because</p> <p>Next reason: Struts is not a mature project so far. His change is really too big. 1.0 and 0.5 have a big change. So my plan has changed. I hope to completely push it out in 1.1 and then do it (1.1 is now the beta version).</p> <p>At the same time, I also write a letter to Craig R. McClanahan (Team Leader developed by Struts).</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-101571.html</div><div class="plugin d-flex justify-content-center mt-3"></div><hr><div class="row"><div class="col-lg-12 text-muted mt-2"><i class="icon-tags mr-2"></i><span class="badge border border-secondary mr-2"><h2 class="h6 mb-0 small"><a class="text-secondary" href="tag-2.html">9cbs</a></h2></span></div></div></div></div><div class="card card-postlist border-white shadow"><div class="card-body"><div class="card-title"><div class="d-flex justify-content-between"><div><b>New Post</b>(<span class="posts">0</span>) </div><div></div></div></div><ul class="postlist list-unstyled"> </ul></div></div><div class="d-none threadlist"><input type="checkbox" name="modtid" value="101571" checked /></div></div></div></div></div><footer class="text-muted small bg-dark py-4 mt-3" id="footer"><div class="container"><div class="row"><div class="col">CopyRight © 2020 All Rights Reserved </div><div class="col text-right">Processed: <b>0.052</b>, SQL: <b>9</b></div></div></div></footer><script src="./lang/en-us/lang.js?2.2.0"></script><script src="view/js/jquery.min.js?2.2.0"></script><script src="view/js/popper.min.js?2.2.0"></script><script src="view/js/bootstrap.min.js?2.2.0"></script><script src="view/js/xiuno.js?2.2.0"></script><script src="view/js/bootstrap-plugin.js?2.2.0"></script><script src="view/js/async.min.js?2.2.0"></script><script src="view/js/form.js?2.2.0"></script><script> var debug = DEBUG = 0; var url_rewrite_on = 1; var url_path = './'; var forumarr = {"1":"Tech"}; var fid = 1; var uid = 0; var gid = 0; xn.options.water_image_url = 'view/img/water-small.png'; </script><script src="view/js/wellcms.js?2.2.0"></script><a class="scroll-to-top rounded" href="javascript:void(0);"><i class="icon-angle-up"></i></a><a class="scroll-to-bottom rounded" href="javascript:void(0);" style="display: inline;"><i class="icon-angle-down"></i></a></body></html><script> var forum_url = 'list-1.html'; var safe_token = '_2B647fgm4kZK_2Fuk9jFKqElP1MNY1ACq_2FdNjcHWB2racDyc0dsStbAn2hyMI8zKwHkjoTmK_2FNLeV4rHbST'; var body = $('body'); body.on('submit', '#form', function() { var jthis = $(this); var jsubmit = jthis.find('#submit'); jthis.reset(); jsubmit.button('loading'); var postdata = jthis.serializeObject(); $.xpost(jthis.attr('action'), postdata, function(code, message) { if(code == 0) { location.reload(); } else { $.alert(message); jsubmit.button('reset'); } }); return false; }); function resize_image() { var jmessagelist = $('div.message'); var first_width = jmessagelist.width(); jmessagelist.each(function() { var jdiv = $(this); var maxwidth = jdiv.attr('isfirst') ? first_width : jdiv.width(); var jmessage_width = Math.min(jdiv.width(), maxwidth); jdiv.find('img, embed, iframe, video').each(function() { var jimg = $(this); var img_width = this.org_width; var img_height = this.org_height; if(!img_width) { var img_width = jimg.attr('width'); var img_height = jimg.attr('height'); this.org_width = img_width; this.org_height = img_height; } if(img_width > jmessage_width) { if(this.tagName == 'IMG') { jimg.width(jmessage_width); jimg.css('height', 'auto'); jimg.css('cursor', 'pointer'); jimg.on('click', function() { }); } else { jimg.width(jmessage_width); var height = (img_height / img_width) * jimg.width(); jimg.height(height); } } }); }); } function resize_table() { $('div.message').each(function() { var jdiv = $(this); jdiv.find('table').addClass('table').wrap('<div class="table-responsive"></div>'); }); } $(function() { resize_image(); resize_table(); $(window).on('resize', resize_image); }); var jmessage = $('#message'); jmessage.on('focus', function() {if(jmessage.t) { clearTimeout(jmessage.t); jmessage.t = null; } jmessage.css('height', '6rem'); }); jmessage.on('blur', function() {jmessage.t = setTimeout(function() { jmessage.css('height', '2.5rem');}, 1000); }); $('#nav li[data-active="fid-1"]').addClass('active'); </script>