action不能往jsp中传递数据的问题

struts中一个action的配置如下:





action 中部分源代码:

public ActionForward list(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
log.debug(“list library “);

List list = libraryManagerService.listLibrarys();

if(list == null) log.debug(“there is not any library ,because list is null”);
else log.debug(“there are ” + list.size() + ” librarys .”);

request.setAttribute(“list”, list) ;
// request.getSession().setAttribute(“list”, list);
return mapping.findForward(“listLibrarys”);
}

jsp中部分源代码:

${library.code} ${library.name} ${library.buildTime} “>添加 “>修改 “>删除




问题:


一次也不能循环.

在action的 List list = libraryManagerService.listLibrarys();后中加入:

log.debug(“list size=”+list.size());

运行结果:list size=6.

原因是:

中的redirect=”true”.直接转向后.request中的数据丢失.

改为redirect=”false”即可.

redirect 决定了action在使用forward跳转的时候是使用

RequestDispatcher rd = getServletContext().getRequestDispatcher(uri).forward(request, response);

还是

response.sendRedirect(response.encodeRedirectURL(uri));

在structs的类RequestProcessor中processForwardConfig方法里面有如下代码:

if(forward.getRedirect())//判断redirect是否为true
{
if(uri.startsWith(“/”))
uri = request.getContextPath() + uri;
response.sendRedirect(response.encodeRedirectURL(uri));
} else
{
doForward(uri, request, response);
}

其中的方法doForward如下:

protected void doForward(String uri, HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException
{
RequestDispatcher rd = getServletContext().getRequestDispatcher(uri);
if(rd == null)
{
response.sendError(500, getInternal().getMessage(“requestDispatcher”, uri));
return;
} else
{
rd.forward(request, response);
return;
}

这两种跳转方法的区别是:

forward 会将 request state , bean 等等信息带往下一个 jsp

redirect 是送到 client 端后,再由client向服务器发送request,服务器接收到后来发的request中已经不包含上次的request的state,bean等信息了。所以在jsp中就接收不到相应的数据

发表评论