Using JavaBean and List (multiline data) type properties in struts

Source: Internet
Author: User
Tags add object empty end html form interface string contact form
Data

In Strust, we may often have to use other JavaBean as attribute types in Actionform, and here is how to use the data exchange between these attributes and the HTML Form, so let's take a look at these questions.

As in the user registration interface, we usually will be the user's contact form a separate new Class, such as contacts, including the following attributes: Telephone (tel), cell phone (cell), email (email), QQ (QQ), mailing address (adress), etc. The hierarchy is also clearer, and the following is part of the code for 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 (which needs to be instantiated in the Reset function), mainly because of the mechanism of struts: if we pass the HTML Form element (such as email) value to actionform, the action that Struts needs to perform is the GE Tcontact (). Setemail (String email), if the contact object returned is empty, then how the assignment is done, and Struts will not know how to instantiate the contact, sometimes these JavaBean The type may be an interface, and instantiation is more unknown, so the instantiation of the JavaBean type in actionform, you need to do it yourself, and it must be done. As for the actual operation of the judge whether the JavaBean has been carried out related operations (no longer the initial state), you need to judge for yourself, in fact, is also very simple, such as the contact class to write a function test can be.

After the Actionform is created, we need to reference these JavaBean types of attribute values in the Jsp form, which is very simple, just take the "Form's property name +". "+" property name in JavaBean structure of the name to pay the relevant elements. Such as:

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

The actual situation may be more complicated, we may have to submit more than one line of data to the back, and the data in the row is relevant, such as we need to submit a number of user contact information, these lines of data, such as email, telephone, cell phone, these data are related to user code, and now we modify contact class , add a user encoding (USERID) attribute, in which case we might need a list data (list) type in the actionform we build. Here's a partial code for this actionform:

public class Modifybatchcontactform extends Actionform

{

Private List Contact =new autoarraylist (contact.class);

.....

}

In the above code, we also handle the initialization of the List type data. When Struts assigns values to objects in a list, it is of course necessary to get the list data first and then assign a value to an object (through Index) at the end of the list. Here think we can more detailed analysis of the browser end of the data, the field value form is as follows: Contact[0].email=linux_china@hotmail.com, Struts get the list data in the Actionform, through the Index (This is 0) to get the JavaBean object encapsulated in the List. However, when the list is empty (although initialized but without data), it is not possible to get the encapsulated object, so we want to create one when Struts gets the encapsulated object in the list so that the object is Fetch and assign a successful, all we create a autoarraylist class, inherit ArrayList, just rewrite get (int index) method, in fact very simple, the code is 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);

}

}

This allows us to complete the actionform design of multiple rows of data submissions, and here is a reminder that if the row index passed from the browser is a jump Yes, if there is a lack of intermediate line, and you do not want the data, and the beginning and end of the index significantly different, this way may not be appropriate. This multiline commit form is more appropriate for data processing for fixed rows. If the number of lines is not fixed, you can refer to the use of the Mapform method implementation.

Let's take a look at how to use this actionform in the JSP, in fact, only need to execute a loop can:

<logic:iterate id= "Contacts" Name= "FormName" property= "Contact" indexid= "index" >

</logic:iterate>

Here we explain, the code appears in the "Contact" (red) is the list data type variable name in Actionform, please ensure consistent, do not change the name, it is also convenient to submit to the background of the data received. "FormName" is the name of the Actionform declared in Struts-config. Indexed= "True" ensures that the name of the generated HTML element is unique and does not lack this declaration. The above JSP code, you can also use JSTL to complete, depends on how you get used to it.

With the above steps, we have completed the submission of multiple rows of data so that Struts can do all the other things, and our code logic and implementation are much simpler.

Summary: With the above two examples, using the JavaBean and list type data in Actionform is no longer unfamiliar with the Map type, so you can create a better actionform design, for Mapform, refer to Http://www.je tmaven.net/documents/j_mapforminstruts.php. However, after this approach, you may have a bit of a head on Validator (such as generating Validator files via XDoclet may be incomplete), and you may need to write some code to do the job.




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.