自动填充Bean的三种方式

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();
        }
  
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值