首先要引进<%@ taglib uri="/tags/struts-nested" prefix="nested"%>
在这次的编码中用到这里的nested:iterate、nested:write俩个标签。
nested:iterate:负责得到action中给出的list
nested:write:负责将list中的内容输出
注意:这里一定要在引用的action所对应的form中对这个list进行set、get,不然jsp页面取不到值。
到action之前正常运行的前提下,JSP页面的编写:
<nested:iterate id="obj" property="list" name="selectUserForm"
indexId="idx">
<tr>
<td><nested:write property="user_id"/></td>
<td><nested:write property="name" /></td>
<td><nested:write property="age" /></td>
<td><nested:write property="sex" /></td>
<td><nested:write property="birthday" /></td>
<td>
<input type="hidden" name="userId" value="<nested:write property='user_id'/>">
<input type="button" value="删除" id="delete"
οnclick="deleteUser()"></input></td>
</tr>
</nested:iterate>
property表示的是在action的request.setAttribute("list", ***) 中的list,***为我自己定义的将要传值的list。
name:这个一般都被忘记,它是在配置struts时xml中相对应form的<form-bean name="selectUserForm">
如果缺失,页面将抛异常。
indexId是迭代变量。int型,用的时侯随便起变量名,
nested:write这个标签可以直接将list中的元素解包,直接在property后写入变量名就可以。
附一个网上找的例子:
/*在action里servletRequest.setAttribute(“survey“,survey);
jsp文件片断(注意:不需usebean,标签会自动找):
*/
<nested:write name="survey" property="title"/><br>
<nested:write name="survey" property="poster"/><br>
<nested:write name="survey" property="postDate"/><br>
<nested:iterate id="item" name="survey" property="items" type="survey.SurveyItem">
<nested:equal name="item" property="type" value="select_single">
<nested:write property="title"/><br>
options:<br>
<nested:iterate id="option" name="item" property="options" type="survey.SurveyItemOption">
<nested:write property="index"/>
<nested:write property="text"/><br>
</nested:iterate>
</nested:equal>
<nested:equal name="item" property="type" value="select_multi">
<nested:write property="title"/><br>
options:<br>
<nested:iterate id="option" name="item" property="options" type="survey.SurveyItemOption">
<nested:write property="index"/>
<nested:write property="text"/><br>
</nested:iterate>
</nested:equal>
<nested:equal name="item" property="type" value="text">
<nested:write property="title"/><br>
</nested:equal>
</nested:iterate>