/** * Override handleRequest in order to perform processing for this component. This implementation checks whether the * link has been pressed via the current ajax operation. * * @param request the request being responded to. */ @Override public void handleRequest(final Request request) { // Check if this link was the AJAX Trigger AjaxOperation operation = AjaxHelper.getCurrentOperation(); boolean pressed = (operation != null && getId().equals(operation.getTriggerId())); // Protect against client-side tampering of disabled/read-only fields. if (isDisabled() && pressed) { LOG.warn("A disabled link has been triggered. " + getText() + ". " + getId()); return; } // If an action has been supplied then execute it, but only after // handle request has been performed on the entire component tree. final Action action = getAction(); if (pressed && action != null) { final ActionEvent event = new ActionEvent(this, getActionCommand(), getActionObject()); Runnable later = new Runnable() { @Override public void run() { action.execute(event); } }; invokeLater(later); } }
/** * Override preparePaintComponent to register an AJAX operation if this link has an action. * * @param request the request being responded to. */ @Override protected void preparePaintComponent(final Request request) { super.preparePaintComponent(request); UIContext uic = UIContextHolder.getCurrent(); // If the link has an action, register it for AJAX final Action action = getAction(); final AjaxTarget[] actionTargets = getActionTargets(); if (action != null && uic.getUI() != null) { // Register AJAX operation if AJAX targets are set, otherwise rely on InternalAjax request if (actionTargets != null && actionTargets.length > 0) { List<String> targetIds = new ArrayList<>(); for (AjaxTarget target : actionTargets) { targetIds.add(target.getId()); } // Register the action targets AjaxHelper.registerComponents(targetIds, getId()); } } }
@Test public void testDoPaintWithAction() throws IOException, SAXException, XpathException { WContainer root = new WContainer(); WLink link = new WLink("test", "http://test"); WPanel target1 = new WPanel(); WPanel target2 = new WPanel(); root.add(link); root.add(target1); root.add(target2); // No Targets (target itself) link.setAction(new TestAction()); assertSchemaMatch(root); assertXpathEvaluatesTo(link.getId(), "//ui:ajaxtrigger/@triggerId", root); assertXpathEvaluatesTo("1", "count(//ui:ajaxtrigger/ui:ajaxtargetid)", root); assertXpathEvaluatesTo(link.getId(), "//ui:ajaxtrigger/ui:ajaxtargetid[1]/@targetId", root); // With Targets link.setAction(new TestAction(), target1, target2); assertSchemaMatch(root); assertXpathEvaluatesTo(link.getId(), "//ui:ajaxtrigger/@triggerId", root); assertXpathEvaluatesTo("2", "count(//ui:ajaxtrigger/ui:ajaxtargetid)", root); assertXpathEvaluatesTo(target1.getId(), "//ui:ajaxtrigger/ui:ajaxtargetid[1]/@targetId", root); assertXpathEvaluatesTo(target2.getId(), "//ui:ajaxtrigger/ui:ajaxtargetid[2]/@targetId", root); }