简体   繁体   中英

How to pass an object inside a jsp page [s:iterator] to an action class?

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> 

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 .

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM