开发者

Retrieve values from JSP to Javascript

开发者 https://www.devze.com 2023-03-28 11:09 出处:网络
Below is the javascript on my JSP page: <SCRIPT language=\"JavaScript\"> function checkPass(){ var pass1=request.getParameter(\'password\');

Below is the javascript on my JSP page:

<SCRIPT language="JavaScript">
function checkPass(){

var pass1=request.getParameter('password');
var pass2=request.getParameter('password2');

    if (pass1==pass2){
        window.alert("YES");
    }
    else{
        window.alert("NO");
    }
    }
</SCRIPT>

Below is my form input text boxes:

                    <h:outputLabel value="Password:" for="password" />
 <h:inputSecret id="password" value="#{StaffController.staff.password}" 
            size="20" required="true"
            label="Password" >
            <f:validateLength minimum="8" />
        </h:inputSecret>

        <h:message for="password" style="co开发者_StackOverflow中文版lor:red" />

                    <h:outputLabel value="Password:" for="password2" />
 <h:inputSecret id="password2" 
            size="20" required="true"
            label="Password" >
            <f:validateLength minimum="8" />
        </h:inputSecret>
 <h:message for="password2" style="color:red" />

Below is my commandbutton linked with onmousedown to call the script when clicked:

<h:commandButton action="#{StaffController.saveUser()}" onmousedown="checkPass();" value="Submit"/>

I can't seem to retrieve value from my user input form.


  1. JSF changes id of components - view source of your page in browser. It should be something like a combination of form id+component id

  2. Submit command invokes onmousedown event before making submit, so in checkPass you do not have a request object

  3. Better to write a custom validator for checking password


First you require to set in the h:form the value false to the "prependId" attribute to make possible to get the textboxes by id on javascript. Is not recomended to remove the prependId. But it makes to you easier to work with javascript. In other way you could use a field name or css name an other kind of selection (like using Jquery $('.cssname') or $('#cssid')

Now you need to change the javascript. javascript is performed on the client side. You can't get data from the request because the request is not done when the javascript is executed.

You need to get the inputSecret object ,that in the html is an input item, using something like this:

var pass1 = document.getById('password').value;
var pass2 = document.getById('password2').value;
0

精彩评论

暂无评论...
验证码 换一张
取 消