
org.primefaces.component.inputtext.InputTextRenderer Maven / Gradle / Ivy
/*
* The MIT License
*
* Copyright (c) 2009-2025 PrimeTek Informatics
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package org.primefaces.component.inputtext;
import org.primefaces.expression.SearchExpressionUtils;
import org.primefaces.renderkit.InputRenderer;
import org.primefaces.util.ComponentUtils;
import org.primefaces.util.HTML;
import org.primefaces.util.LangUtils;
import org.primefaces.util.WidgetBuilder;
import java.io.IOException;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.context.ResponseWriter;
public class InputTextRenderer extends InputRenderer {
@Override
public void decode(FacesContext context, UIComponent component) {
InputText inputText = (InputText) component;
if (!shouldDecode(inputText)) {
return;
}
decodeBehaviors(context, inputText);
String clientId = inputText.getClientId(context);
String submittedValue = context.getExternalContext().getRequestParameterMap().get(clientId);
if (submittedValue != null) {
int maxlength = inputText.getMaxlength();
if (maxlength > 0 && submittedValue.length() > maxlength) {
submittedValue = LangUtils.substring(submittedValue, 0, maxlength);
}
inputText.setSubmittedValue(submittedValue);
}
}
@Override
public void encodeEnd(FacesContext context, UIComponent component) throws IOException {
InputText inputText = (InputText) component;
encodeMarkup(context, inputText);
encodeScript(context, inputText);
}
protected void encodeScript(FacesContext context, InputText inputText) throws IOException {
String counter = inputText.getCounter();
WidgetBuilder wb = getWidgetBuilder(context);
wb.init("InputText", inputText)
.attr("maxlength", inputText.getMaxlength(), Integer.MIN_VALUE);
if (counter != null) {
UIComponent counterComponent = SearchExpressionUtils.contextlessResolveComponent(context, inputText, counter);
if (counterComponent != null) {
wb.attr("counter", counterComponent.getClientId(context))
.attr("counterTemplate", inputText.getCounterTemplate(), null)
.attr("countBytesAsChars", inputText.getCountBytesAsChars());
}
}
wb.finish();
}
protected void encodeMarkup(FacesContext context, InputText inputText) throws IOException {
ResponseWriter writer = context.getResponseWriter();
String clientId = inputText.getClientId(context);
writer.startElement("input", inputText);
writer.writeAttribute("id", clientId, null);
writer.writeAttribute("name", clientId, null);
writer.writeAttribute("type", inputText.getType(), null);
String valueToRender = ComponentUtils.getValueToRender(context, inputText);
if (valueToRender != null) {
writer.writeAttribute("value", valueToRender, null);
}
if (inputText.getStyle() != null) {
writer.writeAttribute("style", inputText.getStyle(), null);
}
writer.writeAttribute("class", createStyleClass(inputText, InputText.STYLE_CLASS), "styleClass");
renderAccessibilityAttributes(context, inputText);
renderRTLDirection(context, inputText);
renderPassThruAttributes(context, inputText, HTML.INPUT_TEXT_ATTRS_WITHOUT_EVENTS);
renderDomEvents(context, inputText, HTML.INPUT_TEXT_EVENTS);
renderValidationMetadata(context, inputText);
writer.endElement("input");
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy