sabato 25 agosto 2012

JSF EL different behaviour with boolean vs Boolean Bean attribute

According Java Bean specification when you define a bean attribute (any kind of type - primitive or Object) you should also define getter and setter methods.

For get method a special attribute is the boolean attribute; infact for boolean attribute you may implement both  'is' method and 'get' method.

Sometimes you may choose for some reason to change an attribute from boolean to Boolean type.

You can make your change but with some concerns:

-consider how attribute initialization differs between  primitive boolean (compiler force you to initializate to 'false' or 'true' value) and Boolean (you can leave it null).

- another great difference is about getter methods: infact with Boolean type you have to implement get method and not is method (allowed only for boolean) otherwise you may expect errors.



Here a sample:

HTML JSF page

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html">
   <h:head>
      <title>Welcome</title>
   </h:head>
   <h:body>
      <h:form>
         <h3>Please enter your name and password.</h3>  
         <table>
            <tr>
               <td>Name:</td>
               <td><h:inputText value="#{user.name}" rendered="#{user.illiquid}" /></td>
            </tr>
            <tr>
               <td>Password:</td>
               <td><h:inputSecret value="#{user.password}"/></td>
            </tr>
         </table>
         <p><h:commandButton value="Login" action="welcome"/></p>
      </h:form>
   </h:body>
</html>

 That's is user backing bean with visible property type changed from boolean to Boolean.

package com.corejsf;

import java.io.Serializable;
import javax.inject.Named;
   // or import javax.faces.bean.ManagedBean;
import javax.enterprise.context.SessionScoped;
   // or import javax.faces.bean.SessionScoped;

@Named("user") // or @ManagedBean(name="user")
@SessionScoped
public class UserBean implements Serializable {
   private String name;
   private String password;
   private Boolean visible = true;

   public String getName() { return name; }  
   public void setName(String newValue) { name = newValue; }

   public String getPassword() { return password; }
   public void setPassword(String newValue) { password = newValue; }  
  
   public Boolean isVisible(){
       return visible;
   }
  }

Here the error
/index.xhtml @15,83 rendered="#{user.visible}": The class 'com.corejsf.UserBean$Proxy$_$$_WeldClientProxy' does not have the property 'visible'.

And here fixed bean

package com.corejsf;

import java.io.Serializable;
import javax.inject.Named;
   // or import javax.faces.bean.ManagedBean;
import javax.enterprise.context.SessionScoped;
   // or import javax.faces.bean.SessionScoped;

@Named("user") // or @ManagedBean(name="user")
@SessionScoped
public class UserBean implements Serializable {
   private String name;
   private String password;
   private Boolean visible = true;

   public String getName() { return name; }  
   public void setName(String newValue) { name = newValue; }

   public String getPassword() { return password; }
   public void setPassword(String newValue) { password = newValue; }  
  
   public Boolean getVisible(){
       return visible;
   }
  
  



1 commento:

  1. can we use boolean variables in managed bean without giving it a getter setter methd?

    RispondiElimina