Struts中的Action使用request不当导致前端无法找到相应的属性
在使用Struts时,我们经常在Action中向request存储结果有两种方式,一种是直接使用setAttribute()方法,一种是先调用request的getsession()方法后,然后再使用setAttribute()方法。
开始由于不知道这两者的差别,我使用了后一种方式,即request.getSession().setAttribute(),但是这样在页面中使用logic:present时,如:
??????????<logic:present?name="Customers"?scope="request">
??????????????<logic:iterate?id="customer"?name="Customers"?scope="request">
??????????????????<tr><td>$
{customer.cname?}</td><td>$
{customer.code?}</td></tr>
??????????????</logic:iterate>
??????????</logic:present>
如果定义scope为request.程序是始终都找不相应attribute的。
因为前一种方式是在request的范围内设置Attribute,而后一种方式中的getSession()是获取当前的session,这样设置Attribute当然是在session范围内设置Attribute,在前台当然是找不到相应的属性的。