JSF参数传递方式说明

先说明一种:
-----------------------------------------

Html代码   收藏代码
  1. < h:commandLink   value = "整理材料 "   action = "#{documentPressBackingBean.cleanDatum}"   target = "HideFrame" >   
  2.  < f:param   name = "documentArchiveId"   value = "#{pre.documentArchiveId}" > </ f:param >   
  3. </ h:commandLink >   
  4. < h:outputLabel   styleClass = "icoArrange"   id = "show" >   
  5. </ h:outputLabel >   
  6. < h:commandButton   value = "整理材料"   action = "#{documentPressBackingBean.cleanDatum}"    
  7. onclick = "this.form.target='HideFrame';"   style = "display:none" >   
  8. < x:updateActionListener   property ="#  
  9. {documentPressBackingBean.documentArchiveId}" value = "#{pre.documentArchiveId}"   />   
  10. </ h:commandButton >   


------------------------------------------------

以下是其他方式:

JSF参数传递方式之一:f:param标签

页面到Bean的参数传递
页面中设置参数:

Java代码   收藏代码
  1. <h:form>      
  2.             <h:commandLink value="Test2"  action= "#{paramBean.test}" >      
  3.                 <f:param name="name"  value= "zhang" ></f:param>      
  4.                 <f:param name="id"  value= "123456" ></f:param>      
  5.             </h:commandLink>      
  6. </h:form>     


注意:这里只能使用h:commandLink标签,而不能使用h:commandButton标签!
后台取参数:
(1) 通过Request对象取值

