aspose file tools*
The moose likes Struts and the fly likes iterate doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "iterate doubt" Watch "iterate doubt" New topic
Author

iterate doubt

Pradeep Adibatla
Ranch Hand

Joined: Oct 27, 2009
Posts: 336
I have an arrayList called "atrValAL" with one value in it...

I am iterating this list in a select box...

I wrote a JSTL/EL like this...



However,the list is empty.Any correction in this??

Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9304
    
  17

Pradeep Adibatla wrote:I have an arrayList called "atrValAL" with one value in it...

Where is this ArrayList?? Is it stored as an attribute in any of the scopes like request or session?? Are you using Struts?? If yes then you might consider using struts iterator for doing this. Depending upon the version of struts you are using, you can use logic:iterate (struts 1) or s:iterator (struts 2)...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Pradeep Adibatla
Ranch Hand

Joined: Oct 27, 2009
Posts: 336
The ArrayList is in the Action class .I am using struts-2 ans somehow my s:iterator is not populating the values in the select box I mean the box is empty but in the action class I could see the values that are to be populated in the select . Hence I moved to EL.

This is my action class code....



output:--
--------------
in getConAtrAL !!!
x is :--> 0
i is :--> 0
s is :--> A1



This is in my JSP
----------------------






somehow the select is empty !!!
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

where you are setting the ArrayList into ActionForm ? i dont see this
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9304
    
  17

1. I can't understand how the getAtrValAL method is called before the execute method (judging from this line of output: x is :--> 0). Does this action send the user to the JSP page that you've shown or not?? As far as the JSP goes, you can change your code to something like this

You can also use s:select tag which generates the select tag and a list of options itself...
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9304
    
  17

seetharaman venkatasamy wrote:where you are setting the ArrayList into ActionForm ? i dont see this

He's using Struts 2, there's no ActionForm in Struts 2...
Pradeep Adibatla
Ranch Hand

Joined: Oct 27, 2009
Posts: 336
My version is 2.0.11 and , symbol " { " is invalid as it throws an error.
"[" is valid but the issue persists...

I tried like this...



Does this action send the user to the JSP page that you've shown or not??


No,I am doing it in the same jsp. I mean I am populaing within the same jsp.


Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9304
    
  17

My version is 2.0.11 and , symbol " { " is invalid as it throws an error.
:
No,I am doing it in the same jsp. I mean I am populaing within the same jsp.

1. I used %{} and not ${} in s:property tag. ${} is EL, %{} is OGNL.
2. I'm confused with how you are populating withing the same jsp. You showed us code of an action class which populates the ArrayList, and now you are saying that its getting populated in the JSP. Exactly what is the flow of your application??
Pradeep Adibatla
Ranch Hand

Joined: Oct 27, 2009
Posts: 336
You are spot on,var is replaced by id and i wrote like this...


and also this...



but to no avail !

This is the flow...

Go(button)
rules.jsp------------------------>tabtest.jsp
|
|
|
rulesAction.java


In tabtest .jsp I have a button "Click" ,onclick I need to populate Select...

So I make an Ajax call sending the button value to rulesAction.java ...

the URL...



"cVal" is the button value in the javascript !!!

and then in the execute of my action class I check whether the button is clicked.If yes then I populate the list as I had written above !!!

A(button click)
tabtest.jsp------------------------------rulesAction.java-------------->tabtest.jsp


Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9304
    
  17

If you are making an AJAX call, then how is the s:iterator supposed to get the values?? After you make the AJAX call, how are you handling the response of the AJAX call?? Can you show us the code of the AJAX call i.e. how you send a request to the server and how you handle the response...
Pradeep Adibatla
Ranch Hand

Joined: Oct 27, 2009
Posts: 336
on button click, I do this Ajax call...





-----------------------

Now I go to rulesAction.java and I check



and the code i pasted initially to set the ArrayList...

but when the framework comes across iterator name in jsp it will look for it in all action classes and set it,isn't it?


Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9304
    
  17

I'm still trying to understand what you are doing.

