1

I am working on a jsf project where i have to integrate a applet for figure print verification for user. My applet is working fine and i am getting a boolean value from my applet to jsf page. At first my commandbutton in jsf form will be disable. I just want to enable my jsf button when ever my applet return true value. I am pasting my code here.

My jsf code is:

<!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<!-- <ui:composition xmlns="http://www.w3.org/1999/xhtml"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:c="http://java.sun.com/jsp/jstl/core"
    template="/layouts/BasicTemplate.xhtml"
    xmlns:h="http://java.sun.com/jsf/html">
    <ui:define name="content">
        <div> -->

<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:p="http://primefaces.org/ui">

<ui:composition template="/layouts/BasicTemplate.xhtml">
    <ui:define name="content">
        <div class="container">

        <script>

        function doAlert(s)
        {
            if(s=="true")
                {
                alert(s);
                rc();
                alert('hi i am');
                }
            else
                {
                 alert("Wrong Finger Print");
                }
        }

        </script>


          <h:form class="form-horizontal" id="myform" >
                <div class="row">
                    <!-- <p:growl id="msgs" sticky="true" showDetail="true" /> -->
            <!-- <div>
                <div>&nbsp;</div>
                <div>
                    <c:if test="${not empty param.login_error}">
                        <div>
                            Your login attempt was not successful, try again.
                            <br/>
                            Reason: #{sessionScope.SPRING_SECURITY_LAST_EXCEPTION.message}
                            <br/>
                        </div>
                    </c:if>
                    <form name="f" action="${request.contextPath}/j_spring_security_check" method="post">
                        <fieldset>
                            <h4>User Login</h4> 
                            <div>
                                <div>
                                    <span>Username:</span>
                                    <c:if test="${not empty param.login_error}">
                                        <c:set var="username" value="${sessionScope.SPRING_SECURITY_LAST_USERNAME}" />
                                    </c:if>
                                    <input type="text" name="j_username" value="#{username}" />
                                    <br/>
                                    <span class="password">Password:</span>

                                    <input type="password" name="j_password" />

                                </div>
                                <div>
                                    <div>
                                        <input name="submit" type="submit" value="Login" />
                                    </div>
                                </div>
                            </div>
                        </fieldset>
                    </form>
                </div> 
            </div> -->


        <h:messages />
        <h:panelGrid columns="3" cellpadding="4" border="0" columnClasses="control-label">

            <h:outputText  value="Username :" />
            <h:inputText id="username" value="#{loginBean.login}" label="username" style="width: 200;" /> 
            <p:watermark for="username" value="Username" ></p:watermark>

            <h:outputLabel for="password" value="Password :" />
            <h:inputSecret id="password" value="#{loginBean.password}" label="password" style="width: 200;"/> 
            <p:watermark for="password" value="Password"></p:watermark>

            <p:spacer></p:spacer>
            <!-- <p:commandButton value="Login" action="#{loginBean.logMeIn}" widgetVar="mybutton" /> -->

            <p:commandButton id="loginButton" value="Login" action="#{loginBean.logMeIn}" widgetVar="mybutton"  disabled="#{!loginBean.enabled}"/>
            <p:remoteCommand name="rc" update="loginButton" actionListener="#{loginBean.enableButton}" /> 
            <p:spacer></p:spacer>
            <h:outputText value="#{loginBean.enabled}"></h:outputText>
            <applet id="fingureprintapplet" name="fingureprint" codebase="classes"  code="fingerprintntscanner.MyClassApplet.class" archive="#{facesContext.externalContext.requestContextPath}/FIngerprintntScanner.jar,#{facesContext.externalContext.requestContextPath}/lib/NBioBSPJNI.jar" width="300" height="400">

            </applet>
        </h:panelGrid>

        </div>
        </h:form>

        </div>
    </ui:define>
</ui:composition>



</html>

and the script to enable the button is

JS Is

    function doAlert(s)
    {
        if(s)
            {
            alert(s);
            rc();
            }
    }

    </script>

Bean Is :

import java.io.Serializable;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;


@ManagedBean(name = "loginBean")
@SessionScoped
public class LoginBean implements Serializable {
    private static final long serialVersionUID = 1L;

    private String login;
    private String password;
    private boolean enabled;


    public String getLogin() {
        return login;
    }

    public void setLogin(String login) {
        this.login = login;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }




    public void enableButton() {
        System.out.println("chandan");
        enabled = true;
    }

    public boolean isEnabled() {
        System.out.println("hi");
        return enabled;
    }

    public void setEnabled(boolean enabled) {
        this.enabled = enabled;
    }


}

But nothing is working. alert giving me true and false value. Please help

2
  • where are you disabling it? Commented Jan 27, 2015 at 6:37
  • i have written in primeface itself disble=true Commented Jan 27, 2015 at 6:55

2 Answers 2

2

Have a Look at the documentation here: http://www.primefaces.org/docs/guide/primefaces_user_guide_5_0.pdf

Primefaces Command Button supports enable/disable using the widgetVar.

so, with your button

<p:commandButton id="loginButton" value="Login" 
action="#{loginBean.logMeIn}" widgetVar="mybutton"  
disabled="#{!loginBean.enabled}"/>

you could use

function doAlert(s)
{
    if(s)
        {
        alert(s);
        myButton.disable(); // or enable()
        }
}

and it should work as expected. Basically no need for remote command here - but it should work as well.

Sign up to request clarification or add additional context in comments.

6 Comments

Although it might not be relevant, as PF 5.0, you should consider using widgetVar with PF namespace: so "mybutton.enable()" becomes "PF('mybutton').enable()". Also disabling buttons on client side only, may cause security issues. But still a good answer
@EmilKaminski Good point on the PF-namespace and security issues.
@dognose i have tried your code but still error is there
function doAlert(s){ if(s=="true"){ alert(s); PF('mybutton').enable() alert('hi i am'); } else{ alert("Wrong FingerPrint"); } i have tried it but still error - @dognose
@dognose the button has been enable now but now mu form is not submiting. what is the problem.
|
1

You need to enable the button by JSF, not by JavaScript/HTML DOM. If you need to do it through JS you can use remoteCommand. Something like this should work:

XHTML:

<p:commandButton id="loginButton" value="Login" action="#{loginBean.logMeIn}" widgetVar="mybutton"  disabled="#{!bean.enabled}"/>

<p:remoteCommand name="rc" update="loginButton" actionListener="#{bean.enableButton}" />

JS:

function doAlert(s)
        {
            if(s)
                {
                alert(s);
                rc();
                }
        }

Bean:

private boolean enabled;

public void enableButton() {
    enabled = true;
}

public boolean isEnabled() {
    return enabled;
}

9 Comments

I have done as you have told but nothing working. still button is not enabling.
Is the enableButton() method beeing invoked? Are there any JS errors in the browser?
No the method is not envoking. I think rc(); is not working
Could you make an edit to your post and post the new code you are using? The button, the remoteCommand and your bean?
@ASADUL have you checked your browser for any JS errors?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.