Corrective Entries in JBoss Seam

I am building a web application using JBoss Seam 2.2.0, and I want to trim my inputs before receiving them, even before the Hibernate Bean verification phase. Is it possible?

I saw someone using a PhaseListener to perform the same function. Is this the best way to do this?

+1
java java-ee jsf jboss seam
source share
3 answers

I use a converter for this. It works very well.

Page:

 <h:inputText value="#{myBean.myValue}" converter="#{stringTrimConverter}"/> 

the code:

 @Name("stringTrimConverter") @BypassInterceptors @Converter public class StringTrimConverter implements javax.faces.convert.Converter { public Object getAsObject(FacesContext context, UIComponent cmp, String value) { if(StringUtils.isBlank(value)) { return null; } else { return value; } } public String getAsString(FacesContext context, UIComponent cmp, Object value) { if(value != null) { return value.toString().trim(); } return null; } } 
+9
source share

One suggestion is to trim the text in Javascript as soon as the user changes the input value:

 <h:inputText ... onchange="this.value = trim(this.value);"/> 

with javascript function:

 function trim(myString) { return myString.replace(/^\s+/g,'').replace(/\s+$/g,''); } 

<h / "> Change your comment:

My solution is the best way to do this because it is done on the client side. However, as you said in the comment, this will not work if the client browser does not allow Javascript. As shown here , 95% of users activate Javascript in their browsers (and this was in January 2008!).

However, if you really need to support any Javascript browser, I suggest you really implement the PhaseListener solution.

<h / "> Edit2

The solution proposed by Damo with Convertor also works, but you will need to specify a converter for each input that is not needed with PhaseListener , that is, you always need to add converter="#{stringTrimConverter}" for all your inputs.

+2
source share

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()); } // 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> 
+2
source share

All Articles