Prepare Interview

Mock Exams

Make Homepage

Bookmark this page

Subscribe Email Address

Apache%20Tapestry%20Interview%20Questions%20and%20Answers

Question: How do we write components in Apache Tapestry?
Answer:
Retrieving bound properties: When writting a component, you often require various properties to be supplied by the component user. At some point during rendering, you will want to use the value of this property.
You can do this by accessing the Binding. Assume we have a component with one property called 'values'. Our component will use this list in its preRenderCommponent() method to setup some model, for use elsewhere.

.... if(getValues() == null) {
  IBinding binding = getBindings("values"); if(binding.getObject() == null) {
      throw new RequestCycleException("The value for 'values' cannot be null", this);
  } setValues((List)values.getObject());
}

The binding itself will ensure that the object value is the correct type (assuming of course, it's been setup right).
Performing Lazy Instantiation of state based upon component properties: In some cases, the output of a component may be based upon the state of some other property of the same component. For example, imagine a form where the user can choose the type of product to view. When they choose a product, the form makes a query to the database for products matching this type, and reshows the list on the same page. (the list would be included outside of the form element itself).
Lets assume that the page object exposes it's products via a getProductModel(), which is an instance of IPropertySelectionModel.
We will also assume that the remainder of the page has the other components necessary to render correct HTML, using the value supplied by the getProductModel() result.
Here, it is helpful to know when in the rendering process you can rely on the value of selectedProduct to be set correctly, so that you can instantiate a ProductModel based on the provided value, for use in the form rendering. The best place to setup state is in the preRenderComponent() method. This is called by Tapestry just before it renders any component, but AFTER it has set component properties. So, we might write:

protected void preRenderComponent() {
  String selected = getSelectedProduct();
  List products = getMatchingProducts(selectedProduct);
  productModel = new ProductModel(products);
  .. other initialization code ...
Is it helpful? Yes No

Most helpful rated by users:

©2024 WithoutBook