---
title: "托管 Bean 页面传值的使用方法和技巧 "
author: "lichangwei"
date: "2009-01-06T23:18:04Z"
tags: []
slug: "bean"
draft: false
lastmod: "2009-01-06T23:18:04Z"
id: 311241
url: "https://lichangwei.iteye.com/blog/311241"
---

[托管 Bean 页面传值的使用方法和技巧](http://www.blogjava.net/algz/articles/193459.html)

最近开始学习JSF基础，发现比较重要的一点，就是如何用编程方式访问托管Bean。看了一些JSF的实例，不过大多都是用JSF1.1来实现的。虽然在JSF1.2的环境中也可以很好运行，但是在编译的时候会看到降级的消息。这里找了一些资料总结一下JSF1.1和JSF1.2访问托管Bean的方法。

**一、从JSF页面传递参数给托管Bean**  
虽然利用h:commandLink 和h:commandButton组件，可以通过action和actionListener来触发托管Bean中的方法，但是不能向这些方法中传递参数。对于动态传递参数，不是不可以实现，这点可以通过使用f:attribute来实现。而且f:attribute也可以很好的和actionListener联合使用。  
例子：

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;h:commandLink actionListener="#{myBean.action}"&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)        &lt;f:attribute name="attrname1" value="attrvalue1" /&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)        &lt;f:attribute name="attrname2" value="attrvalue2" /&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)        ...     
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)        &lt;h:outputText value="Click here" /&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    &lt;/h:commandLink&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)      
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    &lt;h:commandButton value="Press here" actionListener="#{myBean.action}"&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)        &lt;f:attribute name="attrname1" value="attrvalue1" /&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)        &lt;f:attribute name="attrname2" value="attrvalue2" /&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)        ...  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    &lt;/h:commandButton&gt;

这些属性可以通过父组件的getAttributes()方法取到，父组件可以通过传递给actionListener的ActionEvent实例取到

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)public void action(ActionEvent event)   
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)    {     
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        String attrvalue1 = (String) event.getComponent().getAttributes().get("attrname1");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        String attrvalue2 = (String) event.getComponent().getAttributes().get("attrname2");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        ...  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)    }

变量attrvalue1和attrvalue2包含从f:attribute set进来的值。  
另一个欠文雅的方式就是通过f:param组件来传值，这个只是对h:commandLink起效。

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;h:commandLink action="#{myBean.action}"&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)        &lt;f:param name="paramname1" value="paramvalue1" /&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)        &lt;f:param name="paramname2" value="paramvalue2" /&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)        ...  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)        &lt;h:outputText value="Click here" /&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    &lt;/h:commandLink&gt;

这些参数可以通过FacesContext的getRequestParameterMap()方法取到。通过下面的方法，可以获取任何写在command块中f:param的值。

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)public static String getFacesParamValue(String name)   
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)    {     
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        return (String) FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get(name);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)    }

上面的方法可以用在任何Bean的方法中。举例

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)public void action()   
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)    {     
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        String paramvalue1 = getFacesParamValue("paramname1");     
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        String paramvalue2 = getFacesParamValue("paramname2");     
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        ...  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)    }

变量paramvalue1和paramvalue2包含从f:param set进来的值。  
不过要注意，属性的名字要唯一，而且不能用其他组件的属性名，比如："id", "name", "value","binding", "rendered",等。

**二、从JSF页面传递组件属性到托管Bean**  
f:attribute组件也可以用来访问，任何绑定在托管Bean的UI组件的属性。这些属性值可以通过父组件的getAttributes()来获得。因为不能以方法参数的方式传值给托管Bean绑定的UI组件的getter和setter方法，这时f:attribute就会非常有用。这里有个UI组件绑定到托管Bean的基础例子

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;h:outputText binding="#{myBean.myText}" value="#{myBean.myTextValue}"&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)        &lt;f:attribute name="attributename" value="attributevalue" /&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    &lt;/h:outputText&gt;

 

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)托管Bean代码：  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import javax.faces.component.html.HtmlOutputText;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)public class MyBean   
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif){     
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    private HtmlOutputText myText;   
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)      
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    public HtmlOutputText getMyText()   
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)    {     
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        return myText;    
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)    }     
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)      
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    public String getMyTextValue()   
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)    {         
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        return (String) myText.getAttributes().get("attributename");     
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)    }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)      
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    public void setMyText(HtmlOutputText myText)   
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)    {         
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        this.myText = myText;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)    }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)}

**三、在请求之间传递对象（Passing objects from request to request）**  
如果有一个request范围内的托管Bean，在下一个请求中想要重用这个bean的属性，参数，或者对象，但是不想一遍又一遍的初始化这个Bean。可以用h:inputhidden来解决这个问题。这里有个简单的例子：

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;h:form&gt;     
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)        ...  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)        &lt;h:inputHidden value="#{myBean.value}" /&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)      ...  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    &lt;/h:form&gt;

