Java 8默认接口方法未在EL中识别为托管Bean属性 [英] Java 8 default interface methods not recognized as managed bean properties in EL

查看:200
本文介绍了Java 8默认接口方法未在EL中识别为托管Bean属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试设置自己的JSF标签库.因此,我创建了一个具有后备接口的复合组件,并将其作为蓝图为该组件构建后备Bean.

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";
   }
}

因此,我具有登录站点的密码,用户名和验证方法.另外,O要为Inputtext水印和Button提供一些默认命名.如果实施者想要更改它,他可以.

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.

我使用自己的JSF标签在真实应用程序的Backing bean中实现了此接口.

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;
    }
}

我坚信它可以这样工作.但我收到错误消息:由:javax.el.PropertyNotFoundException引起:类'Login'没有属性'usernameWatermark'.'

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'.'

似乎接口的默认实现未在我的实现Bean中继承.为什么以及如何为我的组件提供默认实现?

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?

我尝试了以下操作来省略对接口默认方法概念的误解.我将我的接口和der实现类放在一个普通的Java项目中,试图通过Login类访问getLoginButtonValue.

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());
    }
}

效果很好.正确的字符串被打印出来.无需实现默认方法即可访问它们.那么问题出在哪里呢?也许有些东西像cdi,bean-resolver或其他东西不知道这个Java 8概念?

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?

推荐答案

使用Apache EL可以通过使用其全名调用默认方法来实现.尝试在您的代码中以这种方式使用它:

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()}

这篇关于Java 8默认接口方法未在EL中识别为托管Bean属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