Perhaps a better way is to extend the <h:inputText>
, create your own component that is pretty much the same as <h:inputText>
but that trimmes the result by default.
In my opinion though, there should be an attribute in inputText that trimmed by default ie:
<h:inputText value="#{myBean.text}" trim="true"/>
Update:
Ok, so here is how you can create a component that trim's the inputText fields.
Note, however that I haven't tested the code, so I am not 100% sure it will work, but it should.
In faces-config.xml
Add your component
<component>
<component-type>foo.InputControlComponent</component-type>
<component-class>my.package.foo.InputControl</component-class>
</component>
Create WEB-INF/foo.taglib.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE facelet-taglib PUBLIC
"-//Sun Microsystems, Inc.//DTD Facelet Taglib 1.0//EN"
"http://java.sun.com/dtd/facelet-taglib_1_0.dtd">
<facelet-taglib>
<namespace>http://whatever.com/foo</namespace>
<tag>
<tag-name>inputControl</tag-name>
<component>
<component-type>foo.InputControlComponent</component-type>
</component>
</tag>
</facelet-taglib>
In web.xml
<context-param>
<param-name>facelets.LIBRARIES</param-name>
<param-value>/WEB-INF/foo.taglib.xml</param-value>
</context-param>
InputControl.java
public class InputControl extends UIPanel {
public InputControl() {
super();
}
private void childrenEncodeBegin(FacesContext context, List<UIComponent> children) {
for (UIComponent comp : children) {
if (comp instanceof UIInput) {
comp = (UIInput) comp;
((UIInput) comp).setValue(((UIInput) comp).getValue().toString().trim());
}
// Encode recursively
if (comp.isRendered() && comp.getChildCount() > 0)
childrenEncodeBegin(context, comp.getChildren());
}
}
public void encodeBegin(FacesContext context) throws IOException {
if (getChildren() != null)
childrenEncodeBegin(context, getChildren());
}
}
Now in your xhtml you can use it like this:
<foo:inputControl>
<ui:include src="myForm.xhtml"/>
</foo:inputControl>