如何在struts2迭代器中提供超链接 [英] How can I give a hyperlink in a struts2 iterator

查看:101
本文介绍了如何在struts2迭代器中提供超链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用struts 2 iteratorJSP上打印字符串ArrayList(学生的详细信息).随着ArrayList中详细信息的数量增加,我想到了保持到这些字段之一的超链接,以便每当用户单击任何学生的姓名时,都应出现该学生的其他详细信息.像这样单击,从那里我可以到Dao处获取一个动作,以获取该学生的其他详细信息.

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> 

让我们说这打印__aadya__, __rolenum__, __name__, __surname__, __contact__, __email__.当最终用户单击用户名时,他应该能够获得aadya的其他详细信息.

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.

推荐答案

您可以通过多种方式来实现. /p>

You can do this in several ways.

  1. 急切加载详细信息,并在页面隐藏的情况下呈现页面

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

如果您的表格是分页的(并且应该如此),并且每页显示例如10或50行,则可以评估是否需要调用操作,或者是否可以/方便地将所有学生与详细信息,将其隐藏在页面中.此时,单击用户名将触发一个简单的javascript,以显示/隐藏该学生已在页面中的详细信息.

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.

通过表单帖子加载详细信息

在每个用户名周围使用指向加载学生详细信息的动作的形式,并包含带有id的隐藏参数,或将其放在查询字符串中.

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.

通过AJAX调用加载详细信息

将Javascript事件附加到元素(锚,div,td本身等),对加载学生详细信息的动作执行AJAX调用,解析响应(可以是JSON或JSP代码段或Stream ...)以将其放入页面中(通过手动创建HTML元素,或将响应原样注入目标div).在SO上有几个示例,例如

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).

解决方案1超级快,我想这就是您所需要的.解决方案3也很好,因为像解决方案2一样(旧方法),重新发布整个页面只是为了获得一个数据对象实在可惜.

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).

这篇关于如何在struts2迭代器中提供超链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