• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1579
  • Last Modified:

Passing an array into a JSP from a struts action.

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
jwright9
Asked:
jwright9
  • 5
  • 2
  • 2
  • +1
2 Solutions
 
rrzCommented:
><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
 
dxdinhCommented:
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
 
rrzCommented:
dxdinh has a good point. Try using
<input id='ValueList' type='hidden' value='${valueList[0]}' />
0
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
Sathish David Kumar NArchitectCommented:
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
 
Sathish David Kumar NArchitectCommented:
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
 
Sathish David Kumar NArchitectCommented:
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
 
dxdinhCommented:
I m glad it works for you and thanks for the points -
0
 
Sathish David Kumar NArchitectCommented:
Really your getting answer with that solution.....

your selecting you cant get the values without casting .......................
0
 
jwright9Author Commented:
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
 
Sathish David Kumar NArchitectCommented:
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

  • 5
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now