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

working with hashmap using struts2

Aditi Gandy
Ranch Hand

Joined: Apr 23, 2008
Posts: 37
Hello,
I am very new to struts 2.
I have a requirement to pass HashMap object as argument to execute method in action class.... and display its contents in JSP.

How to make variables stateless in struts 2?

IS there any example or any book to refer?

Please help me !


Thanks & Regards,
Author
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8908
    
    8

I've never tried, but I don't see any facility to pass an argument to an action method in the documentation.
Exactly what are you trying to do? Where does this hashmap come from? In order to be passed to the execute method it has to come from a form or an interceptor. Is there any way for the action method to fetch the information it needs?


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
Aditi Gandy
Ranch Hand

Joined: Apr 23, 2008
Posts: 37
NO actually, I have hardcoded a HashMap in execute method. When i try to display the contents of Hashmap in JSP, it is not displaying it.
My code goes here:






And also, i am not able to trace, where exactly values of System.out.println is printing..i am not able to figure out where is console output in Tomcat 6.0

Can anyone please help.
Aditi Gandy
Ranch Hand

Joined: Apr 23, 2008
Posts: 37
please help me.

Regards,
Ashvini
Nishan Patel
Ranch Hand

Joined: Sep 07, 2008
Posts: 687


Hi ashvini pradeep ,

First of all understand hasMap concept. HasMap contains key and value fields.

In your code
# myMap.put("Sun", "Java");
# myMap.put("MS", "Net");
# myMap.put("SAP", "ABAP");
# myMap.put("SQL", "DataBase");

"Sun","MS"...etc are your key value and "Java","Net"...etc are your value so,

If you want to display it in jsp

Use this code:


<s:iterator value="myMap">
<s:property value="key" /> <s:property value="value" />
</s:iterator>

The first <s:property value="key" /> will display "Sun","MS"...etc your key value.

And second <s:property value="value" /> will display "Java","Net"...etc display your value.



Thanks, Nishan Patel
SCJP 1.5, SCWCD 1.5, OCPJWSD Java Developer,My Blog
Aditi Gandy
Ranch Hand

Joined: Apr 23, 2008
Posts: 37
Thank you Nishan,
Thank you very much for your reply.

Even after adding your code to display key and value, I am not able to see the contents of my Hashmap on the web page.
Can't understand where i am going wrong.

my log entries as follows:



Nishan Patel
Ranch Hand

Joined: Sep 07, 2008
Posts: 687


Hello Again,

Change your code in struts.xml.


# <action name="it" class="Iterate">
# <result>/pages/Iterate.jsp</result>
# </action>

Remove and change code as belove

# <action name="it" class="Iterate">
# <result name="success">/pages/Iterate.jsp</result>
# </action>


Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8908
    
    8

You have declared two actions, "doLogin" and "it". Your log indicates you are invoking an action "HelloWorld":

Nishan Patel
Ranch Hand

Joined: Sep 07, 2008
Posts: 687

Actually change your Helloword form action type to "it".


You are submitting from with action "Helloword " which is not found so please change it to "it" in your jsp

<s:form action="it" method="post" />

</s:form>

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: working with hashmap using struts2