带有Playframework的虚荣URL

n00*_*13f 1 java playframework vanity-url

我想为构建在Playframework上的 Web应用程序创建虚荣URL.

我试图index在主页面中为方法添加一个参数,Application如果我打电话,这将有效,http://localhost:9000/?vanity_url而不是http://localhost:9000/vanity_url.

我怎么做到这一点?我希望用户创建自己的网址,解决方案必须是动态的,就像开始一样Facebook

all*_*skd 7

在你要放的路线上

GET /{<[a-z]+>fanPage}/? Page.showFanPage

有了这个你可以获得:

http://localhost:9000/facebook

我还建议使用slugifyplay框架提供的方法,通常你会检查slug是否存在于数据库中,并查找slug来获取数据(fanpage的标题/粉丝页面的所有者/有多少观看次数)/etc等)

在表格中:

名称:fanpage

pageID integer或bigint
title varchar
slug varchar唯一
所有者整数
内容文本

在代码中:

public static void showFanPage(String fanPage) {
    // use models to look for the slug
    // grab the data
    // do what you need to do
}
Run Code Online (Sandbox Code Playgroud)

我在这里举例说明如何创建网址,因为我不知道你正在构建什么应用程序:
GET /post/slug/? Page.createPage
POST /post/slug/? Page.processPage

public static void createPage() {
         // remember to create the template
         render();
}

public static void processPage(String slugName) {
        // slugName refers to the input field
        // check if the slug exists and throw error if it does
        // process
}
Run Code Online (Sandbox Code Playgroud)

(注意这只是一个例子,我不知道你正在构建什么样的应用程序)我希望这有帮助,让我知道这是否是你的意思