简体   繁体   English

JSF转换前保存输入值

[英]JSF Save value of input before conversion

I have a input field where user enter their social security number(userid). 我有一个输入字段,用户可以在其中输入其社会保险号(userid)。 This number has to be in a certain format so I use a custom converter to format it correctly. 该数字必须采用某种格式,因此我使用自定义转换器将其正确格式化。 Later the number is checked against a DB. 之后,将根据数据库检查该编号。 When the check fails, I want the number to be displayed the way the user entered it for UX reasons. 当检查失败时,出于用户体验的原因,我希望以用户输入的方式显示该数字。 But the conversion is before the check and the userid in the backing bean is set to the converted value, the original number is lost. 但是转换是在检查之前进行的,并且将支持bean中的userid设置为转换后的值,原始编号丢失了。 What is the best way to save the original value? 保存原始价值的最佳方法是什么?

<h:inputText id="userId" value="#{bean.userId}">
    <f:converter converterId="IdConverter" />
</h:inputText>

If I understand your problem correctly then you do not need to save the original value. 如果我正确理解了您的问题,则无需保存原始值。 Take advantage of the JSF lifecycle. 利用JSF生命周期。

In addition to a custom converter, you would also need a custom validator. 除了自定义转换器外,您还需要一个自定义验证器。 In your validator, if the check between the converted input and the data on the db is successful then the getAsString of your custom converter will simply return the converted input. 在您的验证器中,如果转换后的输入与db上的数据之间的检查成功,则自定义转换器的getAsString将仅返回转换后的输入。 However, if conversion succeeds but validation fails (meaning the check against the db record is unsuccessful) then you will simply throw a ValidatorException . 但是,如果转换成功但验证失败(意味着对db记录的检查不成功),那么您将只抛出ValidatorException The getAsString will not get called and the raw input will be displayed. 不会调用getAsString并且将显示原始输入。

There are two ways that I can think of on how you can achieve this. 关于实现该目标的方式,我有两种思考方式。 For the first approach, you can define a validator method within your bean and move your validation logic in that method. 对于第一种方法,您可以在bean中定义一个验证器方法,然后在该方法中移动验证逻辑。 Example : 范例:

public class Bean {    
    //Remainder omitted

    public void validate(FacesContext fc, UIComponent uic, Object o) {
        //Get the converted input. Assuming of type String
         String convertedInput = (String) o; 

        //Move the db check in this method. If it fails simply throw 
        //a ValidatorException like below. If it succeeds, don't return anything

        //throw new ValidatorException(new FacesMessage("Validation Failed")); 
    }
} 

Be mindful of the return type and the parameters. 注意返回类型和参数。

Then you would add the validator attribute in the <h:inputText> like so 然后,您可以像这样在<h:inputText>添加validator属性

<h:inputText id="userId" value="#{bean.userId}" validator="#{bean.validate}">

One advantage with this approach is that this method will be able to access other instance variables of the Bean class but as a downside, it is less portable. 这种方法的一个优点是该方法将能够访问Bean类的其他实例变量,但缺点是它的可移植性较差。

Another approach would be to have a separate class implement the Validator interface. 另一种方法是让一个单独的类实现Validator接口。 Example: 例:

@FacesValidator("customValidator")
public class MyValidator implements javax.faces.validator.Validator {

    @Override
    public void validate(FacesContext fc, UIComponent uic, Object o) {
        //Same logic as bean validator method
    }
}

Then add in your <h:inputText> 然后添加您的<h:inputText>

<f:validator validatorId="customValidator" />

You problem seems to indicate a lack of understanding of the JSF Lifecycle. 您的问题似乎表明对JSF生命周期缺乏了解。 I would highly suggest that you take the time to understand this concept as much as possible. 我强烈建议您花时间尽可能多地理解这个概念。 You might not understand it in one sitting but as you get more involved with JSF, some aspects will become clearer. 您可能不会一口气理解它,但是随着您更多地参与JSF,某些方面将变得更加清晰。 Here's a good place to start. 这是一个不错的起点。

Debug JSF lifecycle 调试JSF生命周期

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

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