Trying to nest JSF expression strings

▼魔方 西西 提交于 2020-01-05 04:50:14

问题


I'd like to know whether it is possible to adapt the ExtendedBeanELResolver from the question nesting-jsf-expression-strings as well to handle this nested EL expression:

#{controllerBean.getBean('userProfileBean', component).street}*

whereby controllerBean.getBean returns the bean userProfileBean. I'll get with the ExtenedBeanELResolver following exception:

SCHWERWIEGEND: javax.el.PropertyNotFoundException: /WEB-INF/templates/modification/userProfile.xhtml @35,196 value="#{controllerBean.getBean('userProfileBean', component).street}": Property 'getBean' not found on type com.something.ControllerBean

If I insert additional brackets it works, but looks even more ugly than now:

#{(controllerBean.getBean('userProfileBean', component)).street}*

Is it possible to do without the additional brackets?

UPDATE 1 after CycDemo's answer:

Strange problem. If i put the

<h:inputText value="#{beanOne.getBean('data').title}" />

inside a form the page renders, but only until i submit the form. After that the same error will be shown.

WARNUNG: /WEB-INF/templates/home.xhtml @61,66 value="#{beanOne.getBean('data').title}": Property 'getBean' not found on type com.something.BeanOne

If i change it to

<p:inputText value="#{beanOne.getBean('data').title}" />

the page won't even render in the beginning. I think the problem is that the JSF is trying to call the setter when the form is submitted but can't evaluate it correctly.

Any ideas?

UPDATE 2

Appearently JSF is trying to call getGetBean() because it is looking for a property on BeanOne (I had a breakpoint there in eclipse) which is wrong:

@ManagedBean
@ViewScoped
public class BeanOne {

  private Object bean = new String("something");

  public Object getBean(String name) {
    FacesContext context = FacesContext.getCurrentInstance();
    return context.getApplication().evaluateExpressionGet(context,
        "#{" + name + "}",
        Object.class);
  }

  public Object getGetBean() {
    return bean; // <-- will be called by JSF
  }

  public void setGetBean(Object bean) {
    this.bean = bean;
  }

}

回答1:


Make sure

    #{(controllerBean.getBean('userProfileBean', component)).street}
    change to
    #{controllerBean.getBean('userProfileBean', component).street}

It is work in JSF 2.0 when I test.

Use jboss-el. Download jar file here

BeanOne.java import javax.faces.bean.ManagedBean; import javax.faces.bean.RequestScoped;

@ManagedBean(name="BeanOne")
@RequestScoped
public class BeanOne {
    private Data data;

    public Data getBean(String title) {
        data = new Data(title);
        return data;
    }
    public void show() {
        System.out.println("User Input ==>" + data.getInput());
    }
}

Date.java

public class Data {
    private String title;
    private String input;

    public Data(String title) {
        this.title = title;
    }

    public String getInput() {
        return input;
    }

    public void setInput(String input) {
        this.input = input;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }
}

pageOne.xtml

<h:form>
    <h:inputText value="#{BeanOne.getBean('Test').input}"/><br/>
    <h:commandButton value="Show" action="#{BeanOne.show}"/>
</h:form>

add the following configuration in web.xml

<context-param>     
    <param-name>com.sun.faces.expressionFactory</param-name>
    <param-value>org.jboss.el.ExpressionFactoryImpl</param-value>   
</context-param>


来源:https://stackoverflow.com/questions/12929488/trying-to-nest-jsf-expression-strings

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!