不推荐使用JSF 1.x ValueBinding,正确的替换是什么?

sen*_*ale 8 jsf binding deprecated

我有一些JSF 1.0/1.1代码:

FacesContext context = FacesContext.getCurrentInstance();
ValueBinding vb = context.getApplication().createValueBinding("#{someBean}");
SomeBean sb = (SomeBean) vb.getValue(context);
Run Code Online (Sandbox Code Playgroud)

从JSF 1.2开始,ValueBinding不推荐使用并替换为ValueExpression.我不知道如何更改上面的代码才能使用ValueExpression.

Bal*_*usC 17

那个部分

ValueBinding vb = context.getApplication().createValueBinding("#{someBean}");
SomeBean sb = (SomeBean) vb.getValue(context);
Run Code Online (Sandbox Code Playgroud)

应该被替换

ValueExpression ve = context.getApplication().getExpressionFactory().createValueExpression(context.getELContext(), "#{someBean}", SomeBean.class);
SomeBean sb = (SomeBean) ve.getValue(context.getELContext());
Run Code Online (Sandbox Code Playgroud)

或更好

SomeBean bc = context.getApplication().evaluateExpressionGet(context, "#{someBean}", SomeBean.class);
Run Code Online (Sandbox Code Playgroud)

也可以看看:

  • 本答案应标记为正确答案 (2认同)