2

I have a JSP page displaying an object called docsFacebook. I need to pass this object to an action class in order to process its content. How to do it?

JSP

<s:iterator value="docsFacebook">
    <p>
       <img src="images/fb.png" />
       <b><s:property value="Newspaper"/></b>       
       <s:property value="Date"/></p>               
       <p><s:property value="Message"/></p>              
       <p><a href=<s:property value="Link_url"/>>
       <s:property value="Link_url"/></a>
    </p>
    <br></br>           
</s:iterator>

In the same page I have a button with a link to an action class..

<a href="<s:url action="display" />">Facebook</a>

How to retrieve this data from that action class?

struts.xml

<struts>
    <package name="default" extends="struts-default">
        <action name="doFirstQuery" class="action.FirstQuery">
            <result name="success">success.jsp</result>
            <result name="failure">error.jsp</result>               
        </action>
        <action name="doFollowingQuery" class="action.FollowingQuery">
            <result name="success">success.jsp</result>                         
        </action> 
        <action name="display" class="action.Display">
            <result name="success">dis.jsp</result>                         
        </action>           
    </package>
</struts> 
Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243
  • So you want to pass `docsFacebook` object to `display` action? – SatyaTNV Sep 06 '15 at 10:46
  • Do you want to pass or retrieve the data or both? What did you try? – Roman C Sep 06 '15 at 11:23
  • Yes, i want to pass docsFacebook to display action in order to visualize that object inside a new jsp page linked to that class – Alessandro Pietra Sep 06 '15 at 11:26
  • what is the obj type `docsFacebook`. – SatyaTNV Sep 06 '15 at 11:27
  • docsFacebook is of type array Risultato[] ---------------------------------------------------------------------------------- public class Risultato { String Date; String Id; String Index; String Message; String N_likes; String Newspaper; String Link_url; public Risultato(String date, String id, String index, String message, String n_likes, String newspaper, String link_url) { super(); Date = date; Id = id; Index = index; Message = message; N_likes = n_likes; Newspaper = newspaper; Link_url = link_url; } getters and setters.... – Alessandro Pietra Sep 06 '15 at 11:32

2 Answers2

1

1 - Appropriate accessors / mutators:

Since you are reading an array, you need an array getter in the source action:

public Risultato[] getDocsFacebook(){...}

then in the page you are choosing a single element, and hence you need an object setter in the destination action:

public void setDocsFacebook(Risultato risultato){...}

2 - Sending the object:

To send the object you have two problems:

  • the properties printed with are not form elements, but plain text; you need then to use an hidden field for everyone of them to send the value:

    <s:property value="foo" />
    <s:hidden    name="foo" />
    
    <s:textfield name="bar" />
    
  • To send a single object from within an iterated collection, you need to specify an index with OGNL. The whole thing is described quite well in this answer, be sure to read it all.

3 - The INPUT result

Your (awfully italian-localized) logs are telling you that, since there is some kind of conversion or validation error, an INPUT result is returned.

INPUT is different from ERROR because it indicates a recoverable error in the data inserted by the user, and it should ask the user to enter them again, instead for example of land to an error page.

I strongly suggest you to read what the INPUT result is.

Community
  • 1
  • 1
Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243
  • thank you. So: in the action class Display(destination action called by my jsp page) i have to define a variable of type Risultato because I have to read each element of the list...i need also getter and setter methods. In my jsp page how can i use the hidden tag to get the values of the different fields of type Risultato? – Alessandro Pietra Sep 07 '15 at 16:58
  • I've decided to solve my problem using the interface SessionAware. I have put my object in the session but i'm not able to retrieve it from my jsp page. – Alessandro Pietra Sep 07 '15 at 19:26
  • 1
    You need to stop coding and read a bit about how it works. It would be a lot easier. IF you can choose *only one* element in the JSP to send to *display* action, then you need a setter for a Risultato. If you can choose *multiple* elements at once, you need a setter for Risultato[], or List. The hidden tag usage it's in the answer I've linked in the point 2. As said, **be sure to read it all**... – Andrea Ligios Sep 07 '15 at 21:40
0

Create a variablename (some property to hold the docsFacebook value) property in Display action class and write setter and getter methods to it.

If docsFacebook is of type Risultato then

In Dsiaply action class

private Risultato variablename;

public Risultato setVariablename(Risultato variablename)
{
     ............
}
public Risultato getVariablename()
{
     .............
}

Now in JSP call action class method

 <a href='display?variablename=<s:property value="docsFacebook"/>'>Facebook</a>

If you clicks on link Facebook the display action class method will be called.

If docsFacebook is of type Risultato[] then replace Risultato with Risultato[] in Display action class.

SatyaTNV
  • 4,137
  • 3
  • 15
  • 31