Form for receiving arrays in struts and Ajax

Source: Internet
Author: User

We are too familiar with the form passing value in struts2. It is very easy to pass one or more values or attributes of an object.

But we wantUpload an array and expect struts to receive it correctlyWhat should I do?

 

The following describes how to use common forms and Ajax. First, we have an entity, an action, and a JSP.

 

Student. Java

Public class student
{
Private string name;
Private string num;
}

Studentaction. Java

Public class studentaction extends actionsupport
{
Private list <student>Lststu;
}

XY. jsp

<SCRIPT type = "text/JavaScript">
VaR stus = [];
Stus. Push ({num: "1", name: "xy1 "});
Stus. Push ({num: "2", name: "xy2 "});
Stus. Push ({num: "3", name: "xy3 "});
</SCRIPT>

 

The following code is written in the script area of xy. jsp.

 

Common form-traverse the array and construct a form hidden field

VaR htmlcontent = "";
For (VAR I = 0; I <stus. length; I ++ ){
Htmlcontent + = "<input type = 'den den 'name = 'lstu [" + I + "]. name 'value = '"+ stus [I]. name + "'/> ";
Htmlcontent + = "<input type = 'den den 'name = 'lstu [" + I + "]. num 'value = '"+ stus [I]. num + "'/> ";
}

Special Cases

<Input type = 'did' name = 'lstu. name' value = 'xy1'/>
<Input type = 'did' name = 'lstu. name' value = 'xy2'/>
<Input type = 'did' name = 'lstu. name' value = 'xy3'/>

Struts can recognize the attributes of leaflets, indicating three different student. However, uploading two attributes won't work because struts doesn't know the combination. Not recommended.

 

Ajax form -- traverse the array and construct a JSON object

VaR Param = {};
For (VAR I = 0; I <stus. length; I ++ ){
Param ["lststu [" + I + "]. Name"] = stus [I]. Name;
Param ["lststu [" + I + "]. Num"] = stus [I]. Num;
}

$. Ajax ({
Data: Param
});

In fact, we have constructed such a JSON object.

Data :{
Lststu [0]. Num: "1", lststu [0]. Name: "xy1 ",
Lststu [1]. Num: "2", lststu [1]. Name: "xy2 ",
Lststu [2]. Num: "3", lststu [0]. Name: "xy3"
}

Some people say that it is not convenient to directly upload the stus array as data to the action? The answer is that it cannot be passed in this way, so that the action cannot receive or struts does not know how to process the sent array.

 

Related Article

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.