Solved

Passing an array into a JSP from a struts action.

Posted on 2011-02-18
12
1,549 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
 
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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

863 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

23 Experts available now in Live!

Get 1:1 Help Now