Java - 获取 URL 的参数
Java - Getting the URL's parameter
我有一个URL像这样的东西
我正在使用 play 框架
HTTP 路由:
GET /students controllers.Application.Get()
POST /students controllers.Application.Post()
PUT /students/{studentNo} controllers.Application.Put()
如何获得 {studentNo} 值?例如,我正在使用邮递员(休息客户端),我在 url http://localhost:9000/students/2012111222 中输入了一些内容,我将如何获得“2012111222”。我是否需要拆分 URL 的值只是为了获得该参数,还是有其他东西。谢谢!
做这样的事情:
如果你有这样的字符串:
String s="POST /students/12 controllers.Application.Post()";
您可以像这样检索“12”:
int studentNo = Integer.parseInt(s.split("\s+|,\s*|\.\s*")[1].split("/")[2]);
正如您在 Play's routing documentation 中看到的那样,您可以使用冒号语法来定义路由 URL 的某些部分是一个变量,并将该变量传递给控制器方法,即:
POST /students/:studentNo controllers.Application.Post(studentNo: Long)
您不能使用 /students/:studentNo
,应在 POST 请求中使用 /students?number=111
并使用 DyanamicForm
或 request().getQueryString("number")
获取值,而无需提及controllers.Application.Post(studentNo: Long)
我有一个URL像这样的东西
我正在使用 play 框架
HTTP 路由:
GET /students controllers.Application.Get()
POST /students controllers.Application.Post()
PUT /students/{studentNo} controllers.Application.Put()
如何获得 {studentNo} 值?例如,我正在使用邮递员(休息客户端),我在 url http://localhost:9000/students/2012111222 中输入了一些内容,我将如何获得“2012111222”。我是否需要拆分 URL 的值只是为了获得该参数,还是有其他东西。谢谢!
做这样的事情:
如果你有这样的字符串:
String s="POST /students/12 controllers.Application.Post()";
您可以像这样检索“12”:
int studentNo = Integer.parseInt(s.split("\s+|,\s*|\.\s*")[1].split("/")[2]);
正如您在 Play's routing documentation 中看到的那样,您可以使用冒号语法来定义路由 URL 的某些部分是一个变量,并将该变量传递给控制器方法,即:
POST /students/:studentNo controllers.Application.Post(studentNo: Long)
您不能使用 /students/:studentNo
,应在 POST 请求中使用 /students?number=111
并使用 DyanamicForm
或 request().getQueryString("number")
获取值,而无需提及controllers.Application.Post(studentNo: Long)