2016-11-15 56 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