【问题标题】:How to access getComponent in a derived class in Vaadin如何在 Vaadin 的派生类中访问 getComponent
【发布时间】:2016-05-01 04:37:33
【问题描述】:

这是我的代码。我想做以下事情。但我在 LoginStep1 类的 getComponent(0) 处遇到异常。我应该如何解决这个问题。 我是 vaadin 的新手。如果我的方法有误,需要您的指导。

public class Login extends VerticalLayout implements View {
    public static String viewName = "login";

    public void enter(ViewChangeEvent event) {
        removeAllComponents();
        CustomLayout viewScreen = new CustomLayout("screens/screen-login");

        Component step1 = new LoginStep1().getLoginStep1();

        viewScreen.addComponent(step1, "login-steps");
        addComponent(viewScreen);
    }
}
@SuppressWarnings("serial")
public class LoginStep1 extends Login {
    public Component getLoginStep1() {
        CustomLayout stepScreen = new CustomLayout("components/screens/login-step1");
        Button loginBtn = CommonComponents.getButton("Login", "btn btn-green btn-block");

        loginBtn.addClickListener(new Button.ClickListener() {
            @Override
            public void buttonClick(ClickEvent event) {
                // this line gives me exception.
                CustomLayout currentLayout = (CustomLayout) getComponent(0);
                currentLayout.addComponent(new LoginStep2().getLoginStep2(request, posInfoResponse.getBody()), "login-steps");
            }
        });
    }

    stepScreen.addComponent(loginBtn,"login-btn");
    return stepScreen;
}

它给出以下异常

com.vaadin.server.ServerRpcManager$RpcInvocationException: Unable to invoke method click in com.vaadin.shared.ui.button.ButtonServerRpc
    at com.vaadin.server.ServerRpcManager.applyInvocation(ServerRpcManager.java:160)
    at com.vaadin.server.ServerRpcManager.applyInvocation(ServerRpcManager.java:118)
    at com.vaadin.server.communication.ServerRpcHandler.handleInvocations(ServerRpcHandler.java:408)
    at com.vaadin.server.communication.ServerRpcHandler.handleRpc(ServerRpcHandler.java:273)
    at com.vaadin.server.communication.UidlRequestHandler.synchronizedHandleRequest(UidlRequestHandler.java:79)
    at com.vaadin.server.SynchronizedRequestHandler.handleRequest(SynchronizedRequestHandler.java:41)
    at com.vaadin.server.VaadinService.handleRequest(VaadinService.java:1409)
    at com.vaadin.server.VaadinServlet.service(VaadinServlet.java:364)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:217)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:673)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1500)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1456)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at com.vaadin.server.ServerRpcManager.applyInvocation(ServerRpcManager.java:158)
    ... 29 more
Caused by: com.vaadin.event.ListenerMethod$MethodException: Invocation of method buttonClick in com.herman.login.LoginStep1$1 failed.
    at com.vaadin.event.ListenerMethod.receiveEvent(ListenerMethod.java:528)
    at com.vaadin.event.EventRouter.fireEvent(EventRouter.java:198)
    at com.vaadin.event.EventRouter.fireEvent(EventRouter.java:161)
    at com.vaadin.server.AbstractClientConnector.fireEvent(AbstractClientConnector.java:1003)
    at com.vaadin.ui.Button.fireClick(Button.java:377)
    at com.vaadin.ui.Button$1.click(Button.java:54)
    ... 34 more
Caused by: java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
    at java.util.LinkedList.checkElementIndex(Unknown Source)
    at java.util.LinkedList.get(Unknown Source)
    at com.vaadin.ui.AbstractOrderedLayout.getComponent(AbstractOrderedLayout.java:414)
    at com.herman.login.LoginStep1$1.buttonClick(LoginStep1.java:23)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at com.vaadin.event.ListenerMethod.receiveEvent(ListenerMethod.java:508)
    ... 39 more

【问题讨论】:

  • 您遇到了什么异常?
  • 修改任何解决方案。
  • getLoginStep1() 是否在进入Login 视图之前执行?从外观上看,您是在实际添加任何组件之前执行该方法...
  • 但是如果我在同一个类 Login 中添加 getLoginStep1() 方法,它工作正常
  • 因为它们位于对象的同一个实例中,而不是您现在拥有的半循环调用。我假设您在收到错误消息时正在导航到 Login

标签: java vaadin vaadin7 vaadin6


【解决方案1】:

有些事情还不清楚,因为你还没有回答所有的 cmets,所以 我不确定您将 3 个实现中的哪一个(LoginLoginStep1LoginStep2)用作导航器组件的导航视图。但是,查看LoginLoginStep1,我可以看到您可能需要在当前设计中重新考虑的一些事情:您正在创建继承类的新实例并在它们上调用方法,而不是使用当前的实例...

由于LoginStep2 的来源不可用,我将尝试介绍其他两种情况:

1)假设您正在导航到Login 的实例。

  • 当调用enter 方法时,会创建一个新的LoginStep1 实例并将其添加到CustomLayout 内的组件列表中。

  • 当您单击登录按钮时,getComponent(0) 将在 LoginStep1 的新实例上调用,该实例不继承自导航中使用的 Login 实例,因此它包含 0 个导致您的异常的组件

2)假设您正在导航到LoginStep1 的一个实例,它扩展了Login

  • 当触发enter事件并执行继承自超类的enter方法时,会创建一个新的LoginStep1实例并将其添加到CustomLayout内的组件列表中。

  • 当您点击登录按钮时,getComponent(0) 会在LoginStep1 的新实例上被调用,这与导航中使用的当前LoginView1 实例无关,因此它包含 0 个导致您的异常的组件


总之,这更可能与基本的java继承概念有关,而不是与Vaadin有关,主要问题可能是Component step1 = new LoginStep1().getLoginStep1(); // <= why create a new instance?

public class Login extends VerticalLayout implements View {
    public static String viewName = "login";

    public void enter(ViewChangeEvent event) {
        removeAllComponents();
        CustomLayout viewScreen = new CustomLayout("screens/screen-login");

        Component step1 = new LoginStep1().getLoginStep1(); // <= why create a new instance?

        viewScreen.addComponent(step1, "login-steps");
        addComponent(viewScreen);
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-03-05
    • 2014-03-16
    • 1970-01-01
    • 2018-07-14
    • 2021-10-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多