Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java 8 default interface methods not recognized as managed bean properties in EL

I am trying to setup my own JSF tag libary. So I created a composite component with an backing interfaces as a blueprint to build a backing bean for this component.

public interface CompLogin {
   String getUsername();
   void setUsername(String username);

   String getPassword();
   void setPassword(String password);

   String validateLogin();

   default String getPasswordWatermark() {
      return "Passwort";
   }

   default String getUsernameWatermark() {
      return "Loginname:";
   }

   default String getLoginButtonValue() {
      return "Login";
   }
}

So I have Password, Username and an Validate method for a Login site. Additionally O want serve some default namings for Inputtext watermarks and the Button. If the implementing person want to change it, he could.

I implemented this interface inside a Backing bean of a real application using my own JSF tag.

@Named
@RequestScoped
public class Login implements Serializable, CompLogin {

    private String username;
    private String password;


    @Override
    public String getUsername() {
        return username;
    }

    @Override
    public void setUsername(String username) {
        this.username = username;
    }

    @Override
    public String getPassword() {
        return password;
    }

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

    @Override
    public String validateLogin() {
        System.out.println(username + " " + password);
        return null;
    }
}

I tought it could work this way. But I get the error: 'Caused by: javax.el.PropertyNotFoundException: The class 'Login' does not have the property 'usernameWatermark'.'

It seems like the default implementation of the interface is not inherited in my implementing bean. Why and what could I do to serve default implementations for my components?

Edit: I tried the following to ommit a missunderstanding of the interface default method conecpt. I took my interface and der implementing class in a normal java project tried to access the getLoginButtonValue thru the Login class.

public class Main {

    public static void main(String[] args) {
        Login log = new Login();
        System.out.println(log.getLoginButtonValue());
    }
}

Works very well. The correct String got printed out. There is no need to implement the default methods to access them. So where is the problem? Maybe there is something like cdi, bean-resolver or somthing else not aware of this java 8 concept?

like image 328
ScreamingTree Avatar asked May 13 '16 09:05

ScreamingTree


People also ask

Can we override default method of interface Java 8?

A default method cannot override a method from java.

What is the use of default method in interface in Java 8?

The default methods were introduced to provide backward compatibility so that existing interfaces can use the lambda expressions without implementing the methods in the implementation class. Default methods are also known as defender methods or virtual extension methods.

What is the use of default and static methods in interface Java 8?

Default methods enable you to add new functionality to the interfaces of your libraries and ensure binary compatibility with code written for older versions of those interfaces. A static method is a method that is associated with the class in which it is defined rather than with any object.

How will you call the default method of interface without implementation?

A class can override a default interface method and call the original method by using super , keeping it nicely in line with calling a super method from an extended class. But there is one catch, you need to put the name of the interface before calling super this is necessary even if only one interface is added.


2 Answers

Working with Apache EL this works by calling the default method by its full name. Try to use it this way in your code:

#{login.getUsernameWatermark()}
like image 51
sofarsoghood Avatar answered Nov 16 '22 14:11

sofarsoghood


The problem is likely caused by EL relying on reflection to find the appropriate accessor methods, but doing it in a way that fails for default methods.

Consider implementing a custom ELResolver similar to what they did here.

like image 44
jvalli Avatar answered Nov 16 '22 14:11

jvalli