action不能往jsp中傳遞數據的問題

struct z中一個action的配置以下:web

 
    path="/library"
    parameter="method"
    name="libraryForm"
    scope="request"
    validate="true"
    type="org.springframework.web.struts.DelegatingActionProxy">
   
        name="listLibrarys"
        path="/form/listlibrarys.jsp"
        redirect="true" />
     
     
    spring

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");
 }
  app

jsp中部分源代碼:jsp


 














                                 
${library.code}
${library.name}
${library.buildTime}
">添加
">修改
">刪除

 

問題:ui


一次也不能循環.spa

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

 log.debug("list size="+list.size()); code

運行結果:list size=6.orm

 緣由是:

 
        name="listLibrarys"
        path="/form/listlibrarys.jsp"
        redirect="true" />

中的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中就接收不到相應的數據

相關文章
相關標籤/搜索