简体   繁体   中英

How can I give a hyperlink in a struts2 iterator

I am using the struts 2 iterator to print a ArrayList (Details of a student) of string on the JSP . As the number of details in the ArrayList are more I thought of keeping a hyperlink to one of the fields so that whenever a user clicks on the name of any student, other details of the student should come. Like that click calling a action from where I can go to a Dao to get the other details of that student.

My problem is how can keep a hyperlink to the element in a iterator, even then how to pass the name to action?? Can anyone help me to solve this?

    <table class="display" border="1">
<tr>
    <th>Username</th>
    <th>Role</th>
    <th>Name</th>
    <th>Surname</th>
    <th>Contact number</th>
    <th>Email</th>
</tr>
<s:iterator value="list">
    <tr>
        <td><s:property value="userName"/></td>
        <td><s:property value="role"/></td>
        <td><s:property value="name"/></td>
        <td><s:property value="initial"/></td>
        <td><s:property value="contact"/></td>
        <td><s:property value="email"/></td>
    </tr>
</s:iterator> 

lets say this prints __aadya__, __rolenum__, __name__, __surname__, __contact__, __email__ . when end-user clicks on username, he should be able to get the other details of aadya.

You can do this in several ways .

  1. Load the details eagerly , and render the page with them hidden

    If your table is paginated (and it should), and shows for example 10 or 50 rows per page, you can evaluate if it's the case to call an action or if instead is possible / convenient to load all the students along with their details, hiding them in the page. At this point, clicking on username would trigger a simple javascript to show / hide the details of that student, that are already in the page.

  2. Load the details with a Form post

    Surround each username with a form pointing to the action that loads the student's details, and include an hidden parameter with the id, or put it in the Querystring.

  3. Load the details with an AJAX call

    Attach a Javascript event to an element (an anchor, a div, the td itself, etc...), perform the AJAX call to the action that loads the student's details, parse the response (that can be JSON, or JSP snippet, or a Stream...) to put it in the page (by manually creating the HTML elements, or by injecting the response as-is into a target div). There are several examples on SO, like this and this . Just remember to NOT use struts2-dojo tags for AJAX because they're deprecated from long time ago... use raw jQuery (better in this case), or Struts2-jQuery-plugin (fine the same).

Solution 1 is superfast, I guess it's what you need. Solution 3 is good too, because it's a shame to repost an entire page just to obtain a single data object, as in solution 2 (the old way).

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