我需要在primefaces中随机隐藏密码输入字段jsf

时间:2016-11-24 05:42:30

标签: jsf primefaces

我正在使用jsf primefaces进行两步验证,第一步用户输入验证码,下一步用户必须输入8位密码。我想随机隐藏密码字段。例如,如果用户密码是12345678,系统应该要求输入密码的第2,第3,第6和第7个字符,并禁用其他字段。字段将随机禁用。代码如下以及附加屏幕

<ui:repeat value="#{logBean.passFields}" varStatus="loop">
  <p:password styleClass="Box key1" size="1" maxlength="1" disabled="false" id="password${loop.index}" value="#{logBean.passFields[loop.index]}">
 </p:password>
</ui:repeat>

Password Screen

2 个答案:

答案 0 :(得分:0)

如果我理解为corectly,你想要渲染x量的<p:password组件吗?那么也许你可以调用一个随机返回布尔值的方法:

facelet:

<c:forEach begin="0" end="20" var="i">
  #{i}: <h:inputSecret rendered="#{indexBean.randomRender()}" />
 <br />
</c:forEach>

豆:

public boolean randomRender(){
    int ran = (int)(Math.random()*9999);                
    return ran%2 != 0;
}

您可以使用自己的方式循环访问某些集合,而不是使用c:foreach

<ui:repeat value="#{logBean.passFields}" varStatus="loop"> ...

答案 1 :(得分:0)

Bean类

private String password = null;
private int guessCtr = 4;
private String[] passFields;
private String[] newpassFields;

public LogBean(){
    password = "12345678";
    //create your password array
    passFields = password.split("");        
    //if using java 8. remove this line and use the passFields instead.
    //this is just a workaround to remove the first empty element result of .split()
    newpassFields = Arrays.copyOfRange(passFields, 1, passFields.length);

    //create an array of int index
    int[] index = new int[newpassFields.length];
    for(int i=0; i<index.length; i++){
        index[i] = i;
    }
    //store the indexes in a list for the shuffle method.
    //you can use any other method to do your randomization here
    List<Integer> list = new ArrayList<Integer>();
    for (int i : index) {
        list.add(i);
    }
    Collections.shuffle(list);
    //use sublist here to get only the first 4(guessCtr) elements
    list = list.subList(list.size()-(list.size()-guessCtr), list.size());

    //set the 4 random values to '' and will also be used for the disabled logic.
    for(int i = 0; i < list.size(); i++){
        newpassFields[list.get(i)] = "";
    }
}

<h:form>
<ui:repeat value="#{logBean.newpassFields}" varStatus="loop">   
    <p:inputText type="password" disabled="#{logBean.newpassFields[loop.index] != ''}" value="#{logBean.newpassFields[loop.index]}" />
</ui:repeat>
<h:commandButton value="submit" actionListener="#{logBean.submit}"></h:commandButton>
</h:form>

您的提交现在应该只是将newpassFields转换为String并将其与密码进行比较。