另一种方法就是用SessionMap来保存那些需要保存在session中的值。

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)public class MyBean   
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif){     
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    public Object getMyValue()   
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)    {         
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        return FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("MyBeanMyValue");   
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)    }     
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)      
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    public void setMyValue(Object myValue)   
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)    {         
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("MyBeanMyValue", myValue);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)    }    
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    // This might be useful.     
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    private void resetSessionValue(Object myKey)   
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)    {        
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        FacesContext.getCurrentInstance().getExternalContext().getSessionMap().remove(myKey);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)    }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)}

**四、在托管Bean之间通信**  
实践时会有多个托管Bean。如果设计需要，可以利用getSessionMap()在托管Bean之间通信。  
一个例子，有两个托管Bean：

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;managed-bean&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)        &lt;managed-bean-name&gt;myBean1&lt;/managed-bean-name&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)        &lt;managed-bean-class&gt;mypackage.MyBean1&lt;/managed-bean-class&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)        &lt;managed-bean-scope&gt;request&lt;/managed-bean-scope&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    &lt;/managed-bean&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    &lt;managed-bean&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)        &lt;managed-bean-name&gt;myBean2&lt;/managed-bean-name&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)        &lt;managed-bean-class&gt;mypackage.MyBean2&lt;/managed-bean-class&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)        &lt;managed-bean-scope&gt;session&lt;/managed-bean-scope&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    &lt;/managed-bean&gt;

托管Bean myBean1和myBean2是支撑Bean MyBean1.java和MyBean2.java的实例，它们可以通过JSF页面来访问。不管它们的managed-bean-scope是request还是session。如果managed-bean-scope被设置成session，在整个session范围内可以使用同一个Bean的实例；如果managed-bean-scope被设置成request，那么每个request（form action）每次会创建一个Bean的实例。

用下面的方法，可以为每个托管Bean set和get static的参数。可以把它写成protected方法，放在一个超类中，让每个托管Bean继承，这会非常有用，或者写入一个Util类中，自己选择。

 

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)public class MySuperBean   
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif){     
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    protected static Object getSessionValue(Object myKey)   
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)    {         
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        return FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get(myKey);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)    }     
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)      
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    protected static void setSessionValue(Object myKey, Object myValue)   
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)    {         
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put(myKey, myValue);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)    }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)public class MyBean1 extends MySuperBean   
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif){     
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    public void myMethod()   
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)    {         
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        String myValue = "value1";         
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        setSessionValue("MyBean1MyValue", myValue);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        ...     
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)    }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)public class MyBean2 extends MySuperBean   
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif){     
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    public void myMethod()   
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)    {         
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        String myValue = (String) getSessionValue("MyBean1MyValue");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        ...     
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)    }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)}

**五、在托管Bean中访问另一个托管Bean**  
如果有很多托管Bean，想要从其中一个托管Bean访问其他的托管Bean，这里有六中方法。  
可以使用：  
  1）getVariableResolver  
  2）createValueBinding  
  3）getELResolver (since JSF 1.2)  
  4）createValueExpression (since JSF 1.2)  
  5）getRequestMap  
  6）getSessionMap.  
举例：假设有两个托管Bean

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;managed-bean&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    &lt;managed-bean-name&gt;myBean1&lt;/managed-bean-name&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    &lt;managed-bean-class&gt;mypackage.MyBean1&lt;/managed-bean-class&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    &lt;managed-bean-scope&gt;request&lt;/managed-bean-scope&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;/managed-bean&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;managed-bean&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;managed-bean-name&gt;myBean2&lt;/managed-bean-name&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    &lt;managed-bean-class&gt;mypackage.MyBean2&lt;/managed-bean-class&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    &lt;managed-bean-scope&gt;session&lt;/managed-bean-scope&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;/managed-bean&gt;

托管Bean myBean1和myBean2是支撑Bean MyBean1.java和MyBean2.java的实例，它们可以通过JSF页面来访问。不管它们的managed-bean-scope是request还是session。  
只是要注意的是，getRequestMap在托管Bean被设置成request的时候使用，getSessionMap在托管Bean被设置成session的时候使用。

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;h:form&gt;     
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    &lt;h:commandButton action="#{myBean1.action1}" value="action1" /&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    &lt;h:commandButton action="#{myBean1.action2}" value="action2" /&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    &lt;h:commandButton action="#{myBean1.action3}" value="action3" /&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    &lt;h:commandButton action="#{myBean1.action4}" value="action4" /&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    &lt;h:commandButton action="#{myBean1.action5}" value="action5" /&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    &lt;h:commandButton action="#{myBean1.action6}" value="action6" /&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    &lt;h:outputText binding="#{myBean2.text}" /&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;/h:form&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)MyBean1.java:  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)package mypackage;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import javax.faces.context.FacesContext;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)public class MyBean1   
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif){      
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    // Using VariableResolver. NOTE: this is deprecated since JSF 1.2!     
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    public void action1()   
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)    {         
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        FacesContext context = FacesContext.getCurrentInstance();         
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        MyBean2 myBean2