条纹 URL 前缀作为请求参数

发布于 2024-10-14 17:58:53 字数 691 浏览 3 评论 0原文

我正在尝试为我的应用程序构建一种方法,使其具有类似于 Twitter 等应用程序的 URL 模式/方案。例如 myapplication.com/username,其中用户名是被视为参数的实际用户名。该参数将用于显示用户相关页面。我的应用程序也有这个 URL 方案,其中用户名是其他资源的前缀,例如 myapplication.com/username/accountsettings 或 myapplication.com/username/profile。用户名充当某种子上下文路径。

正如标题所示,我使用的是 Stripes。我现在要做的是创建一个自定义 ActionResolver,在其中从 URL 绑定中取出第一个非空字符串(在本例中为用户名),因为我的 ActionBeans 不考虑 URL 前面的用户名。例如,处理 myapplication.com/username/accountsettings 的操作 bean 仅绑定到 /accountsettings。

当我尝试使用自定义 ActionResolver 作为 Stripes 过滤器的 ActionResolver.Class 初始化参数时,它似乎不起作用。看起来它仍然使用默认的 ActionResolver。我该如何改变这个?

另外,有没有更简单/更直观的方法来做到这一点?我对 Stripes 和 URL 绑定不是很熟悉,那么 Stripes 中是否有一种工具可以让我在不扩展/更改框架组件的情况下执行此操作?

谢谢。

I'm trying to build a way for my application to have a URL pattern/scheme like that of applications like Twitter. For example myapplication.com/username where the username is an actual username treated as a parameter. This parameter will be used to display the user related page. My application also has this URL scheme where the username is a prefix for other resources e.g. myapplication.com/username/accountsettings or myapplication.com/username/profile. The username acts some sort of sub-context path.

As the title suggests, I'm using Stripes for it. What I'm doing now is create a custom ActionResolver where I take out the first non-empty string from the URL binding (the username in this case) since my ActionBeans do not take into account the username prepended to the URL. So for example, the action bean that handles myapplication.com/username/accountsettings is only bound to /accountsettings.

When I tried to use the custom ActionResolver as ActionResolver.Class init param for the Stripes filter, it doesn't seem to be working. It seems like it's still using the default ActionResolver. How do I alter this?

Also, is there an easier/more intuitive way to do this? I'm not very familiar with Stripes and URL bindings so is there a facility in Stripes that would allow me to do this without extending/altering the framework components?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

止于盛夏 2024-10-21 17:58:53

我同意卢卡斯的观点,URL映射应该由 UrlBinding 完成注解。没有必要开始搞乱 URL 重新映射过滤器,这只会破坏 Stripes 系统为您生成正确的 URL。

它应该是这样的:

@UrlBinding("{username}/profile/")
public class MyProfileAction implements ActionBean {
    private ActionBeanContext context;
    private String username;

    public ActionBeanContext getContext() {
        return context;
    }

    public void setContext(ActionBeanContext context) {
        this.context = context;
    }

    public void setUsername(String username) {
        this.name = name;
    }

    public String getUsername() {
        return username;
    }

    @DefaultHandler
    public Resolution view() {
        return new ForwardResolution(“/WEB-INF/MyProfile.jsp”);
    }
}

I agree with lucas, URL mappings should be done by UrlBinding annotation. There is no need to start messing around with URL remapping filters, that will only break the Stripes system in generating correct URL’s for you.

This is how it should look like:

@UrlBinding("{username}/profile/")
public class MyProfileAction implements ActionBean {
    private ActionBeanContext context;
    private String username;

    public ActionBeanContext getContext() {
        return context;
    }

    public void setContext(ActionBeanContext context) {
        this.context = context;
    }

    public void setUsername(String username) {
        this.name = name;
    }

    public String getUsername() {
        return username;
    }

    @DefaultHandler
    public Resolution view() {
        return new ForwardResolution(“/WEB-INF/MyProfile.jsp”);
    }
}
鹿! 2024-10-21 17:58:53

我偶然发现了 URL 重写过滤器,到目前为止它似乎满足我的应用程序的需求。

I stumbled upon URL Rewrite Filter and it seems to fit the needs of my application so far.

黎夕旧梦 2024-10-21 17:58:53

Stripes 确实内置了这个功能。它称为 @URLBinding 注释,它包含在 Stripes 1.5 中。 这里有一些文档,并且Stripes 书 IIRC 中有更多内容。

@UrlBinding("/foo/{bar}/{baz}") 地图
对“/foo”的操作并指示
“bar”和“baz”参数
可以嵌入到 URL 中。在这个
在这种情况下,URL /foo/abc/123 将
调用将 bar 设置为的操作
“abc”和 baz 设置为“123”。

Stripes does have this built in. It's called the @URLBinding annotation, and it was included in Stripes 1.5. There is some documentation here, and there was more in the Stripes book IIRC.

@UrlBinding("/foo/{bar}/{baz}") maps
the action to "/foo" and indicates
that the "bar" and "baz" parameters
may be embedded in the URL. In this
case, the URL /foo/abc/123 would
invoke the action with bar set to
"abc" and baz set to "123".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文