简体   繁体   English

整理JBoss Seam中的输入

[英]Trimming inputs in JBoss Seam

I am making a web application using JBoss Seam 2.2.0, and I want to trim my inputs before receiving them, even before the Hibernate Bean Validation phase. 我正在使用JBoss Seam 2.2.0制作一个Web应用程序,我想在接收到输入之前(甚至在Hibernate Bean Validation阶段之前)修剪输入。 Is this possible? 这可能吗?

I saw someone using a PhaseListener to do the same functionality. 我看到有人使用PhaseListener来执行相同的功能。 Is this the best way to do it? 这是最好的方法吗?

I use a Converter for this. 我为此使用转换器。 Works very well. 效果很好。

Page: 页:

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

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;
    }

}

One suggestion is to trim the text in Javascript, once the user changes the value of the input: 一种建议是,一旦用户更改了输入值,便会修剪Javascript中的文本:

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

with the Javascript function: 使用Javascript函数:

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


Edit, regarding your comment: 编辑,关于您的评论:

The solution I suggested is the best way to do that because it is done on the client side. 我建议的解决方案是最好的方法,因为它是在客户端完成的。 However, as you said in the comment, it will not work if the client's browser does not allow Javascript. 但是,正如您在评论中所说,如果客户端的浏览器不允许使用Javascript,它将无法正常工作。 As shown here , 95% of the users activate the Javascript on their browsers (and it was in January 2008 !). 如此处所示95%的用户在其浏览器上激活了Javascript(那是在2008年1 !)。

However, if you really need to support none Javascript browsers, I suggest that you indeed implement the PhaseListener solution. 但是,如果确实不需要支持Javascript浏览器,则建议您确实实现PhaseListener解决方案。


Edit2 编辑2

The solution proposed by Damo with a Convertor is also working, but you will need to specify the converter for every input, which is not needed with the PhaseListener , ie you will need to always add converter="#{stringTrimConverter}" for all of your inputs. 与提议由大摩的解决方案Convertor也在努力,但你需要指定每个输入,这不需要与转换器PhaseListener ,即你需要随时添加converter="#{stringTrimConverter}"所有的您的输入。

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. 也许更好的方法是扩展<h:inputText> ,创建自己的组件,该组件与<h:inputText>几乎相同,但是默认情况下会修剪结果。

In my opinion though, there should be an attribute in inputText that trimmed by default ie: 但是我认为,inputText中应该有一个默认修剪的属性,即:

<h:inputText value="#{myBean.text}" trim="true"/>

Update: 更新:

Ok, so here is how you can create a component that trim's the inputText fields. 好的,这就是您如何创建修剪inputText字段的组件的方法。 Note, however that I haven't tested the code, so I am not 100% sure it will work, but it should. 请注意,但是我还没有测试代码,所以我不是100%确信它会工作,但是应该可以。

In faces-config.xml 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 创建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 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: 现在,您可以在xhtml中像这样使用它:

<foo:inputControl>
  <ui:include src="myForm.xhtml"/>
</foo:inputControl> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM