简体   繁体   English

无法使用Vaadin导航器重定向

[英]Cannot redirect with the Vaadin navigator

I've a very strange behavior, I think I have two issues, I put them together on the same post because they can be linked : 我的行为很奇怪,我认为我有两个问题,我可以将它们放在一起,因为它们可以链接在一起:

my code is : 我的代码是:

VaadinSession.getCurrent().setAttribute("user", user);
System.out.println("User :"+ user);
getUI().getNavigator().navigateTo(HomePageView.HOMEPAGE);

1. First issue 1.第一期

I'm on the login page, I can see my user information, but I cannot navigate to the homepage. 我在登录页面上,可以看到我的用户信息,但无法导航到主页。 I don't have any error ! 我没有任何错误!

If I delete the line with the vaadinSession, the navigator is working... 如果我删除带有vaadinSession的行,则导航器正在运行...

2. Second Issue 2.第二期

I tried to debug my code but I received a "source not found", to fix that, I follow Eclipse java debugging: source not found . 我尝试调试代码,但收到一个“找不到源”,为解决此问题,我按照Eclipse Java调试:未找到源 But, seems to working for me. 但是,似乎为我工作。 What I did : 我做了什么 :

  • I recreated a new workspace without success. 我重新创建了一个新的工作区,但没有成功。
  • I edited the source lookup path and I have my java project in it 我编辑了源查找路径,并在其中添加了Java项目。
  • In the preferences -> java -> installed JREs -> I've the 1.8.0 JDK 在首选项中-> java->已安装的JRE->我有1.8.0 JDK
  • right click on the project -> maven -> download sources 右键单击项目-> Maven->下载源
  • right click on the project -> maven -> disable maven nature and after Configure -> project to maven 右键单击项目-> maven->禁用maven性质,然后在Configure->项目之后进行maven

EDIT : SOLUTION for the issue 2 : I was so blocked... I know it's an eclipse issue (configuration or something like that). 编辑:问题2的解决方案:我是如此受阻...我知道这是一个蚀问题(配置或类似问题)。 I changed for IntelliJ. 我换了IntelliJ。 This IDE show me sources without problem. 此IDE向我显示了没有问题的源代码。

INFO 信息

I'm using Vaadin and REST web services (with the javax.ws.rs.client.ClientBuilder). 我正在使用Vaadin和REST Web服务(带有javax.ws.rs.client.ClientBuilder)。 When I use SYSOUT, I have the good information. 使用SYSOUT时,我掌握了很好的信息。 I received the information from the homepage (instead of the view seems to keep the login view). 我从主页收到了信息(该视图似乎保留了登录视图)。

Any hint will be very useful ! 任何提示将非常有用!


EDIT : Full LoginView class 编辑:完全LoginView类

package com.test.project.View;

import com.test.project.model.User;
import com.test.project.restclient.RestClient;
import com.vaadin.annotations.Title;
import com.vaadin.data.Binder;
import com.vaadin.data.validator.EmailValidator;
import com.vaadin.icons.VaadinIcons;
import com.vaadin.navigator.View;
import com.vaadin.navigator.ViewChangeListener.ViewChangeEvent;
import com.vaadin.server.VaadinSession;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.CustomComponent;
import com.vaadin.ui.Panel;
import com.vaadin.ui.TextField;
import com.vaadin.ui.VerticalLayout;


    /**
     * Login View. The user should enter his email address. Extends {@link CustomComponent} and implements {@link View}
     * 
     * @author Bob
     */
    @Title("Sign Up")
    public class LoginView extends CustomComponent implements
            View {

        private static final long serialVersionUID = 1L;

        public static final String LOGIN = "";

        private VerticalLayout vLayout = new VerticalLayout();

        private static final String SIGNUP_LABEL = "Sign Up";
        private static final String EMAIL_CAPTION = "Type your email here :";
        private static final String SIGNIN_LABEL = "Sign In";
        private TextField email;
        private static final String TOKEN_ATTRIBUTE_LABEL = "token";

        private final Binder<User> binder = new Binder<>();
        private User user;

        private Button loginButton;

        /**
         * Login view Constructor
         */
        public LoginView() {
            createLoginPanel();
            addListener();
        }

        /**
         * Add Listener concern by the Login View Fields
         */
        private void addListener() {
            loginButton.addClickListener(e -> {
                RestClient rc = new RestClient();
                user = rc.getUserInfo(email.getValue());
                VaadinSession.getCurrent().setAttribute(TOKEN_ATTRIBUTE_LABEL, user.getToken());
                System.out.println();
                getUI().getNavigator().navigateTo(HomePageView.HOMEPAGE);
            });
        }

        /**
         * Create the login panel with the email field and the login button
         */
        private void createLoginPanel() {
            final VerticalLayout layout = new VerticalLayout();
            layout.setSizeFull();

            Panel panel = new Panel(SIGNUP_LABEL);
            panel.setHeight(200, Unit.PIXELS);
            panel.setWidth(300, Unit.PIXELS);

            email = new TextField();
            email.setCaption(EMAIL_CAPTION);
            email.setHeight(30, Unit.PIXELS);
            email.setWidth(275, Unit.PIXELS);
            binder.forField(email).withValidator(new EmailValidator("This doesn't look like a valid email address")).bind(User::getEmail, User::setEmail);

            loginButton = new Button(SIGNIN_LABEL);
            loginButton.setIcon(VaadinIcons.SIGN_IN);

            layout.addComponents(email, loginButton);
            layout.setComponentAlignment(loginButton, Alignment.BOTTOM_RIGHT);

            panel.setContent(layout);

            vLayout.addComponent(panel);
            vLayout.setSizeFull();
            vLayout.setComponentAlignment(panel, Alignment.MIDDLE_CENTER);
            setCompositionRoot(vLayout);
        }

        /* (non-Javadoc)
         * @see com.vaadin.navigator.View#enter(com.vaadin.navigator.ViewChangeListener.ViewChangeEvent)
         */
        @Override
        public void enter(ViewChangeEvent event) {
            email.focus();
        }
    }

