Miten
Miten

Reputation: 358

enable disable button in jsf 2.2

I am trying simple example to enable and disable button based upon submit of form. If some one can also point me to concept of jsf:id as I am mixing plain html5 with jsf.

Below are relevant code:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
     xmlns:jsf="http://java.sun.com/jsf" 
     xmlns:f="http://java.sun.com/jsf/core">
    <head jsf:id="head">
        <title>Putting it all together </title>
        <!-- script jsf:target="body" jsf:name="js.js"/>
        <link jsf:name="css.css" rel="stylesheet" type="text/css" /-->
    </head>
    <body jsf:id="body">
        <form jsf:id="form" jsf:prependId="false">
            <label jsf:for="name">Name </label>
            <input jsf:id="name" type="text" jsf:value="#{complex.name}">
                <f:ajax execute="@this" render="progress"/>
            </input>
            <label jsf:for="tel">Tel </label>
            <input jsf:id="tel" type="tel" jsf:value="#{complex.tel}">
                <f:ajax execute="@this" render="progress"/>
            </input>

            <label jsf:for="email">Email </label>
            <input jsf:id="email" type="email" jsf:value="#{complex.email}">
                <f:ajax execute="@this" render="progress"/>
            </input>

            <label for="progress">Progress </label>
            <progress jsf:id="progress" max="3">#{complex.progress} of 3 </progress>


            <input jsf:id="submit" type="submit" value="submit">
                <f:ajax execute="@this" render="login" event="click" listener="#{complex.process}" />
            </input>

            <!-- label jsf:id="status"></label-->
            <button jsf:id="login" disabled="#{complex.status}">Login</button>

        </form>
    </body>
</html>

The managed bean is:

package mypackage.bean;
import java.io.Serializable;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.event.AjaxBehaviorEvent;

@ManagedBean
@SessionScoped
public class Complex implements Serializable {
    public Complex() {}

    private String name;
    private String tel;
    private String email;

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getTel() {
        return tel;
    }
    public void setTel(String tel) {
        this.tel = tel;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    private boolean status;

    public void process(AjaxBehaviorEvent event) {
        status = !status;
    }


    public boolean isStatus() {
        return status;
    }

    public boolean getStatus() {
        return status;
    }

    public void setStatus(boolean status) {
        this.status = status;
    }

    public String getProgress() {
        int progress = 0;
        if(name != null) {
            progress++;
        }
        if(tel != null) {
            progress++;
        }
        if(email != null) {
            progress++;
        }
        return progress + "";
    }


}

Here is error printed to web page:

HTTP Status 500 - java.lang.Boolean cannot be cast to java.lang.String

type Exception report

message java.lang.Boolean cannot be cast to java.lang.String

description The server encountered an internal error that prevented it from fulfilling this request.

exception

javax.servlet.ServletException: java.lang.Boolean cannot be cast to java.lang.String
    javax.faces.webapp.FacesServlet.service(FacesServlet.java:658)

root cause

java.lang.ClassCastException: java.lang.Boolean cannot be cast to java.lang.String
    com.sun.faces.renderkit.html_basic.HtmlResponseWriter.getAttributeValue(HtmlResponseWriter.java:1210)
    com.sun.faces.renderkit.html_basic.HtmlResponseWriter.flushAttributes(HtmlResponseWriter.java:1170)
    com.sun.faces.renderkit.html_basic.HtmlResponseWriter.closeStartIfNecessary(HtmlResponseWriter.java:1112)
    com.sun.faces.renderkit.html_basic.HtmlResponseWriter.writeText(HtmlResponseWriter.java:935)
    com.sun.faces.facelets.compiler.LiteralTextInstruction.write(LiteralTextInstruction.java:76)
    com.sun.faces.facelets.compiler.UIInstructions.encodeBegin(UIInstructions.java:82)
    com.sun.faces.facelets.compiler.UILeaf.encodeAll(UILeaf.java:183)
    javax.faces.render.Renderer.encodeChildren(Renderer.java:176)
    javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:900)
    javax.faces.component.UIComponent.encodeAll(UIComponent.java:1847)
    javax.faces.render.Renderer.encodeChildren(Renderer.java:176)
    javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:900)
    javax.faces.component.UIComponent.encodeAll(UIComponent.java:1847)
    javax.faces.component.UIComponent.encodeAll(UIComponent.java:1850)
    javax.faces.component.UIComponent.encodeAll(UIComponent.java:1850)
    com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:443)
    com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:131)
    com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:120)
    com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:202)
    javax.faces.webapp.FacesServlet.service(FacesServlet.java:646)

note The full stack trace of the root cause is available in the Apache Tomcat/7.0.35 logs.
Apache Tomcat/7.0.35

Upvotes: 0

Views: 5480

Answers (1)

Freak
Freak

Reputation: 6873

The critical part of your code is

<button jsf:id="login" disabled="#{complex.status}">Login</button>

and then this exception

java.lang.ClassCastException: java.lang.Boolean cannot be cast to java.lang.String

Now It mean for <button> tag, the value of disabled attribute is not boolean but a String.
So the value of this attribute is disabled="disabled".
Please see this question as well for handling the disabled attribute for HTML5
You need to set the String value from your managed bean and declare status property as a String rather than boolean.

Upvotes: 5

Related Questions