Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

working with hashmap using struts2

 
Aditi Gandy
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 !
 
Joe Ess
Bartender
Posts: 9320
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Aditi Gandy
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
please help me.

Regards,
Ashvini
 
Nishan Patel
Ranch Hand
Posts: 689
Eclipse IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.


 
Aditi Gandy
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 689
Eclipse IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 9320
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have declared two actions, "doLogin" and "it". Your log indicates you are invoking an action "HelloWorld":

 
Nishan Patel
Ranch Hand
Posts: 689
Eclipse IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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>

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic