In my application I need to register 2 different routes withe the same class.
I did it but navigating to both routes I get the same copy of my class and this is what I don’t want.
I use Vaadin 23 and Spring.
How can I solve ?
Tks
I’m not 100% sure I understand your question but assuming that what you want is for a View to be re-rendered on every navigation event, instead of getting the same instance again, you need to take a look at Scopes: How to use Spring scopes in Vaadin applications
Specifically, @RouteScoped.
Sounds like a @RouteAlias to me.
I annotated my View Class as @RouteScope and I registred that class with 2 different routes (say R1 and R2) using
RouteConfiguration routeConfiguration = RouteConfiguration.forApplicationScope(); | |||
---|---|---|---|
routeConfiguration.setRoute(xpPath, DynamicPage.class, getPaxletContainerClass()); | |||
But my class is created only once and the same object is used for both routes. |
If understand right @RouteAlias is usefull to provide 2 different names at the same object, but I need to have 2 differente names for 2 different copies of the same class.
I’m not familiar with dynamic route configuration, so I can’t help with it. My workaround - if RouteAlias is not feasible - would be B extends A and C extends A where A holds 100% of the route’s code
Good Idea but I don’t know how many A, B, C I’ve.
Any suggestion ?
Furthermore the bean ?im speaking about is the View itself.
What if instead of trying to make multiple route urls to map to the same view, you have a single route with parameters and you reroute the user to a different view based on the parameter value?
Nice idea but this prevents the possibility to open that view 2 times with different parameters using different routes.
Unlikely the router seems to look the presence of a view by class and not by route.
Nice idea but this prevents the possibility to open that view 2 times with different parameters using different routes.
I don’t see how? If you want to navigate to a/foo
, a/bar
, b/baz
and b/quux
so that a
and b
are the “routes” and the parts after the /
are the “route parameters”, you should still be able to do that by having a single parameterized route where you parse the parameters and reroute accordingly.
Tke.
I already pass parameters as in
/sale-app/funzionedinamicautente_DYNAMIC_funzionedinamicautente?parameterIndex=1
but the class routed by my route is created only once and this could create errors if I’m not wrong.
I verified also using root parameters but nothing changed.
The problem is even wrost because the view is removed from the originanal container and moved to the second one.
It seems that in order to have different routes You are forced to have different classes.
Till now I didn’t find the way to obtain 2 different istances of the same view class for 2 different routes.
A component instance cannot be attached to multiple parents. Thus you can’t just take an existing component instance and add it to new layout, it will be removed from the previous. You need a new component instance.
Router itself when setup correctly will instantiate a new instance of the view component when navigated into it. This works also when you use parameters. I.e. when you open a new browser tab with the same route but different parameter, Router will create a new instance of the view.
I suggest that you are not trying to re-invent the wheel here.
I agree with You, I don’t want ro re-invent the wheel, however it doesn’t work.
I created 2 routes (I define them at runtime as already shown) with the same view class.
When I try to show the first route the instance of the class is created and used.
When I try to show the second one the instance is not created but used and this don’t work.
Debugging the Router I found a piece of code where the router ask himself if exists an instance of that class and, if present, provide it without creating a new one.
I use Spring but it doesn’t seem to have any relation with the problem.
I there’s a setting of Router that can solve the issue, please inform me.
Tks
We probably need a code example to understand where your problem is. Re-using the instance sounds like no big deal for me. Just clear your route in beforeEnter and re-create it.
Unlikely using the same instance is a problem in my environment because both views are shown in 2 different tabs of a tabsheet and should be visible both at same time.
That sounds like an odd way to use a Tabsheet; it feels like you’re trying to use the wrong tool for the job, but it’s really hard to say without seeing more of the code and understanding the wanted behavior.
Provide You the code is a huge problems because of the code dimension and create a testcase is not easy and it would not reproduce the real problem.
You told “an odd way to use tabsheet” : may be.
My application has a menu bar each menu items is a route.
Each route should be shown below the menubar in a tabsheet (a tab for each route).
Can You suggest a “non odd” way to create such presentation ?
Tks