gpt4 book ai didi

jsf - 执行 (Primefaces) 菜单项的 ActionListener 会导致 IllegalStateException

转载 作者:行者123 更新时间:2023-12-04 14:18:51 26 4
gpt4 key购买 nike

在 JSF 支持的 bean 中,我得到了一个 IllegalStateException当调用以编程方式添加的 Primefaces 菜单项的以编程方式添加的 Action 监听器时。我都试过了 requestsession范围,但两者都导致相同的错误。显然,根据堆栈跟踪,需要在执行 Action 监听器时恢复 View ,我让 ToolbarBean实现 Serializable没有不同的效果。我应该考虑什么才能让它发挥作用?

用户界面定义

<?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:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://primefaces.prime.com.tr/ui">

<h:head>
<title>TITLE</title>
</h:head>

<h:body>
<h:form>
<p:menu model="#{toolbarBean.model}" type="tiered" />
</h:form>
</h:body>
</html>

提供菜单的支持 bean
@Named
@Scope("request")
public class ToolbarBean implements Serializable {

private static final long serialVersionUID = -8556751897482662530L;

public ToolbarBean() {
model = new DefaultMenuModel();

MenuItem item;

// Direct menu item
item = new MenuItem();
item.setValue("Menuitem 1");
item.addActionListener(new ActionListener() {
@Override
public void processAction(ActionEvent event)
throws AbortProcessingException {
System.out.println(event.toString());
}
});

model.addMenuItem(item);

item = new MenuItem();
item.setValue("Menuitem 2");
item.addActionListener(new ActionListener() {
@Override
public void processAction(ActionEvent event)
throws AbortProcessingException {
System.out.println(event.toString());
}
});

model.addMenuItem(item);
}

private MenuModel model;

public MenuModel getModel() {
return model;
}
}

单击菜单按钮之一时出现异常
javax.faces.FacesException: java.lang.IllegalStateException: java.lang.InstantiationException: id.co.sofcograha.baseui.ToolbarBean$1
at javax.faces.component.UIComponent.invokeOnComponent(UIComponent.java:1284)
at javax.faces.component.UIComponentBase.invokeOnComponent(UIComponentBase.java:673)
at javax.faces.component.UIComponent.invokeOnComponent(UIComponent.java:1290)
at javax.faces.component.UIComponentBase.invokeOnComponent(UIComponentBase.java:673)
at javax.faces.component.UIComponent.invokeOnComponent(UIComponent.java:1290)
at javax.faces.component.UIComponentBase.invokeOnComponent(UIComponentBase.java:673)
at javax.faces.component.UIComponent.invokeOnComponent(UIComponent.java:1290)
at javax.faces.component.UIComponentBase.invokeOnComponent(UIComponentBase.java:673)
at com.sun.faces.application.view.StateManagementStrategyImpl.restoreView(StateManagementStrategyImpl.java:297)
at com.sun.faces.application.StateManagerImpl.restoreView(StateManagerImpl.java:177)
at com.sun.faces.application.view.ViewHandlingStrategy.restoreView(ViewHandlingStrategy.java:119)
at com.sun.faces.application.view.FaceletViewHandlingStrategy.restoreView(FaceletViewHandlingStrategy.java:438)
at com.sun.faces.application.view.MultiViewHandler.restoreView(MultiViewHandler.java:144)
at javax.faces.application.ViewHandlerWrapper.restoreView(ViewHandlerWrapper.java:284)
at com.sun.faces.lifecycle.RestoreViewPhase.execute(RestoreViewPhase.java:182)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:97)
at com.sun.faces.lifecycle.RestoreViewPhase.doPhase(RestoreViewPhase.java:107)
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:114)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:308)

最佳答案

EL(阅读:反射)无法访问/构造匿名类。将它们重构为完全值得的类。

所以,更换

    item.addActionListener(new ActionListener() {
@Override
public void processAction(ActionEvent event)
throws AbortProcessingException {
System.out.println(event.toString());
}
});

经过
    item.addActionListener(new FooActionListener());


public class FooActionListener implements ActionListener {

@Override
public void processAction(ActionEvent event)
throws AbortProcessingException {
System.out.println(event.toString());
}

}

也可以看看:
  • How to invoke a JSF action on an anonymous class?
  • 关于jsf - 执行 (Primefaces) 菜单项的 ActionListener 会导致 IllegalStateException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5432897/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com