例如,我有一个JSF页面:<h:form>
<h:inputText value="#{person.name}"/>
//...
<h:commandButton>
<f:actionListener type="PersonListener"/>
</h:commandButton>
</h:form>这里有个Bean,已经在config中注册了:public class Person{
//...
}现在使用一个ActionListener,那在这个类中如何获取Person的实例呢(使用myfaces)?public PersonListener implements ActionListener{
public void processAction(ActionEvent event){
//how could I get the person instance in the above jsf page?
}
}

解决方案 »

  1.   

    public PersonListener implements ActionListener{
    public void processAction(ActionEvent event){
        FacesContext context = FacesContext.getCurrentInstance();
        ValueBinding binding =
        context.getApplication().createValueBinding("#{person}");
        Person person = (Person) binding.getValue(context);
      }
    }
      

  2.   

    为何你的程序与JSF耦合得如此强?
      

  3.   

    jsp调用Bean.比较直接一点,就用getXXX()获取,,,setXXX赋值..
    或者,直接用jsp标签..别把问题想复杂了.
      

  4.   

    to ochinchina() :
    我是个新手,对这个不太熟悉。那你认为该如何做才能降低耦合度呢?谢谢!
      

  5.   

    FacesContext context = FacesContext.getCurrentInstance( );
            ELContext elContext = context.getELContext();
            ExpressionFactory expressionFactory = context.getApplication().getExpressionFactory();         
            ValueExpression ve = expressionFactory.createValueExpression(elContext, "#{ person}", Person.class);
    Person p = (Person) ve.getValue(elContext);