Home Web Front-end JS Tutorial Ajax+Struts2 receives array form (with code)

Ajax+Struts2 receives array form (with code)

Apr 25, 2018 pm 04:10 PM
code form

This time I will bring you Ajax Struts2 to receive an array form (with code). What are the precautions for Ajax Struts2 to receive an array form? The following is a practical case, let's take a look.

I will explain below through two methods: ordinary form and ajax. First we have the following entity, an action and a jsp.

Student.java

1

2

3

4

5

6

7

8

9

10

public class Student

{

 private String name;

 private String num;

}

StudentAction.java

public class StudentAction extends ActionSupport

{

 private List<Student> lstStu;

}

Copy after login

xy.jsp

1

2

3

4

5

6

<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>

Copy after login

Let’s get started. The following code is written in xy. jsp script area.

Normal form form——Traverse the array, construct the form hidden field

1

2

3

4

5

var htmlContent = "";

for(var i=0;i<stus.length;i++){

 htmlContent += "<input type='hidden' name='lstStu[" + i + "].name' value='" + stus[i].name + " ' />";

 htmlContent += "<input type='hidden' name='lstStu[" + i + "].num' value='" + stus[i].num + " ' />";

}

Copy after login

Special case

1

2

3

<input type='hidden' name='lstStu.name' value='xy1' />

<input type='hidden' name='lstStu.name' value='xy2' />

<input type='hidden' name='lstStu.name' value='xy3' />

Copy after login

When passing a single attribute, struts can recognize it and represent three different students. But passing two attributes won't work, because struts doesn't know the combination. Not recommended.

ajax form - traverse the array and construct a json object

1

2

3

4

5

6

7

8

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

});

Copy after login

In fact, we constructed such a json object

1

2

3

4

5

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"

}

Copy after login

I believe you have mastered the method after reading the case in this article, and more How exciting, please pay attention to other related articles on php Chinese website!

Recommended reading:

Detailed explanation of JSONP implementation principles and cases

How to use the ajax.load() method in jQuery

The above is the detailed content of Ajax+Struts2 receives array form (with code). For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to solve win7 driver code 28 How to solve win7 driver code 28 Dec 30, 2023 pm 11:55 PM

How to solve win7 driver code 28

What to do if the blue screen code 0x0000001 occurs What to do if the blue screen code 0x0000001 occurs Feb 23, 2024 am 08:09 AM

What to do if the blue screen code 0x0000001 occurs

The computer frequently blue screens and the code is different every time The computer frequently blue screens and the code is different every time Jan 06, 2024 pm 10:53 PM

The computer frequently blue screens and the code is different every time

Resolve code 0xc000007b error Resolve code 0xc000007b error Feb 18, 2024 pm 07:34 PM

Resolve code 0xc000007b error

What does the blue screen code 0x000000d1 represent? What does the blue screen code 0x000000d1 represent? Feb 18, 2024 pm 01:35 PM

What does the blue screen code 0x000000d1 represent?

GE universal remote codes program on any device GE universal remote codes program on any device Mar 02, 2024 pm 01:58 PM

GE universal remote codes program on any device

Detailed explanation of the causes and solutions of 0x0000007f blue screen code Detailed explanation of the causes and solutions of 0x0000007f blue screen code Dec 25, 2023 pm 02:19 PM

Detailed explanation of the causes and solutions of 0x0000007f blue screen code

How to use JavaScript to implement real-time verification of the input box content of a form? How to use JavaScript to implement real-time verification of the input box content of a form? Oct 18, 2023 am 08:47 AM

How to use JavaScript to implement real-time verification of the input box content of a form?

See all articles