Solved

Multiple select in Struts

Posted on 2010-08-24
16
877 Views
Last Modified: 2012-05-10
Darn,

I think that I have found the answer for this in my previous project but I just forgot it.

Situation: I need to use JavaScript to auto select all the option inside the select box then my FormBean can see the value (String[]). If I do not do that, it simply returns null.

Is this correct? or something wrong with my Struts config?

Thanks
David
0
Comment
Question by:suprapto45
  • 9
  • 6
16 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 33512366
0
 
LVL 16

Author Comment

by:suprapto45
ID: 33512425
Hi CEHJ,

Nah, I think that you misunderstood my question.

I have been able to select all the options in my dropdown (using jQuery)....however, my dropdown has over 600 items hence it take some times for JavaScript to autoselect all of them hence we want to avoid this.

My question is that is there any way that we can pass the parameters to Struts without auto select the dropdown?

Thanks
David
0
 
LVL 16

Author Comment

by:suprapto45
ID: 33512435
And is it part of Struts requirement that we have to autoselect the dropdown before submitting? (I do not think so)

Hence, I suspected that there may be something wrong with my Struts configuration as well.

Thanks
David
0
 
LVL 20

Expert Comment

by:Sathish David Kumar N
ID: 33515272
Can you tell me clearly ???
0
 
LVL 16

Author Comment

by:suprapto45
ID: 33517574
Hi,

Sorry for not being clear enough. It was already late yesterday when I posted this question.

Okay, currently, I have one dropdown list in my JSP that allows multiple selection as shown in the attached code below. Now, everytime I submit the form, I am mapping the notAssignList into String[] in my FormBean.

However, in my Struts action, when I retrieved the parameters from my FormBean, it always said that my notAssignList is null. After checking via Fiddler, I found out that the notAssignList is not submitted to the request. However, another parameter i.e. companyNo is working fine and it can be retrieved from the FormBean.

if (form != null) {
    caf = (CompanyAssignForm) form;
                           
    notAssignList = caf.getNotAssignList();
}

Now, if I highlight / select all the option within the select dropdown (using JavaScript), I can retrieve the value from the FormBean properly.

Hence, I am confused here. Must we always auto-select all the options item within the select dropdown in order for Struts to be able to populate the FormBean?

Thanks.
David
-- JSP side

<select name="notAssignList" id="notAssignList" size="10" style="width:200px;" multiple="multiple">

    <option value="10">User 10</option>

    <option value="11">User 11</option>

</select>





-- Form Bean

public class CompanyAssignForm extends ActionForm {

	private static final long serialVersionUID = 1L;



	private int companyNo;

	private String[] assignList;

	private String[] notAssignList;

	

	/**

	 * @param mapping

	 * @param request

	 */

	public void reset(ActionMapping mapping, 

	 				  HttpServletRequest request) {

		this.companyNo = -1;

		this.assignList = null;

		this.notAssignList = null;

	}	

	

	/**

	 * @return the companyNo

	 */

	public int getCompanyNo() {

		return companyNo;

	}

	/**

	 * @param companyNo the companyNo to set

	 */

	public void setCompanyNo(int companyNo) {

		this.companyNo = companyNo;

	}

	/**

	 * @return the assignList

	 */

	public String[] getAssignList() {

		return assignList;

	}

	/**

	 * @param assignList the assignList to set

	 */

	public void setAssignList(String[] assignList) {

		this.assignList = assignList;

	}

	/**

	 * @return the notAssignList

	 */

	public String[] getNotAssignList() {

		return notAssignList;

	}

	/**

	 * @param notAssignList the notAssignList to set

	 */

	public void setNotAssignList(String[] notAssignList) {

		this.notAssignList = notAssignList;

	}	

Open in new window

0
 
LVL 16

Author Comment

by:suprapto45
ID: 33517578
Hi,

>>"Now, if I highlight / select all the option within the select dropdown (using JavaScript), I can retrieve the value from the FormBean properly. "
I mean that highlight / select all the options before the form is being submitted.

Thanks
David
0
 
LVL 16

Accepted Solution

by:
suprapto45 earned 0 total points
ID: 33518133
Hi All,

The answer is that the problem is not on Struts. This is on how the browser behaves. Multiple dropdown can also be normally considered as List box in HTML. Without highlighting any item in the List box, it just means that user does not choose anything hence it is not being submitted to the HTTP request. I have tested this and all browsers are having the same behavior as it is.

For the performance of JS in selecting the item in dropdown, I will find an alternative way of doing this.

Thanks
David
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 33518926
Why is it a problem getting null btw?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 16

Author Comment

by:suprapto45
ID: 33518955
Hi CEHJ,

If we do not select any item in the list box (dropdown), there will be no parameter passed to FormBean in Struts hence causing my FormBean to return the variable as null.

Similarly, if we choose one item, it will return String[1] and if we choose two items, it will return String[2] and so on.

Thanks
David
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 33518968
>>causing my FormBean to return the variable as null.

Yes, but why is that a problem? Null seems to be very good...
0
 
LVL 16

Author Comment

by:suprapto45
ID: 33519001
Hi CEHJ,

Yes, not a problem with that.

My initial problem is that I thought that this is Struts problem. I thought that without selecting any item, it should also pass the parameters to my FormBean eliminating the need to programmatically select all the items in each dropdown. However, apparently, this is not a Struts issue and this is just behavior of all the browsers.

Thanks
David
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 33519116
>>eliminating the need to programmatically select all the items in each dropdown

Why is there that need though?
0
 
LVL 16

Author Comment

by:suprapto45
ID: 33522087
Hi CEHJ,

>>"Why is there that need though?"
Because my users said that it is rather slow. Consider that I have 6 dropdown in one of my JSP page and each dropdown has > 500 items on them.

On submission, I need my JavaScript to programmatically select these 3000 items in dropdown.

This is my main reason on why I posted this question in EE.

Thanks
David

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 33522284
I'm still rather mystified, but we'll leave it there ;)
0
 
LVL 16

Author Comment

by:suprapto45
ID: 33527488
CEHJ,

Thanks for your time in helping :).

Thanks
David
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 33529414
No problem
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
groupSum5 challenge 5 71
PDFBox - convert image to PDF, PDF resolution 3 118
Java SE 8u111  Lot of stuff broke 11 54
couple of eclipse 5 25
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now