Solved

Passing an array into a JSP from a struts action.

Posted on 2011-02-18
12
1,552 Views
Last Modified: 2012-05-11
I am passing an array of string values from my database into a jsp via a form bean.  The array of string values were retrieved and and loaded into the form bean in a Struts action..  However, when I do this:

<input id='ValueList' type='hidden' value='<%=valueList%>' />

The array is not getting assigned correctly to the hidden input variable correctly.

These javascript lines are not working well:

var answer = [];
answer = document.getElementById('ValueList').value;
alert('MY VALUE LIST:::::' + answer[0]);

Is there a way to do this that actually works?
0
Comment
Question by:jwright9
  • 5
  • 2
  • 2
  • +1
12 Comments
 
LVL 27

Expert Comment

by:rrz
ID: 34930739
><input id='ValueList' type='hidden' value='<%=valueList%>' />  
I don't know Struts but that can't work. The expression <%=  %>   is for accessing  scripting variables on the page. I think you should try  
<input id='ValueList' type='hidden' value='${valueList}' />
or
<input id='ValueList' type='hidden' value='<%=session.getAttribute("valueList")%>' />
or maybe  
<input id='ValueList' type='hidden' value='<%=request.getAttribute("valueList")%>' />
0
 
LVL 7

Accepted Solution

by:
dxdinh earned 300 total points
ID: 34930748
you probably have to apply the split function to cast it back to array list

var answer = document.getElementById('ValueList').value;

var answerArray = new Array();
// I assume comma as a separator
answerArray = answer.split(',');

alert('MY VALUE LIST:::::' + answerArray[0]);
0
 
LVL 27

Expert Comment

by:rrz
ID: 34930793
dxdinh has a good point. Try using
<input id='ValueList' type='hidden' value='${valueList[0]}' />
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 20

Expert Comment

by:Sathish David Kumar N
ID: 34932899
As per i know thats not possible bcz your array list have more than beans values so you cant take that values in java script .

 
do one thing just print this value
<input id="ValueList" type="text" value="<%=valueList%>" />

you will get result like this
@and something
0
 
LVL 20

Assisted Solution

by:Sathish David Kumar N
Sathish David  Kumar N earned 200 total points
ID: 34932914
if your array list have direct string

ArrayList a=new ArrayList();
a.add("a");  a.add("b"); this array list you can take

but urs is

ArrayList a=new ArrayList();
a.add(new formBean.setName("Sathish"));  here you need casting to get the value
0
 
LVL 20

Expert Comment

by:Sathish David Kumar N
ID: 34932943
so you can put that values like this

<%  
List a=(ArrayList) request.getAttribute("valueList");
Formbean fb;
for (int i=0; i<a.size;i++)
{
fb=(Formbean) a.get(i);
%>
<input type="hidden" id="hidd<%=i%>"  name="hidd" value="<%=fb.getName()%>">
<%}%>


in java script

var answer = document.formname.hidd.value;   //// u can get all values

thats all
0
 
LVL 7

Expert Comment

by:dxdinh
ID: 35053357
I m glad it works for you and thanks for the points -
0
 
LVL 20

Expert Comment

by:Sathish David Kumar N
ID: 35054082
Really your getting answer with that solution.....

your selecting you cant get the values without casting .......................
0
 

Author Comment

by:jwright9
ID: 35063705
dravidnsr has made a correct point.  He left alot of useful examples.   I would like to give him 200 points.  I did give the other responder the initial points.  I really do appreciate their effort.  This is better than a text book since I got great help on a problem I encountered in practice.
0
 
LVL 20

Expert Comment

by:Sathish David Kumar N
ID: 35068116
0

Featured Post

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

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

Suggested Solutions

This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn how to implement Singleton Design Pattern in Java.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

856 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