1. Can you show us the complete code of the rulesAction.java. Also is this action mapped to the path rules.action??
2. When you post any code of any JSP, please specify the name of the JSP file in which the code actually is. You mentioned that you have rules.jsp and tabtest.jsp but you don't mention the name of the file with the code that you post. This makes it hard to understand which code is in which file.
3. When you make an ajax call, are you expecting that the JSP page in the browser will automatically be updated. What I mean is if tabtest.jsp is open in the browser, you make an ajax call to the server, and the tabtest.jsp is processed again on the server side, then the tabtest.jsp in the browser won't be updated to reflect the changes in the jsp due to the ajax call.

This is all that I can help because I can't understand the problem properly...
Pradeep Adibatla
Ranch Hand

Joined: Oct 27, 2009
Posts: 336


When you make an ajax call, are you expecting that the JSP page in the browser will automatically be updated. What I mean is if tabtest.jsp is open in the browser, you make an ajax call to the server, and the tabtest.jsp is processed again on the server side, then the tabtest.jsp in the browser won't be updated to reflect the changes in the jsp due to the ajax call.



I think this is what I am doing and as you described it doesn't work

So how will I send the button value on button click in tabtest.jsp to action class to prepare the Arraylist???

relation1 relation2
----------- ---------------

rules.jsp tabtest.jsp
| |
| |
| |
rulesAction.java tabtestAction.java

considering what you said, I think it all should be done with relation2(above)


So I think I would write in tabtest.jsp...



but how will I reach action class? Will the framework immediately looks for the "name=condCList" in action class?

Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9304
    
  17

If all that you want to do is on the click of a button, you want to populate a combo box, then when you make the AJAX call, the action should return HTML containing only the <option> tags. Then in javascript you can add that response to the <select>. Basically in your code

You can employ various approaches to send the response from the action to javascript (like JSON result instead of HTML etc), but this is the simplest approach according to me...
Pradeep Adibatla
Ranch Hand

Joined: Oct 27, 2009
Posts: 336
That makes it all clear now, but...



the action should return HTML containing only the <option> tags


Is it

request.setAttribute("AtrValAL",val); ??



Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9304
    
  17

Basically what I mean is, in the action after you populate the ArrayList in the action, your action must send the control to a JSP page which will only generate <option> tags and not the complete tabtest.jsp page. You can also return JSON instead of HTML from the server, and use it in the javascript to generate the necessary HTML <option> tags, for that you can create a custom JSON result (if you need to reuse it in different pages). An example of handling JSON response in javascript is given here...
Pradeep Adibatla
Ranch Hand

Joined: Oct 27, 2009
Posts: 336

your action must send the control to a JSP page which will only generate <option> tags and not the complete tabtest.jsp page


Whoa! I thought I should wirte a code to send <option? tags from Action class .you mean after creating the ArrayList I should return say "abc" and in struts.xml set the result to someother jsp on return of 'abc'? what about the tabtest.jsp then? confusion again...

Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9304
    
  17

I thought I should wirte a code to send <option> tags from Action class

You can write the option tags from the Action too, but that's probably not a very good idea.
what about the tabtest.jsp then?

In your action you have a condition which is
Inside this condition you can separately return the "abc" result mapping. The normal response will return the output of tabtest.jsp page, and when the AJAX request is received, you can send a different result...
Pradeep Adibatla
Ranch Hand

Joined: Oct 27, 2009
Posts: 336
Well,Ankit,can you provide a link which gives an example for writing that in action class or any book ,notes for the same??
I googled it but to no avail .


and when the AJAX request is received, you can send a different result...


I have like this...




in struts.xml






Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9304
    
  17

There seems to be a JSON Plugin in struts. There is an old discussion about this here. There's a tutorial available about AJAX and JSON in Struts2 here. There's also another tutorial about writing JSON using Jettison library here. As far as your code goes, you can return a different result type in the if condition i.e. a different result if the if condition is true and a different result if the if condition is false...
Pradeep Adibatla
Ranch Hand

Joined: Oct 27, 2009
Posts: 336
it's a long time recall but had one doubt...

Does putting in a session help me,avoiding use of JSON?

JSON producing too many errors ...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: iterate doubt