开发者

How to create canocical from controller in grails?

In grails, I have a link like /myapp/questions/all

The all is a parameter (all, replied, ...) passed to my controller.

I have a form to search question depending of type : in all, in replied, ... In the search form, I have an hidden field to pass parameter.

But the url displayed is /myapp/questions/ ans not /myapp/questions/all

So I tried with url : url="[action:'question', controller:'mycontroller', params:['monparam':'${mavariable}']]"

but it's not working.

Any idea ?

开发者_开发百科

Thanks


You can do it like this:

class UrlMappings {

    static mappings = {
        name nameOfTheMapping: "/question/$para/" {
            controller = "mycontroller"
            action = "question"
        }
    ...

Then you can access the mapping by:

<a href='${createLink(mapping: 'nameOfTheMapping', params: [para: para.encodeAsUrl()])}' title='test'>Test</a>

The above code is created in my taglib, so it maybe a little different if you want to use it in a view.


I don't completely understand your question, but it seems you are not following the grails convention. the url is of the form

/app/controller/action

so grails is interpreting the 'all' part of your url as the action to invoke on the questions controller (what I got from your 'link like /myapp/questions/all').

Where I got confused was with your url specification.

 url="[action:'question', controller:'mycontroller', params:['monparam':'${mavariable}']]"

Based on that, you should have a controller called 'mycontroller', with an action called 'question' on it. The url you will see in the browser would be

/app/mycontroller/question?monparam:whatever

See here for details on controllers in general.


You need to edit grails-app/conf/UrlMappings.groovy and create a mapping to the controller that omits the action. (since you're handling this all within one action)

something like

"/questions/$question_type" (controller: 'questions', action: 'your_action')

where "your_action" is the name of the action that is processing these requests.

Then in QuestionsController.groovy:

def your_action = {
    // use question_type as needed
    def questions = Questions.findByQuestionType(params.question_type)
    // etc.
}

You can do a variety of things to affect the mapping of urls to requests, check out the UrlMapping section in the Grails User Guide.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