Playframework 在Play框架中有没有一种方法可以将一个url映射到另一个url
我正在使用Play Framework 2.4.3。我想更改routes文件中的一些URL,但结果是有一个数据库表包含一列URL。(向用户显示警报列表,每个警报都有一个指向警报应用页面的链接。) 我可能能够用新的URL更新数据库中的数据,或者在运行时转换它们,但我认为一个可能更简单的解决方案是以某种方式将旧的URL映射到新的URL。例如,我的routes文件如下所示:Playframework 在Play框架中有没有一种方法可以将一个url映射到另一个url,playframework,Playframework,我正在使用Play Framework 2.4.3。我想更改routes文件中的一些URL,但结果是有一个数据库表包含一列URL。(向用户显示警报列表,每个警报都有一个指向警报应用页面的链接。) 我可能能够用新的URL更新数据库中的数据,或者在运行时转换它们,但我认为一个可能更简单的解决方案是以某种方式将旧的URL映射到新的URL。例如,我的routes文件如下所示: #this is the new url GET /views/producers/:partyId controllers
#this is the new url
GET /views/producers/:partyId controllers.ProducerController.viewProducer(partyId:Integer)
#this is the old url
GET /producer/view/:partyId controllers.ProducerController.viewProducer(partyId:Integer)
#this is the new url
GET /views/producers/:partyId controllers.ProducerController.viewProducer(partyId:Integer)
#this is the old url
GET /producer/view/:partyId controllers.ProducerController.oldViewProducer(partyId:Integer)
表中的值如下所示:
#this is the new url
GET /views/producers/:partyId controllers.ProducerController.viewProducer(partyId:Integer)
#this is the old url
GET /producer/view/:partyId controllers.ProducerController.viewProducer(partyId:Integer)
#this is the new url
GET /views/producers/:partyId controllers.ProducerController.viewProducer(partyId:Integer)
#this is the old url
GET /producer/view/:partyId controllers.ProducerController.oldViewProducer(partyId:Integer)
/制片人/视图/123
我可以通过在routes文件中保留这两个条目来实现。反向路由将选择第一个,两个链接都可以工作。但我不希望控制器功能被列出两次(并冒着失去同步的风险),我也希望用户在浏览器中看到相同的url,不管他们是如何到达的
有没有办法将旧url映射到新url?在数据库中保存url似乎不是一个好主意,但您可以将旧url映射到新url,如下所示:
#this is the new url
GET /views/producers/:partyId controllers.ProducerController.viewProducer(partyId:Integer)
#this is the old url
GET /producer/view/:partyId controllers.ProducerController.viewProducer(partyId:Integer)
#this is the new url
GET /views/producers/:partyId controllers.ProducerController.viewProducer(partyId:Integer)
#this is the old url
GET /producer/view/:partyId controllers.ProducerController.oldViewProducer(partyId:Integer)
其中oldViewProducer方法重定向到新路由:
public static Result oldViewProducer(Integer partyId) {
return redirect(routes.ProducerController.viewProducer(partyId));
}
我建议你更新数据库中的URL,因为没有“干净”的方法通过更改路由文件来解决这个问题。我希望有更好的方法,但我想这是唯一的方法。它确实解决了向用户显示正确url的问题。