2016-11-15 42 views
0

我目前在openshift上托管我的vaadin应用程序。当点击登录按钮时,我的主要网站重定向到vaadin应用程序。用户看到的第一件事是登录页面重定向Vaadin应用程序

我在我的网站上有2个按钮,一个免费试用按钮和一个登录按钮,在我的vaadin应用程序中有两个不同的类,一个登录类和一个免费试用课程。

我如何使登录按钮重定向到我的vaadin应用程序的登录类,免费试用按钮重定向到我的vaadin应用程序的免费试用课程?

这是它目前的样子:

@Theme("mytheme") 
@Widgetset("com.example.myapp.MyAppWidgetset") 
@PreserveOnRefresh 
public class MyUI extends UI { 

    @WebServlet(urlPatterns = "/*", name = "MyUIServlet", asyncSupported = true) 
    @VaadinServletConfiguration(ui = MyUI.class, productionMode = false) 
    public static class MyUIServlet extends VaadinServlet { 
    } 

    @Override 
    protected void init(VaadinRequest vaadinRequest) { 
     login(); 
    } 
+0

我会倾向于在此处使用导航器。尽管你需要重构你的代码。 –

+0

@Chris M谢谢!你有任何洞察到如何实施这个为我的情况?我阅读了文档,但是我没有真正理解它。 如何使用它将www.trial.mywebsite.com重定向到trialUI类 并将www.login.mywebsite.com重定向到LoginUI类? – user3702643

回答

0

你可以在参数您的要求,并通过使用UI提供服务依赖于该参数的UI。

参见here

例子:

public class DifferentFeaturesForDifferentClients extends UIProvider { 

    @Override 
    public Class<? extends UI> getUIClass(UIClassSelectionEvent event) { 
     if ("trial".equalsIgnoreCase(event.getRequest().getParameter("app-type"))) { 
      return TrialUI.class; 
     } else { 
      return LognUI.class; 
     } 
    } 
} 

的web.xml:

<servlet> 
    <servlet-name>My Vaadin App</servlet-name> 
    <servlet-class>com.vaadin.server.VaadinServlet</servlet-class> 
    <init-param> 
     <description>Vaadin UI</description> 
     <param-name>UIProvider</param-name> 
     <param-value>com.example.myexampleproject.DifferentFeaturesForDifferentClients</param-value> 
    </init-param> 
</servlet> 

,然后在主网站的html代码:

<form> 
    <input type="submit" name="app-type" value="Trial" formaction="/your/vaadin/url" formmethod="post"> 
    <input type="submit" name="app-type" value="Login" formaction="/your/vaadin/url" formmethod="post"> 

    <!-- Alternative using button tag (better for customization, but no IE < 9) --> 
    <button name="app-type" value="trial" type="submit">Trial</button> 
    <button name="app-type" value="login" type="submit">Login</button> 
</form> 
+0

这似乎有道理,但我将如何传递参数给我的请求? – user3702643

+0

您可以在您的主网站中使用表单,该表单在使用按钮提交时传递POST参数,并将表单定位到您的vaadin应用程序的url。 – JDC

+0

相应地更新了答案 – JDC

0

您可以使用导航克里斯说,中号,或者你可以尝试实现一个事件驱动的体系结构。我使用Google Guava也使用了Vaadin这样的项目。

在这里你可以找到一些如何使用谷歌番石榴的例子,如果你有兴趣。 http://codingjunkie.net/guava-eventbus/

下面你可以找到我的执行一些代码片段:

// Using guava 
final Button signin = new Button(
        Lang.getMessage("login.signin"), 
        eventClick -> {BarsEventBus.post(new UserLoginRequestedEvent(
          username.getValue(), password.getValue()));}); 


//Using Navigator, I also used spring here, so the registration view is a Spring View 
final Button register = new Button(
        Lang.getMessage("login.registration"), 
        clickEvent -> {getUI().getNavigator().navigateTo(ViewToken.REGISTRO);} 
        ); 


@UIScope 
@SpringView(name = ViewToken.REGISTRO) 
public class RegistrationView extends VerticalLayout implements View {...} 

请确认为vaadin演示应用程序的代码。该代码也有一个如何处理它的例子。在这里找到它: https://github.com/vaadin/dashboard-demo

0

我现在明白你在做什么。你可以很容易地做到这一点。您需要将VaadinRequest转换为VaadinServletRequest。它传递VaadinRequest而不是VaadinServletRequest的原因是您可以将应用程序部署为一个portlet。在这种情况下,您需要将其转换为VaadinPortletRequest。然后你可以使用用户提供的serverName。

@Override 
    protected void init(VaadinRequest vaadinRequest) { 
     VaadinServletRequest req = (VaadinServletRequest) vaadinRequest; 
     String serverName = req.getServerName(); 
     if (serverName.equals("www.login.mywebsite.com")) 
     { 
      login(); 
     } 
     else 
     { 
      trial(); 
     } 
    } 
+0

这几乎是我想要的,但我正在将我的应用程序从openshift重定向到login.mywebsite.com。你提供的代码给了我openshift的url,而不是login.mywebsite.com。你有这个工作吗? – user3702643