2012-05-23 25 views
6

Tôi đã sau hai POJO đơn giản:Không truy cập đến tài sản lồng trong bean được quản lý trong phạm vi p: cột

class Person { 
    String name 
    Address address; 
    //and of course the getter/setter for the attributes 
} 

class Address { 
    String city; 
    //also getter/setter for this attribute 
} 

Và một bean ủng hộ:

@ManagedBean 
@RequestScoped 
class PersonController { 

    private List persons; 
    private List<String> columns = Arrays.toList("name", "address.city"); 
    //of course getter/setter 
} 

Bây giờ tôi muốn tạo một DataTable.

<p:dataTable var="person" value="#{personController.persons}" columnIndexVar="index"> 
    <p:columns var="column" value="#{personController.columns}"> 
     <h:outputText value="#{person[column]}"/> 
    <p:columms> 
</p:dataTable> 

Khi tôi thực hiện điều này tôi nhận được một ServletException:

Các Person lớp không có tài sản 'address.city'.

Nhưng nếu một cố gắng để truy cập vào thành phố bất động sản như thế này trong vòng p: cột:

<h:outputText value="#{person.address.city}"/> 

Mọi việc đã ổn.

Tại sao tôi không thể truy cập thuộc tính lồng nhau như vậy #{person['address.city']}? Và làm thế nào tôi có thể truy cập nó trong phạm vi p:columns?

+0

Thẻ '' không tồn tại. Không phải bạn có quá mức cao hơn cho thẻ '' của PrimeFaces không? – BalusC

+0

Có. Xin lỗi tôi sẽ thay đổi nó. –

Trả lời

8

Thuộc tính bean lồng nhau trong biểu thức chuỗi ký hiệu cú đúp như #{person['address.city']} theo mặc định không được hỗ trợ. Về cơ bản bạn cần một số #{person['address']['city']}.

Bạn cần tùy chỉnh ELResolver tại đây. Dễ nhất là mở rộng BeanELResolver hiện có.

Dưới đây là một ví dụ Kickoff:

public class ExtendedBeanELResolver extends BeanELResolver { 

    @Override 
    public Object getValue(ELContext context, Object base, Object property) 
     throws NullPointerException, PropertyNotFoundException, ELException 
    { 
     if (property == null || base == null || base instanceof ResourceBundle || base instanceof Map || base instanceof Collection) { 
      return null; 
     } 

     String propertyString = property.toString(); 

     if (propertyString.contains(".")) { 
      Object value = base; 

      for (String propertyPart : propertyString.split("\\.")) { 
       value = super.getValue(context, value, propertyPart); 
      } 

      return value; 
     } 
     else { 
      return super.getValue(context, base, property); 
     } 
    } 

} 

Để làm cho nó chạy, đăng ký nó như sau trong faces-config.xml:

<application> 
    <el-resolver>com.example.ExtendedBeanELResolver</el-resolver> 
</application> 
+0

Cảm ơn câu trả lời nhanh chóng của bạn. Tôi sẽ thử nó! –

+0

Nó hoạt động tốt. Cảm ơn nhiều. –

+0

Bạn được chào đón. – BalusC

1

Ngoài @BalusC câu trả lời tôi đã có thêm một tấm séc cho PrimeResourceHandler . Nếu không, tất cả các giải quyết của # {resource ...} giống như # {resource ['primefaces: spacer/dot_clear.gif']} bên trong các primefaces.css không thành công và luồng đầu ra của tệp CSS phân tích bị hỏng.

public class ExtendedBeanELResolver extends BeanELResolver { 

    private static final String PRIMEFACES_RESOURCE_PREFIX = "primefaces:"; 

    @Override 
    public Object getValue(ELContext context, Object base, Object property) throws NullPointerException, 
      PropertyNotFoundException, ELException { 
     if (property == null || base == null || base instanceof ResourceBundle || base instanceof Map 
       || base instanceof Collection || base instanceof PrimeResourceHandler) { 
      return null; 
     } 

     String propertyString = property.toString(); 

     if (!propertyString.startsWith(PRIMEFACES_RESOURCE_PREFIX) && propertyString.contains(".")) { 
      Object value = base; 

      for (String propertyPart : propertyString.split("\\.")) { 
       value = super.getValue(context, value, propertyPart); 
      } 

      return value; 
     } else { 
      return super.getValue(context, base, property); 
     } 
    } 
} 
Các vấn đề liên quan