如何从 Play 2 中的视图模板在路由中定义多个值
How to define multiple values in routes from a view template in Play 2
在我的 Play 2.4.4 项目中,我有一个视图模板,我想要 return 2 个值,一个 Long 和一个 String。
我有一个按钮可以在我的控制器中调用适当的方法:
<p>
<a href="@controllers.routes.Orders.rollbackStatus(order.id, order.status.toString)"
class="btn">@Messages("orders.rollback")</a>
</p>
我的控制器中的方法调用模型对象上的函数:
def rollbackStatus(id: Long, status: String) = Action {
Order.demoteStatus(id, status)
Redirect(routes.Orders.list())
}
在我的路由文件中,我定义了 HTTP 方法、URI 和控制器方法:
GET /orders/:id/:status controllers.Orders.rollbackStatus(id: Long, status: String)
当我按下按钮时,我收到以下消息:
BAD REQUEST
For request 'GET /orders/3,PLACED' [Cannot parse parameter id as Long: For input string: "3,PLACED"]
我已经成功地以同样的方式成功地传递了单个值。
这是为 /orders 定义的其余路由:
GET /orders controllers.Orders.list
GET /orders/pickorder controllers.Orders.getOrder
GET /orders/:id controllers.Orders.show(id: Long)
GET /orders/:id/:status controllers.Orders.rollbackStatus(id: Long, status: String)
好吧,我找到了答案,当您指定第一个参数时,您使用“/:”来定义参数的名称。
所有后续参数仅以“/”分隔。
我认为这是因为“:”指定了参数的开始,但这在 Play 文档中并不清楚
这是更正后的路线列表,所有其他代码均与问题中提供的代码相同:
GET /orders controllers.Orders.list
GET /orders/pickorder controllers.Orders.getOrder
GET /orders/:id controllers.Orders.show(id: Long)
GET /orders/:id/status controllers.Orders.rollbackStatus(id: Long, status: String)
在我的 Play 2.4.4 项目中,我有一个视图模板,我想要 return 2 个值,一个 Long 和一个 String。
我有一个按钮可以在我的控制器中调用适当的方法:
<p>
<a href="@controllers.routes.Orders.rollbackStatus(order.id, order.status.toString)"
class="btn">@Messages("orders.rollback")</a>
</p>
我的控制器中的方法调用模型对象上的函数:
def rollbackStatus(id: Long, status: String) = Action {
Order.demoteStatus(id, status)
Redirect(routes.Orders.list())
}
在我的路由文件中,我定义了 HTTP 方法、URI 和控制器方法:
GET /orders/:id/:status controllers.Orders.rollbackStatus(id: Long, status: String)
当我按下按钮时,我收到以下消息:
BAD REQUEST For request 'GET /orders/3,PLACED' [Cannot parse parameter id as Long: For input string: "3,PLACED"]
我已经成功地以同样的方式成功地传递了单个值。
这是为 /orders 定义的其余路由:
GET /orders controllers.Orders.list
GET /orders/pickorder controllers.Orders.getOrder
GET /orders/:id controllers.Orders.show(id: Long)
GET /orders/:id/:status controllers.Orders.rollbackStatus(id: Long, status: String)
好吧,我找到了答案,当您指定第一个参数时,您使用“/:”来定义参数的名称。
所有后续参数仅以“/”分隔。
我认为这是因为“:”指定了参数的开始,但这在 Play 文档中并不清楚
这是更正后的路线列表,所有其他代码均与问题中提供的代码相同:
GET /orders controllers.Orders.list
GET /orders/pickorder controllers.Orders.getOrder
GET /orders/:id controllers.Orders.show(id: Long)
GET /orders/:id/status controllers.Orders.rollbackStatus(id: Long, status: String)