action与jsp之间传递数据的方法
一、jsp接受action中传来的数据
action调用service中业务函数产生一个数据,例如一个list,将这个list传给jsp进行显示(利用<s:iterator>标签循环显示出来)。
①首先action自动执行execute函数后跳转到某个jsp页面,需要在struts.xml中进行配置。执行完execute函数需要返回一个值,struts.xml根据这个返回值判断需要跳转到哪个jsp页面。例如下面是返回success就跳转到listCategory.jsp页面。
<action name="category" class="categoryAction">
<result name="list">catagory/listCategory.jsp</result>
<!--<result name="input">catagory/listCategory.jsp</result>
--><result name="success">catagory/listCategory.jsp</result>
</action>
②action中list的名字为List,那么在jsp页面中就要用到这个名字List进行获取,使用<s:iterator>标签获取List并循环输出。iterator标签中的value值是从action中取得的变量的名字。
<s:iterator value="threadList" var="thread">
<tr>
<td width="5%">+_+</td>
<th style="width:35%">帖子名称</th>
<td style="width:15%">作者</td>
<th style="width:10%">回复数</th>
<td style="width:20%">最后回复时间</td>
<td style="width:15%">最后回复</td>
</tr>
<tr>
<td>❤</td>
<th><s:property value="#thread.title"/></th>
<th><s:property value="#thread.account"/></th>