0

I have a view:

@SpringUI(path="order")
@Title("order")
public class OrderGUI extends UI{

and I would pass parameter to other:

@SpringUI(path="orderNumber")
@Title("Order number")
public class GetOrderNumber extends UI {

I tried to send parameter(orderNumber) by:

getUI().getPage().setUriFragment(orderNumber);
getUI().getPage().setLocation("orderNumber");

then it goes to /orderNumber but when tried to catch it by:

String fragment = getPage().getUriFragment();

but

System.out.println("Fragment: " + fragment);

says that Fragment: null

How to send parameter from one Vaadin view to another?

@Edit It is close to work. I have changed my View to:

@SpringView(name = "GetOrderNumber")
@Title("Order number")
public class GetOrderNumber extends VerticalLayout implements View {

and in @SpringUI made getUI().getNavigator().navigateTo("GetOrderNumber/" + orderNumber); it actually still throws java.lang.IllegalArgumentException: Trying to navigate to an unknown state '' and an error view provider not present but it works. I mean all the time when I go to localhost:8080/order the

@SpringUI(path="order")
public class OrderGUI extends UI {

throws an error but then works as expected - go to view and pass parameter right. I have no idea why navigator = new Navigator(this, this); cause an error

Tatu Lund
  • 9,949
  • 1
  • 12
  • 26
Michu93
  • 5,058
  • 7
  • 47
  • 80

1 Answers1

2

@SpringUI annotation defines always just the name of the view, it is not the place where you give the URI parameter, the correct place is the navigateTo(..) method, e.g.:

getUI().getNavigator().navigateTo("myview/someparameter");

There is more information about Vaadin and Spring add-on here

https://vaadin.com/docs/v8/framework/advanced/advanced-spring.html

Tatu Lund
  • 9,949
  • 1
  • 12
  • 26
  • First shot - changed to `getUI().getNavigator().navigateTo("orderNumber/" + orderNumber);` and got `java.lang.NullPointerException: null` – Michu93 Apr 15 '18 at 13:16
  • Not even strange if I don't have object of Navigator class – Michu93 Apr 15 '18 at 13:35
  • 1
    Yes, you need to setup navigator first. Usually you do it in in main UI class in small apps or mainview/layout/whatever class in bigger apps. The documentation of Navigator is here: https://vaadin.com/docs/v8/framework/advanced/advanced-navigator.html – Tatu Lund Apr 15 '18 at 13:38
  • 1
    Also note that views should be annotated with `@SpringView` and extend the `com.vaadin.navigator.View` interface, whereas the example code here is using the `@SpringUI` annotation and extends the `com.vaadin.ui.UI` class. There should typically only be one `UI` class per application, but multiple `View` classes. – Leif Åstrand Apr 16 '18 at 06:50
  • I followed documentation linked by @TatuLund and as far as I add `navigator = new Navigator(this, this); navigator.addView("", new GetOrderNumber());` My site is blank. Why is that? @Edit actually `navigator.addView("", new GetOrderNumber());` makes my site blank – Michu93 Apr 19 '18 at 10:01
  • 1
    @Michu93 If you want to pass order number to the view as the parameter, you should not give it in addView(..) method. Instead you should do it this way navigator.addView("myview"); navigator.navigateTo("myview/"+GetOrderNumber()); – Tatu Lund Apr 20 '18 at 05:48
  • @Michu93 Sorry, I thought GetOrderNumber() is getter for your parameter, but then I noticed that it is a view. You should register your view with this syntax: navigator.addView("myview", GetOrderNumber.class); And when you navigate to "myview" and pass parameter to it you do it with navigator.navigateTo("myview/"+orderNumber); – Tatu Lund Apr 20 '18 at 05:57
  • GetOrderNumber is so dumb name for a class. When I add `navigator = new Navigator(this, this); navigator.addView("myview", GetOrderNumber.class);` it immediately throw `java.lang.IllegalArgumentException: Trying to navigate to an unknown state '' and an error view provider not present ` – Michu93 Apr 21 '18 at 07:19
  • Actually `navigator = new Navigator(this, this);` throws java.lang.IllegalArgumentException. – Michu93 Apr 21 '18 at 08:01
  • Have you added navigator.navigateTo("myview"); call? If you do not have a view with name "" registered, your will get exception. So then you need to add navigation to default view. – Tatu Lund Apr 22 '18 at 16:06