Solved

Passing an array into a JSP from a struts action.

Posted on 2011-02-18
12
1,555 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. 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.: (CODE)
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…

735 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