Force JSF to process, validate and update readonly

2019-01-02 21:20发布

I am creating a calculator where a user can do the various operations by clicking on virtual numpad. Now I want to restrict the user to only select the various characters by clicking on the buttons and s/he should not be able to enter the values using keyboard. I tried using readonly="true" but it is neither validating the input nor setting the values in the bean when we click on any button.

Do we have any such restrictions available in jsf?

enter image description here

3条回答
栀子花@的思念
2楼-- · 2019-01-02 21:44

I found another workaround:

function stopEvent(event){
    event.stopPropagation();
    event.preventDefault();
}

onkeyup="stopEvent(event)" onkeydown="stopEvent(event)" onkeypress="stopEvent(event)"
查看更多
柔情千种
3楼-- · 2019-01-02 21:55

That's the effect of JSF's safeguard against tampered/attacked requests wherein the hacker attempts to circumvent the readonly (and disabled) attribute by manipulating the HTML DOM and/or the HTTP request.

Instead of

<x:inputXxx ... readonly="true">

use

<x:inputXxx ... readonly="#{facesContext.currentPhaseId.ordinal eq 6}">

or

<x:inputXxx ... readonly="#{not facesContext.postback or facesContext.renderResponse}">

This makes sure that readonly is only effective during render response phase and not during all other JSF phases. So, when JSF is about to decode the input component during the apply request values phase, it will consider readonly="false" this way.

See also:

查看更多
后来的你喜欢了谁
4楼-- · 2019-01-02 21:57

The key codes for the top row of numbers are 48-57 so you could write a function to disable keypress on those characters.

Something like this

function disableEnter(event) {
    var charCode = (event.which) ? event.which : event.keyCode;
    if (charCode > 31  && (charCode > 47 || charCode < 58)) {
        return false;}
    } else return true;
}

<h:inputText onkeypress="return disableEnter(event);" />
查看更多
登录 后发表回答