Rest client class : 其他客户类:

package com.test.project.restclient;

import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.glassfish.jersey.client.ClientConfig;
import org.json.simple.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.gson.Gson;
import com.test.project.model.User;


/**
 * Class contain the Rest Client which allow to use and call the rest web services.
 * 
 * @author Bob
 */
public class RestClient {

    private static final String EMAIL_LABEL = "email";
    private static final Logger LOG = LoggerFactory.getLogger(RestClient.class);

    private Client client;

    public RestClient() {
        client = ClientBuilder.newClient(new ClientConfig());
    }

    /**
     * Get the user information from the user email
     * 
     * @param email
     * @return user
     */
    @SuppressWarnings("unchecked")
    @POST
    @Path("http://IpAddress:8080/api/authentication/")
    public User getUserInfo(String email) {
        JSONObject obj = new JSONObject();
        obj.put(EMAIL_LABEL, email);

        WebTarget webtarget = client.target("http://IpAddress:8080/api/authentication/");

        Response response = webtarget.request().accept(MediaType.APPLICATION_JSON).post(Entity.entity(obj, MediaType.APPLICATION_JSON));

        String answer = response.readEntity(String.class);
        LOG.info("User information are :" + answer);
        Gson g = new Gson();
        User user = g.fromJson(answer, User.class);
        return user;
    }

}

Home Page View : 主页视图:

package com.test.project.View;

import com.test.project.model.Action;
import com.test.project.restclient.RestClient;
import com.vaadin.data.Binder;
import com.vaadin.icons.VaadinIcons;
import com.vaadin.navigator.View;
import com.vaadin.navigator.ViewChangeListener.ViewChangeEvent;
import com.vaadin.server.VaadinSession;
import com.vaadin.ui.Button;
import com.vaadin.ui.ComboBox;
import com.vaadin.ui.CustomComponent;
import com.vaadin.ui.MenuBar;
import com.vaadin.ui.TextField;
import com.vaadin.ui.VerticalLayout;


/**
 * Home page view.
 *
 * @author Bob
 */
public class HomePageView extends CustomComponent implements
        View {

    private static final long serialVersionUID = 1L;
    public static final String HOMEPAGE = "home";

    private final VerticalLayout layout;

    private static final String TOKEN_ATTRIBUTE_LABEL = "token";

    /**
     * Home page View constructor
     */
    public HomePageView() {
        layout = new VerticalLayout();
        layout.setSizeFull();
        String CURRENT_USER_TOKEN = (String) VaadinSession.getCurrent().getAttribute(TOKEN_ATTRIBUTE_LABEL);
        System.out.println("Current user token : " + CURRENT_USER_TOKEN);
        createMenu();

        setCompositionRoot(layout);
    }

    /**
     * Create a Vertical Menu with the Home page and Actions page
     */
    private void createMenu() {
        MenuBar barmenu = new MenuBar();
        barmenu.addItem("Homepage", VaadinIcons.HOME, null);
        barmenu.addItem("Actions", VaadinIcons.TABLE, null);
        layout.addComponent(barmenu);
    }


    /* (non-Javadoc)
     * @see com.vaadin.navigator.View#enter(com.vaadin.navigator.ViewChangeListener.ViewChangeEvent)
     */
    @Override
    public void enter(ViewChangeEvent event) {

    }

}

Ok, I found the solution/workarround for the issues : 好的,我找到了解决问题的方法/解决方案:

  • For the first issue : "token" seems to be a reserved word, "tokenEmployee" seems to be better and it's working perfectly... 对于第一个问题:“ token”似乎是一个保留字,“ tokenEmployee”似乎更好,并且运行良好...

  • For the second issue : I still don't know why it's not working in Eclipse, I've the source but in debug mode, I'm not able to see them. 对于第二个问题:我仍然不知道为什么它不能在Eclipse中工作,我拥有源代码,但是在调试模式下,我看不到它们。 I changed for IntelliJ. 我换了IntelliJ。 I was not able to find any thing about reserved words for the Vaadin Session. 我无法找到有关Vaadin会议保留字的任何信息。 If someone find a link or something, I'm very interested ! 如果有人找到链接或其他东西,我会很感兴趣!

Too much time trying to fix it.. 太多时间试图修复它。

A bit thanks to @jay who tried to help me! 感谢@jay试图帮助我!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM