2016-11-24 55 views
-1

我正在使用jsf primefaces進行兩步驗證,第一步用戶輸入驗證碼,下一步用戶必須輸入8位密碼。我想隨機地隱藏密碼字段。例如,如果用戶密碼爲12345678,系統應要求輸入密碼的第2,3,6和7個字符,其他字段禁用。字段將隨機禁用。代碼下面給出以及連接屏幕我需要在primefaces中隨機隱藏密碼輸入字段jsf

<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

+0

你的問題是什麼? – Holger

+0

我的問題是如何隨機密碼每次禁用4個字段。 –

+0

有什麼關於' Holger

回答

0

如果我的理解corectly,要渲染的<p:password組件右x量?那麼也許你可以調用隨機返回一個布爾值的方法:

的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"> ... 
+0

感謝您的回答。但我認爲你無法理解。我已經修復了8個

+0

@ T.Hassan:您有一個奇怪的功能需求 – Kukeltje

+0

我的意思是很難理解爲什麼yu會想要這個。解決方案是基本的 – Kukeltje

0

豆類

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轉換爲字符串,並比較其密碼。