Search in sources :

Example 1 with UIParameter

use of jakarta.faces.component.UIParameter in project myfaces by apache.

the class HtmlLinkRendererBase method addChildParametersToHref.

private void addChildParametersToHref(FacesContext facesContext, UIComponent linkComponent, StringBuilder hrefBuf, boolean firstParameter, String charEncoding) throws IOException {
    List<UIParameter> validParams = HtmlRendererUtils.getValidUIParameterChildren(facesContext, getChildren(linkComponent), false, false);
    for (int i = 0, size = validParams.size(); i < size; i++) {
        UIParameter param = validParams.get(i);
        String name = param.getName();
        Object value = param.getValue();
        addParameterToHref(name, value, hrefBuf, firstParameter, charEncoding, myfacesConfig.isStrictXhtmlLinks());
        firstParameter = false;
    }
}
Also used : UIParameter(jakarta.faces.component.UIParameter) ClientBehaviorHint(jakarta.faces.component.behavior.ClientBehaviorHint)

Example 2 with UIParameter

use of jakarta.faces.component.UIParameter in project myfaces by apache.

the class HtmlLinkRendererTest method testDisabledUIParameterNotRenderedOutputLink.

/**
 * If the disable attribute of a child UIParameter is true,
 * he should be ignored.
 * @throws Exception
 */
public void testDisabledUIParameterNotRenderedOutputLink() throws Exception {
    UIParameter param1 = new UIParameter();
    param1.setName("param1");
    param1.setValue("value1");
    param1.setDisable(true);
    UIParameter param2 = new UIParameter();
    param2.setName("param2");
    param2.setValue("value2");
    outputLink.getChildren().add(param1);
    outputLink.getChildren().add(param2);
    outputLink.encodeAll(facesContext);
    String output = writer.getWriter().toString();
    Assert.assertFalse(output.contains("param1"));
    Assert.assertFalse(output.contains("value1"));
    Assert.assertTrue(output.contains("param2"));
    Assert.assertTrue(output.contains("value2"));
}
Also used : UIParameter(jakarta.faces.component.UIParameter)

Example 3 with UIParameter

use of jakarta.faces.component.UIParameter in project myfaces by apache.

the class HtmlButtonRendererTest method testCommandButtonRendersNotDisabledUIParameters.

/**
 * If a h:commandButton has any UIParameter children, he should
 * render them with a renderer of family jakarta.faces.Input and
 * renderer type jakarta.faces.Hidden.
 * If the disable attribute of a child UIParameter is true,
 * he should be ignored.
 * @throws Exception
 */
public void testCommandButtonRendersNotDisabledUIParameters() throws Exception {
    UIParameter param1 = new UIParameter();
    param1.setName("param1");
    param1.setValue("value1");
    param1.setDisable(true);
    UIParameter param2 = new UIParameter();
    param2.setName("param2");
    param2.setValue("value2");
    commandButton.getChildren().add(param1);
    commandButton.getChildren().add(param2);
    commandButton.setValue("commandButton");
    commandButton.encodeAll(facesContext);
    String output = writer.getWriter().toString();
    Assert.assertFalse(output.contains("param1"));
    Assert.assertFalse(output.contains("value1"));
    Assert.assertTrue(output.contains("param2"));
    Assert.assertTrue(output.contains("value2"));
}
Also used : UIParameter(jakarta.faces.component.UIParameter)

Example 4 with UIParameter

use of jakarta.faces.component.UIParameter in project myfaces by apache.

the class HtmlButtonRendererBase method addChildParameters.

private StringBuilder addChildParameters(FacesContext context, List<UIParameter> validParams) {
    // add child parameters
    StringBuilder params = SharedStringBuilder.get(context, SB_ADD_CHILD_PARAMETERS);
    params.append('[');
    for (int i = 0, size = validParams.size(); i < size; i++) {
        UIParameter param = validParams.get(i);
        String name = param.getName();
        Object value = param.getValue();
        // UIParameter is no ValueHolder, so no conversion possible - calling .toString on value....
        // MYFACES-1832 bad charset encoding for f:param
        // if HTMLEncoder.encode is called, then
        // when is called on writer.writeAttribute, encode method
        // is called again so we have a duplicated encode call.
        // MYFACES-2726 All '\' and "'" chars must be escaped
        // because there will be inside "'" javascript quotes,
        // otherwise the value will not correctly restored when
        // the command is post.
        // String strParamValue = value != null ? value.toString() : "";
        String strParamValue = "";
        if (value != null) {
            strParamValue = value.toString();
            StringBuilder buff = null;
            for (int j = 0; j < strParamValue.length(); j++) {
                char c = strParamValue.charAt(j);
                if (c == '\'' || c == '\\') {
                    if (buff == null) {
                        buff = new StringBuilder();
                        buff.append(strParamValue.substring(0, j));
                    }
                    buff.append('\\');
                    buff.append(c);
                } else if (buff != null) {
                    buff.append(c);
                }
            }
            if (buff != null) {
                strParamValue = buff.toString();
            }
        }
        if (params.length() > 1) {
            params.append(',');
        }
        params.append("['");
        params.append(name);
        params.append("','");
        params.append(strParamValue);
        params.append("']");
    }
    params.append(']');
    return params;
}
Also used : SharedStringBuilder(org.apache.myfaces.core.api.shared.lang.SharedStringBuilder) UIParameter(jakarta.faces.component.UIParameter)

