2

I am Learning Vaadin Flow (Vaadin v10) and am having difficulty with the concepts of Route (@Route as annotation) and RouterLayout.

From the documentation that I have already read on Vaadin's website! I could not identify an answer to a particular question.

Can a class implementing RouterLayout be also defined as a @Route?

@Route("")
public class MainLayout extends Div implements RouterLayout {
}

Please (if possible) provide a reference to some documentation. As far as I understand a RouterLayout is intended to render the views in it. Therefore if necessary public void showRouterLayoutContent(HasElement content) method could be Overridden for desired layout creation. Thus placing @Route annotation which essentially defines a view does not make sense to be mixed with a RouterLayout?

Or does it? If RouterLayout can be used with @Route annotation, suggesting that layout is also a view; Can some provide me a simple scenario where can this be useful?

office.aizaz
  • 179
  • 11

1 Answers1

0

It's like you said - on a conceptual level, it seems weird to have both @Route and RouterLayout on the same class. @Route annotations are placed on leaf nodes. You can have a standalone @Route, which will be placed in the <body> element or you can have a nested layout where the route is displayed inside one or more parent layouts. The parent layout can be specified with the second parameter of the annotation, like with

@Route(value = "users", layout = Dashboard.class)
public class Users extends Div {
//...

, where Dashboard needs to implement RouterLayout. So you would access this route with http://server-address-here/users and it would display Users inside Dashboard.

If you wanted to have a more descriptive URL, you could implement that with the @RoutePrefix annotation on Dashboard:

@RoutePrefix("dashboard")
public class Dashboard extends VerticalLayout implements RouterLayout { 
// ...

and now you'd reach the Users view from http://server-address-here/dashboard/users while things would remain otherwise the same as before.

ollitietavainen
  • 3,900
  • 13
  • 30
  • thank you for the clarification. Could you kindly point me to Vaadin official Documentation/Tutorial (anything as such) which clearly states that there is no point of using `@Route` annotation when a class implements `RouterLayout`. – office.aizaz Apr 01 '19 at 10:21
  • I'm not sure such documentation exists. Why do you need it? – ollitietavainen Apr 02 '19 at 07:25
  • We are in the process of upgrading Vaadin7 code to Vaadin10. A lot of code has been migrated in a way that does not adhere to separation of concerns between Layouts and Views. Since no documentation exists that clearly states that Layout and not really views, it has become very difficult to argue for establishing a clear architecture for the new application. I accept your answer here and thank you for taking time. However it would be great if such a concept is clearly mentioned in the official documentation. :-) – office.aizaz Apr 02 '19 at 10:41