如何在匿名类上调用JSF操作? EL无法访问它 [英] How to invoke JSF action on an anonymous class? EL cannot access it

查看:104
本文介绍了如何在匿名类上调用JSF操作? EL无法访问它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望在我的JSF(MyFaces 1.2)应用程序中有一个通用菜单。

I want to have a generic menu in my JSF (MyFaces 1.2) application.

<h:form>
  <h:dataTable id="dt1" value="#{portal.actionList}" var="item">
    <f:facet name="header">
        <h:outputText value="Menu" />
    </f:facet>
    <h:column>
        <h:commandLink action="#{item.action}">
            <h:outputText value="clickme"/>
        </h:commandLink>
     </h:column>
   </h:dataTable>
 </h:form>

然后我在会话范围内的门户网站将如下所示:

Then my portal on session-scope would look like this:

 class Portal {
    private ArrayList<IAction> list = new ArrayList<IAction>();

    public Portal() {
       list.add(new IAction() {
                public action() {
                    log.info("called action here");
                }
        });
    }
    public ArrayList<IAction> getActionList() {
        return list;
    }
 }

当我运行此代码时,它会显示正常。但是当您尝试通过单击clickme命令链接执行操作时,将发生以下异常:

When I run this code it will display fine. But when you try to execute the action by clicking the "clickme" command link - the following exception will occur:

  Class org.apache.el.parser.AstValue can not access a member of class Portal$1 with modifiers "public"

有没有办法显示一个匿名类列表,从中可以执行一个方法(在这个例子中是 ITemplate.action())?

Is there any way to display a list of anonymous classes, from which an method (in this example ITemplate.action()) can be executed?

编辑:

当我使用(内部)类时,它可以正常工作。例如:

It works when I use an (inner) class. Like for example:

    class Bla implements IAction {
         public void action() {
             log.info("yes, i am working");
         }

以及Portal构造函数

and in the Portal constructor

 public Portal() {
   list.add( new Bla() );
}

但这不是我想要的方式......

But this is not the way I want it...

推荐答案

这是因为无法从包含匿名类的包外部访问匿名类。

That's because anonymous classes are not accessible from outside the package containing the anonymous class.

这是演示幕后发生的事情:

Here's a demonstration what's happening behind the scenes:

public static void main(String[] args) throws Exception {
    Portal portal = new Portal();
    Object list = portal.getClass().getDeclaredMethod("getActionList", null).invoke(portal, null);
    Object action = list.getClass().getDeclaredMethod("get", new Class[] { int.class }).invoke(list, 0);
    action.getClass().getDeclaredMethod("action", null).invoke(action, null);
}

尝试在与 Portal <相同的包中执行此操作/ code> class然后再次在包外的另一个类中。在另一个包中,最后一行将抛出完全相同的异常。这是EL正在努力解决的问题,因为它基于反思。

Try executing this in the same package as Portal class and then once again in another class outside the package. In the other package, the last line would then throw exactly the same exception. That's the problem EL is struggling with since it's based on reflection.

我没有看到其他方法来解决这个问题,而不仅仅是创建一个 public (内部) class 。反射(以及EL)也可以从其他包中访问它们。

I don't see other ways to solve this nicely than just creating a public (inner) class. Reflection (and thus also EL) can access them from other packages.

public class Portal {

    private List<IAction> list = new ArrayList<IAction>();

    public Portal() {
        list.add(new IActionImpl());
    }

    public class IActionImpl implements IAction {
        public void action() {
            System.out.println("called action here");
        }
    }

    public List<IAction> getActionList() {
        return list;
    }

}

这篇关于如何在匿名类上调用JSF操作? EL无法访问它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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