target.addComponent(myComponent)

Be careful that when target.addComponent(myComponent) that myComponent.setOutputMarkupId(true) has been set. The ID is required in order for an update can be made on the client.

Adding Views To Targets

You may be tempted to add a view (ListView, RefreshingView, DataView, etc.) to an AjaxRequestTarget only to to find out that you receive an exception indicating that the view cannot be added to the target. Here are a few tips when adding an updated views to targets:

  1. Do not add the view to a target. Use a parent component instead.
  2. If the view is within a Panel do not add the panel to the target. Use a WebMarkupContainer (or similar component) instead.

Below is an example of a view using a WebMarkupContainer to target a RefreshingView. When a WebPage using the panel calls the addRatingScaleComponents(final AjaxRequestTarget target) method the ratingScaleContainer will be added to the target (not the panel):

AbstractRatingScalePanel.java
public abstract class AbstractRatingScalePanel extends Panel {

	private final WebMarkupContainer ratingScaleContainer;
	private final RefreshingView ratingScalesView;

	/**
	 * Creates a rating scale creation/display panel
	 * 
	 * @param id
	 *            the ID of the panel
	 * @param headerLabelModel
	 *            the model that will be used for the header
	 */
	public AbstractRatingScalePanel(final String id, final IModel headerLabelModel) {
		super(id);
		add(new Label("legend-scale-header", headerLabelModel));
		ratingScaleContainer = new WebMarkupContainer("div-rating-scale-container");
		// add the initial list of rating scales
		ratingScalesView = new RefreshingView("tr-rating-scale") {
			private static final long serialVersionUID = 1L;

			/**
			 * {@inheritDoc}
			 */
			@Override
			protected final void populateItem(final Item item) {// WicketAjaxIndicatorAppender IndicatingAjaxLink
				// add the rating scale components
				item.add(new TextField("boundary"));
				item.add(new Label("rating"));
			}

			/**
			 * {@inheritDoc}
			 */
			@SuppressWarnings("unchecked")
			@Override
			protected Iterator<RatingScale> getItemModels() {
				// load the rating scales 
				return new ModelIteratorAdapter(loadRatingScales().iterator()) {
					/**
					 * {@inheritDoc}
					 */
					@Override
					protected final IModel model(final Object object) {
						return new CompoundPropertyModel((RatingScale) object);
					}
				};
			}
		};
		// surround the view with a markup container
		ratingScaleContainer.add(ratingScalesView);
		add(ratingScaleContainer.setOutputMarkupId(true));
	}

	/**
	 * Adds the rating scale component(s) to the specified target
	 * 
	 * @param target
	 *            the target to add the component(s) to
	 */
	public final void addRatingScaleComponents(final AjaxRequestTarget target) {
		target.addComponent(ratingScaleContainer);
	}

	/**
	 * Loads/returns the rating scales used by the panel
	 * 
	 * @return the rating scales used by the panel
	 */
	protected abstract List<RatingScale> loadRatingScales();
}
AbstractRatingScalePanel.html
<?xml version="1.0" encoding="UTF-8"?>
<html xmlns="http://www.w3.org/1999/xhtml"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xmlns:wicket="http://wicket.apache.org"
	xsi:schemaLocation="http://www.w3.org/MarkUp/SCHEMA/xhtml11.xsd http://wicket.apache.org"
	xml:lang="en">
	<body>
		<wicket:panel>
			<fieldset>
				<legend wicket:id="legend-scale-header">
					[4. Determine On What Scale You Will Be Scored]
				</legend>
				<div wicket:id="div-rating-scale-container">
					<table border="0">
						<tr wicket:id="tr-rating-scale">
							<td style="text-align:center;">
								<input wicket:id="boundary" type="text" size="10" />
							</td>
							<td>
								=
							</td>
							<td>
								<span wicket:id="rating">
									[Rating Value]
								</span>
							</td>
						</tr>
					</table>
				</div>
			</fieldset>
		</wicket:panel>
	</body>
</html>
  • No labels