---
title: "valueexpression的用法"
author: "feidaodalian"
date: "2009-09-16T19:52:12Z"
tags: []
slug: "valueexpression"
draft: false
lastmod: "2009-09-16T19:52:12Z"
id: 469979
url: "https://feidaodalian.iteye.com/blog/469979"
---

在1.2之前，可以向下面一样使用ValueBinding:
```java
ValueBinding vb = facesContext.getApplication().createValueBinding("#{bindings}");
DCBindingContainer dc = (DCBindingContainer) vb.getValue(facesContext);
```

但是在JSF1.2中，需要这样使用：

```java
/**
     * Method for taking a reference to a JSF binding expression and returning
     * the matching object (or creating it).
     * @param expression EL expression
     * @return Managed object
     */
    public static Object resolveExpression(String expression) {
        FacesContext facesContext = getFacesContext();
        Application app = facesContext.getApplication();
        ExpressionFactory elFactory = app.getExpressionFactory();
        ELContext elContext = facesContext.getELContext();
        ValueExpression valueExp = 
            elFactory.createValueExpression(elContext, expression, 
                                            Object.class);
        return valueExp.getValue(elContext);
    }
    /**
     * Return the current page's binding container.
     * @return the current page's binding container
     */
    public static BindingContainer getBindingContainer() {
        return (BindingContainer)resolveExpression("#{bindings}");
    }
```