Solved

Passing an array into a JSP from a struts action.

Posted on 2011-02-18
12
1,545 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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
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)

757 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

21 Experts available now in Live!

Get 1:1 Help Now