Perhaps the best way to extend <h:inputText> is to create your own component, which is almost the same as <h:inputText> , but by default this is the result.
In my opinion, although there should be an attribute in inputText that is truncated by default, i.e.:
<h:inputText value="#{myBean.text}" trim="true"/>
Update:
So, here is how you can create a component that truncates inputText fields. Please note, however, that I have not tested the code, so I am not 100% sure that 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()); }
Now in your xhtml you can use it like this:
<foo:inputControl> <ui:include src="myForm.xhtml"/> </foo:inputControl>
Shervin asgari
source share