Personalización básica > User Interface Customization > MVC Components > MVC Components Overview > MVC > Creating Multiple Components
  
Creating Multiple Components
There will be requirement to build multiple components using the same builder and can have two use cases.
Use Case 1
The components are made from an already existing components/builders. For example if there is already a component with id="test.ComponentA" and another with id="test.ComponentB" and we want to get both the components in a page.
@ComponentBuilder(value="test.multi.AB", type=ComponentBuilderType.CONFIG_ONLY)
public class MultiComponentBuilder extends AbstractComponentConfigBuilder{
@Override
public ComponentConfig buildComponentConfig(ComponentParams params)
throws WTException {
MultiComponentConfig configs = new MultiComponentConfig();
configs.addNestedComponent("test.ComponentA");
//you are specifying the componentId
configs.addNestedComponent("test.ComponentB");
configs.setView("/test/multi.jsp");
return config ;
}
}
There is no need for ComponentDataBuilder as this multiComponent by itself doesn't have any data.
You need to specify a view jsp for this ComponentConfig as no default view can handle that.
When you use addNestedComponent to provide a componentId, make sure that componentIds are to be handled by the same controller instance. In other words, you can have only non-type based or type based components combined together with this approach.
Use Case 2
Here the components need to be defined and they exist together. E.g. you want property panel coming with a table.
There is a need for ComponentDataBuilder.
Infrastructure invokes the buildComponentData for each ComponentConfig available in the MultiComponentConfig. It’s invoked in the same order it has been put in the MultiComponentConfig.
You can access the rawData for the ComponentConfig that has already been processed from the ComponentParams supplying the componentId
You need to specify a view jsp for this ComponentConfig as no default view can handle that.
@ComponentBuilder("test.array.PQ")
public class ComponentArrayBuilder extends AbstractComponentBuilder {

@Override
public ComponentConfig buildComponentConfig(ComponentParams params)
throws WTException {
MultiComponentConfig configs = new MultiComponentConfig();

TableConfig table1 = createTable1();
//create a table with id = component1
......
TableConfig table2 = createTable2();
//create a table with id = component2
......
configs.addComponent(table1);
//add the components
configs.addComponent(table2);

configs.setView("/test/array.jsp");
return configs ;
}

@Override
public Object buildComponentData(ComponentConfig config,
ComponentParams params) throws WTException {

if ("component1".equals(config.getId())) {
//return data for table with id= component1
} else {
//return data for table with id= component2
}
}
}
MVC