无法在 Vaadin 中使用 RouterLink 传递多个路径参数。

huangapple go评论76阅读模式
英文:

Can't pass several path parameters in Vaadin using RouterLink

问题

我对Vaadin还不熟悉,正在努力理解如何使视图从URL中获取多个参数。
例如

http://www.some.com/book/18/page/41

其中的18和41是参数。

我发现我可以实现HasUrlParameter<T>,然后使用setParameter方法,但它只能用于一个参数。

英文:

I'm new to Vaadin and trying to understand how to make View to get several parameters from URL.
For example

http://www.some.com/book/18/page/41

Numbers 18 and 41 are parameters.

I've found that I can implement HasUrlParameter&lt;T&gt; and then use setParameter method, but it can be used only for one parameter.

答案1

得分: 1

你的setParameter方法中是否使用了@WildcardParameter通配符URL参数

假设greet(在你的情况下是book)是路由,那么下面的代码会设置18\page\41。由于它是一个字符串,你需要解析它并提取你需要的值,但是值是存在的。

@Route("greet")
public class WildcardGreeting extends Div
        implements HasUrlParameter<String> {

        @Override
        public void setParameter(BeforeEvent event,
                @WildcardParameter String parameter) {
                if (parameter.isEmpty()) {
                        setText("欢迎匿名用户。");
                } else {
                        setText(String.format(
                                "处理参数 %s。",
                                parameter));
                }
        }
}

附言:与问题无关,但看你的URL,也许查询参数更适合你 查询参数

英文:

Are you using @WildcardParameter in your setParameter method? Wildcard URL parameters

Assuming that greet (The book in your case) is the route, then the code below sets 18\page\41. Since it's a string you would need to parse it and extract values you need, but the value is there.

@Route(&quot;greet&quot;)
public class WildcardGreeting extends Div
        implements HasUrlParameter&lt;String&gt; {

        @Override
        public void setParameter(BeforeEvent event,
                @WildcardParameter String parameter) {
                if (parameter.isEmpty()) {
                        setText(&quot;Welcome anonymous.&quot;);
                } else {
                        setText(String.format(
                                &quot;Handling parameter %s.&quot;,
                                parameter));
                }
        }
}

P.S. Not related to the question, but looking at your URL, could it be that query parameters suit you better Query parameters?

答案2

得分: 1

在Vaadin的Java视图中,尚不支持内建的多参数设置。您可以使用@WildcardParameter注解对参数进行标注,以便将多个路径段捕获到一个参数中。然后,您需要手动管理该值的内容 - 在生成URL时连接字符串并在setParameter中解析字符串。

目前正在努力开发支持多参数的功能,但工作尚未完成。目前尚不清楚Vaadin的哪个未来版本将拥有此功能,但我现在的猜测是可能是版本14.3或14.4。

英文:

There is no built-in suppor for having multiple parameters for Java views in Vaadin. What you can do is to annotate the parameter with @WildcardParameter so that multiple path segments can be captured into one parameter. You would then have to manually manage the contents of that value - concatenating strings when generating URLs and parsing strings in setParameter.

Support for multiple parameters is being worked on right now, but the work is not yet completed. It is not yet clear which future version of Vaadin will get this feature, but my guess right now is that it would be either version 14.3 or 14.4.

答案3

得分: 1

似乎 Vaadin 14 已经进行了更新,并且支持了多个路径参数。

示例:

@Route("user/:userID/:messageID/edit")
public class UserProfileEdit extends Div implements BeforeEnterObserver {

    private String userID;
    private String messageID;

    @Override
    public void beforeEnter(BeforeEnterEvent event) {
        userID = event.getRouteParameters().get("userID").get();
        messageID = event.getRouteParameters().get("messageID").get();
    }
}

来源:https://vaadin.com/docs/v14/flow/routing/tutorial-router-templates

英文:

It seems like Vaadin 14 has got an update and got support for multiple path parameters.

Example:

@Route(&quot;user/:userID/:messageID/edit&quot;)
public class UserProfileEdit extends Div implements BeforeEnterObserver {

    private String userID;
    private String messageID;

    @Override
    public void beforeEnter(BeforeEnterEvent event) {
        userID = event.getRouteParameters().get(&quot;userID&quot;).get();
        messageID = event.getRouteParameters().get(&quot;messageID&quot;).get();
    }
}

Source: https://vaadin.com/docs/v14/flow/routing/tutorial-router-templates

答案4

得分: 0

一个简单的示例与解决方案

    @Route("book")
    public class BookView extends Div implements HasUrlParameter<String> {
       
            @Override
            public void setParameter(BeforeEvent event, @WildcardParameter String parameter) {
                if (!parameter.isEmpty()) {
                    String params[] = parameter.split("/");
                    if (params.length == 1) {
                        // 做一些事情..
                    } else if (params.length == 2) {
                        // 做另一件事情..
                    } else {
                        // 做其他事情
                    }
                }
            }
    }

可以像这样创建链接

    new RouterLink("无参数", BookView.class);
    new RouterLink("一个参数", BookView.class, "18");
    new RouterLink("两个参数", BookView.class, "18/edit");
英文:

A simple example with the solution

@Route(&quot;book&quot;)
public class BookView extends Div implements HasUrlParameter&lt;String&gt; {
   
        @Override
        public void setParameter(BeforeEvent event, @WildcardParameter String parameter) {
            if (!parameter.isEmpty()) {
                String params[] = parameter.split(&quot;/&quot;);
                if (params.length == 1) {
                    // Do something ..
                } else if (params.length == 2) {
                    // Do another thing ..
                } else {
                    // Do something else
                }
            }
        }
}

The link can be created like this:

new RouterLink(&quot;No params&quot;, BookView.class);
new RouterLink(&quot;One param&quot;, BookView.class, &quot;18&quot;);
new RouterLink(&quot;Two param&quot;, BookView.class, &quot;18/edit&quot;);

huangapple
  • 本文由 发表于 2020年4月8日 02:59:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/61087441.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定