Grails不匹配url模式与可选参数



在Grails 2.3.7中,假设以下url映射…

"/foo/$number?"     (controller:'test', action:'actionOne')  
"/foo/$number?/bar" (controller:'test', action:'actionTwo')

. .和控制器:

class TestController {
   def actionOne(){ render "Action_ONE: ${params.number}"  }
   def actionTwo(){ render "Action_TWO: ${params.number}" }
}

好…

/foo/1      //prints 'Action_ONE: 1'
/foo/2/bar  //prints 'Action_TWO: 2'
/foo//bar   //prints 'Action_TWO: null'
/foo//      //prints 'Action_ONE: null'

…但

/foo   //prints 'Action_TWO: null'
/foo/  //prints 'Action_TWO: null'

如果/foo/$number?number是可选的,为什么/foo/foo/映射到actionTwo() ?

"/foo/$number?"     (controller:'test', action:'actionOne')  
"/foo/$number?/bar" (controller:'test', action:'actionTwo')

你在url中定义number为可选的,因为你使用了"?"。将

中的"$number?"改为"$number"
 "/foo/$number?/bar" (controller:'test', action:'actionTwo')

第二行,应该可以正常工作。

这里是url映射的详细信息https://grails.org/version/URL+mapping/8

最新更新