自動填滿Bean的三種方式

來源:互聯網
上載者:User
1.使用中間過渡的JSP,使用JSP:setProperty和JSP:forward標籤   登陸頁面沒有什麼特別,只是form的action屬性設定成過渡頁面savebean.jsp 

savebean.jsp
      <jsp:useBean id="info" scope="session" class="untitled2.Infoo">
      </jsp:useBean>
      <jsp:setProperty name="info" property="*"/>
      <%
         request.setAttribute("info",info);
      %>
      <jsp:forward page="/cookie"/>
      其中/cookie為處理邏輯的Servlet
  cookie中得到封裝好的Bean,取其中的屬性
      Infoo i=(Infoo)request.getAttribute("info");   (Infoo為Bean類)
2.不使用中間過渡JSP   只需要加上<%
     request.setAttribute("info",info);
     if(request.getParameter("action")!=null&&request.getParameter  
            ("action").equals("4"))...{
       RequestDispatcher dis=request.getRequestDispatcher("/cookie");
       dis.forward(request,response);
     }
   %>
  form表單提交到自身,但加上action=4的參數,如果判斷參數為4,則說明表單已
    經提交,Bean封裝成功,需要跳轉到Servlet,此處要使用forward,不使用
    SendRedirect跳轉,是因為要把儲存Bean資訊的Request一同轉到Servlet中

 3.使用Jakarta提供的BeanUtils包,此方法無需對JSP頁面做任何處理  Servlet中 Infoo i=new Infoo();
      try ...{
            BeanUtils.populate(i,request.getParameterMap());
        } catch (Exception ex) ...{
            ex.printStackTrace();
        }  

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.