Example 5 with UIParameter

use of jakarta.faces.component.UIParameter in project myfaces by apache.

the class HtmlButtonRendererBase method encodeBegin.

@Override
public void encodeBegin(FacesContext facesContext, UIComponent uiComponent) throws IOException {
    RendererUtils.checkParamValidity(facesContext, uiComponent, UICommand.class);
    String clientId = uiComponent.getClientId(facesContext);
    ResponseWriter writer = facesContext.getResponseWriter();
    // commandButton does not need to be nested in a form since JSF 2.0
    UIForm form = ComponentUtils.findClosest(UIForm.class, uiComponent);
    boolean reset = isReset(uiComponent);
    boolean button = isButton(uiComponent);
    Map<String, List<ClientBehavior>> behaviors = null;
    if (uiComponent instanceof ClientBehaviorHolder) {
        behaviors = ((ClientBehaviorHolder) uiComponent).getClientBehaviors();
        if (!behaviors.isEmpty()) {
            ResourceUtils.renderDefaultJsfJsInlineIfNecessary(facesContext, writer);
        }
    }
    List<UIParameter> validParams = HtmlRendererUtils.getValidUIParameterChildren(facesContext, getChildren(uiComponent), false, false);
    String commandOnclick = (String) uiComponent.getAttributes().get(HTML.ONCLICK_ATTR);
    if (commandOnclick != null && (validParams != null && !validParams.isEmpty())) {
        ResourceUtils.renderDefaultJsfJsInlineIfNecessary(facesContext, writer);
    }
    writer.startElement(HTML.INPUT_ELEM, uiComponent);
    writer.writeAttribute(HTML.ID_ATTR, clientId, ComponentAttrs.ID_ATTR);
    writer.writeAttribute(HTML.NAME_ATTR, clientId, ComponentAttrs.ID_ATTR);
    String image = RendererUtils.getIconSrc(facesContext, uiComponent, ComponentAttrs.IMAGE_ATTR);
    if (image != null) {
        writer.writeAttribute(HTML.TYPE_ATTR, HTML.INPUT_TYPE_IMAGE, ComponentAttrs.TYPE_ATTR);
        writer.writeURIAttribute(HTML.SRC_ATTR, image, ComponentAttrs.IMAGE_ATTR);
    } else {
        String type = getType(uiComponent);
        if (type == null || (!reset && !button)) {
            type = HTML.INPUT_TYPE_SUBMIT;
        }
        writer.writeAttribute(HTML.TYPE_ATTR, type, ComponentAttrs.TYPE_ATTR);
        Object value = getValue(uiComponent);
        if (value != null) {
            writer.writeAttribute(HTML.VALUE_ATTR, value, ComponentAttrs.VALUE_ATTR);
        }
    }
    if (ClientBehaviorRendererUtils.hasClientBehavior(ClientBehaviorEvents.CLICK, behaviors) || ClientBehaviorRendererUtils.hasClientBehavior(ClientBehaviorEvents.ACTION, behaviors)) {
        if (!reset && !button) {
            String onClick = buildBehaviorizedOnClick(uiComponent, behaviors, facesContext, writer, form, validParams);
            if (onClick.length() != 0) {
                writer.writeAttribute(HTML.ONCLICK_ATTR, onClick, null);
            }
        } else {
            Collection<ClientBehaviorContext.Parameter> paramList = ClientBehaviorRendererUtils.getClientBehaviorContextParameters(HtmlRendererUtils.mapAttachedParamsToStringValues(facesContext, uiComponent));
            String onClick = ClientBehaviorRendererUtils.buildBehaviorChain(facesContext, uiComponent, ClientBehaviorEvents.CLICK, paramList, ClientBehaviorEvents.ACTION, paramList, behaviors, commandOnclick, null);
            if (onClick.length() != 0) {
                writer.writeAttribute(HTML.ONCLICK_ATTR, onClick, null);
            }
        }
        Map<String, Object> attributes = uiComponent.getAttributes();
        ClientBehaviorRendererUtils.buildBehaviorChain(facesContext, uiComponent, ClientBehaviorEvents.DBLCLICK, null, behaviors, (String) attributes.get(HTML.ONDBLCLICK_ATTR), "");
    } else {
        // fallback into the pre 2.0 code to keep backwards compatibility with libraries which rely on internals
        if (!reset && !button) {
            StringBuilder onClick = buildOnClick(uiComponent, facesContext, writer, validParams);
            if (onClick.length() != 0) {
                writer.writeAttribute(HTML.ONCLICK_ATTR, onClick.toString(), null);
            }
        } else {
            HtmlRendererUtils.renderHTMLStringAttribute(writer, uiComponent, HTML.ONCLICK_ATTR, HTML.ONCLICK_ATTR);
        }
    }
    if (isCommonPropertiesOptimizationEnabled(facesContext)) {
        CommonHtmlAttributesUtil.renderButtonPassthroughPropertiesWithoutDisabledAndEvents(writer, CommonHtmlAttributesUtil.getMarkedAttributes(uiComponent), uiComponent);
    } else {
        HtmlRendererUtils.renderHTMLAttributes(writer, uiComponent, HTML.BUTTON_PASSTHROUGH_ATTRIBUTES_WITHOUT_DISABLED_AND_EVENTS);
    }
    if (behaviors != null && !behaviors.isEmpty()) {
        HtmlRendererUtils.renderBehaviorizedEventHandlersWithoutOnclick(facesContext, writer, uiComponent, behaviors);
        HtmlRendererUtils.renderBehaviorizedFieldEventHandlers(facesContext, writer, uiComponent, behaviors);
    } else {
        if (isCommonPropertiesOptimizationEnabled(facesContext)) {
            long commonAttributesMarked = CommonHtmlAttributesUtil.getMarkedAttributes(uiComponent);
            CommonHtmlAttributesUtil.renderEventPropertiesWithoutOnclick(writer, commonAttributesMarked, uiComponent);
            CommonHtmlAttributesUtil.renderCommonFieldEventProperties(writer, commonAttributesMarked, uiComponent);
        } else {
            HtmlRendererUtils.renderHTMLAttributes(writer, uiComponent, HTML.EVENT_HANDLER_ATTRIBUTES_WITHOUT_ONCLICK);
            HtmlRendererUtils.renderHTMLAttributes(writer, uiComponent, HTML.COMMON_FIELD_EVENT_ATTRIBUTES);
        }
    }
    if (isDisabled(facesContext, uiComponent)) {
        writer.writeAttribute(HTML.DISABLED_ATTR, HTML.DISABLED_ATTR, ComponentAttrs.DISABLED_ATTR);
    }
    if (isReadonly(facesContext, uiComponent)) {
        writer.writeAttribute(HTML.READONLY_ATTR, HTML.READONLY_ATTR, ComponentAttrs.READONLY_ATTR);
    }
}
Also used : UIParameter(jakarta.faces.component.UIParameter) SharedStringBuilder(org.apache.myfaces.core.api.shared.lang.SharedStringBuilder) UIForm(jakarta.faces.component.UIForm) ClientBehaviorHolder(jakarta.faces.component.behavior.ClientBehaviorHolder) ResponseWriter(jakarta.faces.context.ResponseWriter) UIParameter(jakarta.faces.component.UIParameter) List(java.util.List)

