Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
458 views
in Technique[技术] by (71.8m points)

Send multiple objects of same class from jsp to spring controller

When i am keeping two inputs name same in jsp. I want they should be come to controller in two different objects. @ModelAttribut

@RequestMapping(value = "/addUser", method = RequestMethod.POST)
public String addAdvertiser(@ModelAttribute("userInfoEntity") UserInfoEntity userInfoEntity,@ModelAttribute("addressEntity") AddressEntity personalAddress,@ModelAttribute("addressEntity") AddressEntity billingAddress){
// save this two address to my userInfo Entity 
// here i had one to many relationaship. from UserInfoEntity to AddressEntity
}

My JSP is as follows:

Personal Address
<div class="label"><b>Address 1 :</b></div>
                        <div class="leftSide"><input type="text" name="address1" class="textBox" style="width:250px;" /></div>

                        <div class="label"><b>City :</b></div>
                        <div class="leftSide"><input type="text" name="city" class="textBox" style="width:250px;" /></div>

                        <div class="label"><b>Zipcode :</b></div>
                        <div class="leftSide"><input type="text" name="zipcode" class="textBox" style="width:250px;" /></div>

                        <div class="label"><b>Address 2 :</b></div>
                        <div class="leftSide"><input type="text" name="address2" class="textBox" style="width:250px;" /></div>

                        <div class="label"><b>State :</b></div>
                        <div class="leftSide"><input type="text" name="state" class="textBox" style="width:250px;" /></div>

                        <div class="label"><b>Phone :</b></div>
                        <div class="leftSide"><input type="text" name="phone" class="textBox" style="width:250px;" /></div>




Billing Address
                        <div class="label"><b>Address 1 :</b></div>
                        <div class="leftSide"><input type="text" name="address1" class="textBox" style="width:250px;" /></div>

                        <div class="label"><b>City :</b></div>
                        <div class="leftSide"><input type="text" name="city" class="textBox" style="width:250px;" /></div>

                        <div class="label"><b>Zipcode :</b></div>
                        <div class="leftSide"><input type="text" name="zipcode" class="textBox" style="width:250px;" /></div>

                        <div class="label"><b>Address 2 :</b></div>
                        <div class="leftSide"><input type="text" name="address2" class="textBox" style="width:250px;" /></div>

                        <div class="label"><b>State :</b></div>
                        <div class="leftSide"><input type="text" name="state" class="textBox" style="width:250px;" /></div>

                        <div class="label"><b>Phone :</b></div>
                        <div class="leftSide"><input type="text" name="phone" class="textBox" style="width:250px;" /></div>

Please tell me the solution that how can i send two objects of same AddressEntity from jsp to controller keeping the same value for attribute "name" of input type.

In the given senario i am getting the object but it includes values of both address by ","(comma) seperated.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If the name is same, it will return you an array of objects, that's why when you try to print it, it shows string with comma separation.

You need to cast object to array of your return type ( probably string in your case ). and get values arr[0] and arr[1].


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...