Use JavaBean and LIST type properties in Struts

xiaoxiao2021-03-06  46

In Strust, we may often use other JavaBeans in ActionForm as the attribute type, here there is a data exchange between these properties and HTML FORM, and we do a way of explaining these issues.

For example, in the user registration interface, we usually form a separate new Class such as Contact, including the following properties: Call (TEL), Mobile (Cell), Email (email), QQ (QQ), Communication address, etc., this is also clear, the following is part of this Actionform:

Public Class RegisterForm Extends Actionform

{

Private integer ID;

PRIVATE STRING Logonname

PRIVATE STRING RealName;

Private Contact Contact = New Contact ();

........

}

Here we need to instantiate the Contact (need to be re-institerated in the reset function), mainly because Struts mechanism: If we pass HTML's FORM element (such as email) to Actionform, Struts needs to perform the operation is getContact ( ) .SteTemail (String email) If the Contact object returned at this time is empty, then how to assign, and Struts will not know how to instantiate Contact, sometimes these JavaBean types may be an interface, instantiation is more Unknown, so on the instantiation of the JavaBean type in Actionform, you need to do itself, and you must complete. As for whether javabean is determined in practice (no longer an initial state), you need to judge it, it is also very simple. If you can write a function inspection in the Contact class.

After the ActionForm is created, we need to reference these JAVABean type properties value in JSP Form, which is very simple, just use "Form's property name" "." "Attribute Name in Javabean" structure Pay it to the related elements. Such as:

In this way we can handle the properties of the JavaBean type in Actionform.

The actual situation may be more complex, we may sometimes be submitted to multi-line data to the back, and the data in the line is related. If we need to submit a contact information of multiple users, the data in these lines, such as email, phone, mobile phone, These data are related to user coding, now we modify the Contact class, add a user-encoded property, and we can need a list data (list) type in the ActionForm to handle this situation. The following is part of this ActionForm:

Public Class ModifyBatchContactform Extends Actionform

{

Private List Contact = New AutoArrayList (Contact.class);

... ..

}

In the above code, we also handled the initialization of List type data. When the Struts assigns an object to the object in the List, you must first get the list data, then you get an object in the List (via index), and finally assign the object. Think here we can analyze the browser to pass the data, the field value form is as follows: Contact [0] .Email = Linux_china @ Hotmail.com, Struts Gets the list data in the actionform, through Index (this time is 0 To get the JavaBean object in List, however, List is empty (although initialization, but no data), it is not possible to obtain the encapsulated object, so we have to get the object in Struts to give it an object. Create one, then ensure the success of the object's acquisition and assignment, all we create a new autoarrayList class, inherit arraylist, just override the Get (int index) method, actually simple, code as follows: public class autoarrayList Extends arraylist {

PRIVATE CLASS ITEMCLASS;

Public autoarrayList (class itemclass) {

THIS.ItemClass = itemclass;

}

Public Object Get (int index) {

Try {

While (INDEX> = size ()) {

Add (itemclass.newinstance ());

}

} catch (exception e) {

E.PrintStackTrace ();

}

Return super.get (Index);

}

}

In this way, we will complete the Actionform design that can be submitted multi-line data. It is also a little reminder. If the line index from the browser end is jumping, if you miss the middle line, you don't want this data, and the first tail line index This approach may not be suitable if the difference is very large. This multi-line submission form is more suitable for data processing of the fixed line. If the number of rows is not fixed, you can refer to the use of MapForm.

Let's take a look at how to use this ActionForm in JSP, actually only need to perform a loop:

Here we explain, "Contact" (red) appearing in the code is the list of List data type variables in the Actionform. Please make a consistency, please do not change the name, which is also convenient to submit data reception in the background. "FormName" is the Actionform name declared in Struts-Config. Indexed = "True" ensures that the name of the generated HTML element is unique, don't miss this statement. The above JSP code, you can also use JSTL to do it, just see how you are used to. Through the above steps, we have completed the submission of multi-line data, so Struts will complete all other things, our code logic and implementation are simple.

Summary: Through the above two examples, the letter uses JavaBean and List type data in the actionform, which is not unfamiliar, using the Map type, which can create better ActionForm Design, about mapform, please refer to http: // www. Jetmaven.net/documents/j_mapforminstruts.php. However, after the introduction of this approach, you may take a metric in Validator (if you can generate the Validator file through xdoclet), you may need to handwill some code to complete the work.

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

New Post(0)