Aggregations

UIParameter (jakarta.faces.component.UIParameter)29 UIComponent (jakarta.faces.component.UIComponent)8 SharedStringBuilder (org.apache.myfaces.core.api.shared.lang.SharedStringBuilder)5 ResponseWriter (jakarta.faces.context.ResponseWriter)3 PrintWriter (java.io.PrintWriter)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 ViewHandler (jakarta.faces.application.ViewHandler)2 ClientBehaviorContext (jakarta.faces.component.behavior.ClientBehaviorContext)2 ClientBehaviorHint (jakarta.faces.component.behavior.ClientBehaviorHint)2 HtmlCommandScript (jakarta.faces.component.html.HtmlCommandScript)2 MessageFormat (java.text.MessageFormat)2 MessageUtils.getExceptionMessageString (com.sun.faces.util.MessageUtils.getExceptionMessageString)1 FacesException (jakarta.faces.FacesException)1 ConfigurableNavigationHandler (jakarta.faces.application.ConfigurableNavigationHandler)1 NavigationCase (jakarta.faces.application.NavigationCase)1 NavigationHandler (jakarta.faces.application.NavigationHandler)1 UIForm (jakarta.faces.component.UIForm)1 ClientBehaviorHolder (jakarta.faces.component.behavior.ClientBehaviorHolder)1 SearchExpressionContext (jakarta.faces.component.search.SearchExpressionContext)1