Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8806213
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T02:04:33+00:00 2026-06-14T02:04:33+00:00

We have many XHTML pages, each page has some <h:inputText> , <h:inputSecret> , etc..

  • 0

We have many XHTML pages, each page has some <h:inputText>, <h:inputSecret>, etc.. input components.

I am trying to find out a generic way to trim all those values before form submission.

I implemented a TrimInputTextRenderer class,

XHTML page:

<h:inputText value="#{userBean.emailAddress}" trim="true">
       <f:validator validatorId="emailAddressValidator"/>
</h:inputText>

TrimInputTextRenderer.java:

package com.vyan.web.component;

import java.io.IOException;
import java.util.List;

import javax.faces.component.UIComponent;
import javax.faces.component.UIInput;
import javax.faces.component.UIOutput;
import javax.faces.context.FacesContext;
import javax.faces.render.FacesRenderer;

import com.sun.faces.renderkit.html_basic.TextRenderer;

@FacesRenderer(componentFamily="javax.faces.Input", rendererType="javax.faces.Text")
public class TrimInputTextRenderer extends TextRenderer {

    private void encodeBeginForChildren(FacesContext context, UIComponent component) {
            if (component instanceof UIInput) {
                component = (UIInput)component;
                String componentValue = ((UIOutput)component).getValue().toString();
                componentValue = (componentValue!=null)?componentValue.trim():componentValue;
                ((UIInput)component).setValue(componentValue);
            }

            // Encoding recursively all childrens.
            if (component.isRendered() && component.getChildCount() > 0)
                encodeBeginForChildrens(context, component.getChildren());

    }

    private void encodeBeginForChildrens(FacesContext context, List<UIComponent> children) {
        for (UIComponent comp : children) {
            if (comp instanceof UIInput) {
                comp = (UIInput) comp;
                ((UIInput) comp).setValue(((UIInput) comp).getValue()
                        .toString().trim());
            }

                    // Encoding recursively all childrens.
            if (comp.isRendered() && comp.getChildCount() > 0)
                encodeBeginForChildrens(context, comp.getChildren());
        }

    }

    @Override
    public void encodeBegin(FacesContext context, UIComponent component) throws IOException {
        String trimValue = (String) component.getAttributes().get("trim");
        if (trimValue!= null && Boolean.valueOf(trimValue)) {
            //super.write(string);
            encodeBeginForChildren(context, component);
        } 
        super.encodeBegin(context, component);
    }

}

faces-config.xml:

<render-kit>
        <renderer>
            <component-family>javax.faces.Input</component-family>
            <renderer-type>javax.faces.Text</renderer-type>
            <renderer-class>com.vyan.web.component.TrimInputTextRenderer</renderer-class>
        </renderer>
    </render-kit>

The problem is, everything works fine, the custom renderer trims the values.
But we need the component values in validator & managed bean.
Those were getting executed before the method TrimInputTextRenderer#encodeBegin() method call.
So in validator & managed bean, we are getting non-trimmed values.
Is there a way it can be executed before validation phase,
or do i need to implement this logic some where?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T02:04:35+00:00Added an answer on June 14, 2026 at 2:04 am

    This logic needs to be implemented in the decode() method or, easier, the setSubmittedValue().

    @Override
    public void setSubmittedValue(UIComponent component, Object value) {
        if (value instanceof String) {
            value = ((String) value).trim();
        }
    
        super.setSubmittedValue(component, value);
    }
    

    The easier and non-implementation-dependent alternative to this Mojarra-specific renderer extension was been a @FacesConverter(forClass=String), by the way.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have many MKMapViews, and each of them has an annotation. I am trying
I have this button in header that I include in many page: <input type=button
Am having many xhtml pages in my application. First page that a User gets
We have many projects with several files inside each. Files can be checked in
I have many text files containing bank transactions in the middle of them. Each
I have a little web project where I have many pages and an index/ToC
I have a table that displays pictures in each td element. The page is
In a set of HTML pages that have some important text that requires a
XHTML is still a very poor language and you have to try for many
I have read many of the answers and pages but I cannot seem to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.