我修改了某个Grails Controller
的默认网址映射:
BankRemittanceController.groovy
class BankRemittanceController{
def view(String bankCode, String refNo) {
...
}
}
UrlMappings.groovy
class UrlMappings {
...
/*
* Will accomodate the following URLs
*
* /project/remittance/bank/
* /project/remittance/bank/hsbc/
* /project/remittance/bank/hsbc/8959-87s-777-EI-02
*/
"/remittance/bank/$bankCode?/$refNo?"{
controller={"bankRemittance"}
action=[GET:"show"]
}
...
}
我已经测试过这些网址并且有效。问题是当我尝试在.gsp
上动态生成URL字符串时,它会生成默认URL:
<script>
var redirectURL = "${g.createLink(controller: "remittance", action: "view")}"
// The URL generated here is still:
// /project/bankRemittance/view
//
// instead of the mapped URL in UrlMappings.groovy:
// /project/remittance/bank
//
</script>
如何生成预期的URL字符串?
答案 0 :(得分:0)
我不明白为什么在制图时你会调用非现有的动作&#39; show&#39; 。 我已将其更改为:action = [GET:&#34; view&#34;]
如果您的预期网址是:/ project / remittance / bank, 您可以使用:$ {g.createLink(控制器:&#34;汇款&#34;,操作:&#34;银行&#34;)} 或:$ {g.createLink(uri:&#34; / remittance / bank&#34;)}
答案 1 :(得分:0)
首先,更改已定义映射的声明。从
"/remittance/bank/$bankCode?/$refNo?"{
controller={"bankRemittance"}
action=[GET:"show"]
}
将其设为:
name mappingName: "/remittance/bank/$bankCode?/$refNo?"{
controller={"bankRemittance"}
action=[GET:"show"]
}
然后在controller
上声明action
时使用createLink() mapping
attribute代替.gsp
和<script>
var redirectURL = "${g.createLink(mapping: "mappingName")}"
</script>
:
bankCode
如果您想传递两个可选参数(refNo
和params
),请使用createLink()<script>
var redirectURL = "${g.createLink(mapping: "mappingName",
params: [bankCode: "...", refNo: "...")}"
</script>
属性:
<form action="" method="post">
<input name="email" type="text" placeholder="<?php if(isset($_GET['email'])) echo $_GET['email']; ?>" value="<?php if(isset($_GET['email'])) echo $_GET['email']; ?>" disabled>
</form>