Java代码   收藏代码
  1. HttpServletRequest request = (HttpServletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();      
  2.         request.getParameter("name" );     



(2)通过RequestParameterMap取值

Java代码   收藏代码
  1. Map varMap = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();      
  2.         varMap.get("id" );     


(3)通过配置文件进行Bean的属性值注入,在Bean的方法中直接使用属性

Xml代码   收藏代码
  1. < managed-bean >       
  2.   < managed-bean-name > paramBean </ managed-bean-name >       
  3.   < managed-bean-class > com.spg.bean.ParamBean </ managed-bean-class >       
  4.   < managed-bean-scope > request </ managed-bean-scope >       
  5.   < managed-property >       
  6.    < property-name > id </ property-name >       
  7.    < property-class > java.lang.String </ property-class >       
  8.    < value > #{param.id}  </ value >       
  9.   </ managed-property >       
  10. </ managed-bean >      


页面到页面的参数传递
页面中设置参数:
(1)

Html代码   收藏代码
  1. < h:outputLink   value = "param2.jsf" >       
  2.                 < h:outputText   value = "Test4" > </ h:outputText >       
  3.                 < f:param   name = "name"   value = "chen" > </ f:param >       
  4.                 < f:param   name = "id"   value = "123456" > </ f:param >       
  5.     </ h:outputLink >      


(2)

Html代码   收藏代码
  1. < h:outputLink   value = "param2.jsf?name=chen&id=123456" >       
  2.                 < h:outputText   value = "Test4" > </ h:outputText >       
  3.     </ h:outputLink >      


注意:以上两种方法,不能同时使用!
页面中取参数:
(1) 使用JSF的值表达式

Html代码   收藏代码
  1. < h:outputText   value = "#{param.name}" > </ h:outputText >        
  2.     < h:outputText   value = "#{param.id}" > </ h:outputText >      


(2) 使用JSP的表达式

Html代码   收藏代码
  1. < %=request.getParameter("name")% >       
  2.     < %=request.getParameter("id")% >     



JSF参数传递方式之二:Backing Bean 与 h:inputHidden标签

Java代码   收藏代码
  1. Backing Bean  
  2.   
  3. import  javax.faces.component.UIInput;      
  4. import  javax.faces.component.UIOutput;      
  5.      
  6. public   class  BackingBean      
  7. {      
  8.      
  9.     private  UIOutput idComponent;      
  10.    
  11.     public  UIOutput getIdComponent()      
  12.     {      
  13.         return  idComponent;      
  14.     }      
  15.      
  16.     public   void  setIdComponent(UIOutput idComponent)      
  17.     {      
  18.         this .idComponent = idComponent;      
  19.     }       
  20. }   



页面到Bean的参数传递
页面中设置参数:

Html代码   收藏代码
  1. < h:form >       
  2.             < h:inputHidden   value = "123456"   binding = "#{backingBean.idComponent}" > </ h:inputHidden >       
  3.             < h:commandButton   value = "登录"   action = "#{paramBean.login}" > </ h:commandButton >       
  4. </ h:form >      


后台取参数:

Java代码   收藏代码
  1. FacesContext context = FacesContext.getCurrentInstance();      
  2.         BackingBean backBean =(BackingBean)context.getApplication().getVariableResolver().resolveVariable(context,"backingBean" ); //该方法已经过时       
  3.         BackingBean bean =(BackingBean)context.getApplication().getELResolver().getValue(context.getELContext(), null"backingBean" );      
  4.         backBean.getIdComponent().getValue();      
  5.         bean.getIdComponent().getValue();     
  6.   
  7. FacesContext context = FacesContext.getCurrentInstance();  


2.2页面到页面的参数传递
页面中设置参数:

Html代码   收藏代码
  1. < h:form >       
  2.         < h:inputHidden   value = "123456"              binding = "#{backingBean.idComponent}" > </ h:inputHidden >       
  3.         < h:commandButton   value = "Test5"     action = "param" > </ h:commandButton >       
  4.         < h:commandLink   value = "Test6"   action = "param" > </ h:commandLink >       
  5.     </ h:form >    


注意:h:outputLink 标签不能使用该方式传递参数!
页面中取参数:

Html代码   收藏代码
  1. < h:outputText   value = "#{backingBean.idComponent.value}" > </ h:outputText >     



JSF参数传递方式之三:通过session(application)对象传递

页面到Bean的参数传递
页面中设置参数:

Html代码   收藏代码
  1. < h:form >       
  2.             < %session.setAttribute("name","hujilie"); % >       
  3.             < %application.setAttribute("id","123456"); % >       
  4.             < h:commandButton   value = "Test8"   action = "#{paramBean.test2}" > </ h:commandButton >       
  5.             < h:commandLink    value = "Test8"   action = "#{paramBean.test2}" > </ h:commandLink >       
  6.     </ h:form >    

 
后台取参数:

Java代码   收藏代码
  1. FacesContext context = FacesContext.getCurrentInstance();      
  2.         Map sessionMap =context.getExternalContext().getSessionMap();      
  3.         Map applicationMap = context.getExternalContext().getApplicationMap();      
  4. HttpSession session =(HttpSession) context.getExternalContext().getSession(true );      
  5. ServletContext application = (ServletContext)context.getExternalContext().getContext();      
  6.         sessionMap.get("name" );      
  7.         applicationMap.get("id" );      
  8.         session.getAttribute("name" );      
  9.         application.getAttribute("id" );   


页面到页面的参数传递
页面中设置参数:

Html代码   收藏代码
  1. < h:form >       
  2.             < %session.setAttribute("name","hujilie"); % >       
  3.             < %application.setAttribute("id","123456"); % >       
  4.             < h:outputLink   value = "param2.jsf" > Test10 </ h:outputLink >       
  5.     </ h:form >    


页面中取参数:

Html代码   收藏代码
  1. < h:outputText   value = "#{sessionScope.name}" > </ h:outputText > < br >       
  2.     < h:outputText   value = "#{applicationScope.id}" > </ h:outputText >      



JSF参数传递方式之四:f:attribute标签传递

页面到Bean的参数传递
页面中设置参数:

Html代码   收藏代码
  1. < h:form >       
  2.             < h:commandButton   action = "#{paramBean.test3}"   value = "Test11"   actionListener = "#{paramBean.changeName}" >       
  3.                 < f:attribute   name = "name"   value = "hujilie" />       
  4.             </ h:commandButton >       
  5.             < h:commandLink   action = "#{paramBean.test3}"   value = "Test12"   actionListener = "#{paramBean.changeName}" >       
  6.                 < f:attribute   name = "name"   value = "hujilie" />       
  7.             </ h:commandLink >       
  8.     </ h:form >    



后台取参数:

Java代码   收藏代码
  1. public   void  changeName(ActionEvent e)      
  2.     {      
  3.         UIComponent component = e.getComponent();      
  4.         Map<String, Object> map = component.getAttributes();      
  5.         setName((String)map.get("name" ));      
  6.     }    



JSF参数传递方式之五:f:setPropertyActionListener 标签传递

页面到Bean的参数传递
页面中设置参数:

Html代码   收藏代码
  1. < h:form >       
  2.             < h:commandButton   action = "#{paramBean.test3}"   value = "Test14" >      
  3.                 < f:setPropertyActionListener   value = "hujilie"   target = "#{paramBean.name}" />       
  4.             </ h:commandButton >       
  5.             < h:commandLink   action = "#{paramBean.test3}"   value = "Test15" >       
  6.                 < f:setPropertyActionListener   value = "hujilie"   target = "#{paramBean.name}" />       
  7.             </ h:commandLink >       
  8.     </ h:form >      


后台取参数:直接使用属性的值。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值