1

有没有一种方法可以显式调用 JSP/JSF 表达式语言 (EL) 来替代我可能BeanUtils.setProperty用来设置嵌套和索引属性的方法?

例如,调用BeanUtils.setProperty(object, "foo.bar", "value")将导致object.getFoo().setBar("value")通过反射调用。

有没有办法我可以在 EL 中做到这一点?

我正在尝试做的类似于<h:inputText value="#{object.foo.bar}">但在后端方法中而不是通过 UI,其中“object.foo.bar”属性表达式可能是动态的。我知道BeanUtils有效,但感觉有些过时。

4

1 回答 1

0

你可以用ValueExpression#setValue()这个。

FacesContext context = FacesContext.getCurrentInstance();
ELContext elContext = context.getELContext();
ValueExpression expression = context.getApplication().getExpressionFactory()
    .createValueExpression(elContext, "#{object.foo.bar}", String.class);
expression.setValue(elContext, "value");
于 2012-07-03T16:59:40.287 回答