开发者

How to hide URL parameters in Grails

开发者 https://www.devze.com 2023-04-13 03:17 出处:网络
When a user is creating a new Load object, if the user checks the \"Paid On Delivery\" check box then they will be redirected to the Payment controller immediately after the new Load has been created.

When a user is creating a new Load object, if the user checks the "Paid On Delivery" check box then they will be redirected to the Payment controller immediately after the new Load has been created. Several of the parameters needed to create a new Load are also used to create a new Payment, so I just pass the parameters in the redirect like this:

redirect(controller: "payment", action: "create", params: params)

This works fine, but it gives me a real nasty URL with all the parameters in it. How can I pass my parameters to another controller and keep them from appearing in the URL?

UPDATE:

I should say that I appreciate everyone's suggestions for such a little problem. Even with all the suggestions, it still seems the best way of doing this is the way I wanted to avoid, building the parameter map manually in the redirect call. It isn't that big of a deal, especially since there is only a few params, I just don't believe there is isn't a cleaner more automated way of fixing this.

    def loadInstance = new Load(params)

    if (loadInstance.save(flush: true)) {
        Account.get(params.account.id).balance -= new BigDecimal(params.transactionAmount) 
        flash.message = "${message(code: 'default.created.message', args: [message(code: 'load.label', default: 'Load'), loadInstance.id])}"
        if(params.paidOnDelivery){
            redirect(
                controller: "payment",
                action: "create",

                //There ha开发者_开发知识库s to be a better way than this. Just writing "params:params" results in the values being wrapped in double quotes once they get to the Payment controller. If it wasn't for that then "params:params" would work great and I would not of had to ask this question :)
                params: [
                    "account.id":params.account.id,
                    "dateOfTransaction":params.dateOfTransaction,
                    "dateOfTransaction_year":params.dateOfTransaction_year,
                    "dateOfTransaction_month":params.dateOfTransaction_month,
                    "dateOfTransaction_day":params.dateOfTransaction_day,
                    "dateOfTransaction_hour":params.dateOfTransaction_hour,
                    "dateOfTransaction_minute":params.dateOfTransaction_minute,
                    "transactionAmount":params.transactionAmount
                ]
            ) 
            return
        }
        redirect(action: "show", id: loadInstance.id)
    }
    else {
        render(view: "create", model: [loggedByUsers:loggedByUsers, accounts:accounts, cargoProviders:cargoProviders, deliveredByUsers:deliveredByUsers, loadInstance:loadInstance])
    }


You could do a server-side forward instead of a redirect. Simply replace:

redirect(controller: "payment", action: "create", params: params)

with:

forward(controller: "payment", action: "create", params: params)

Update

To fix the refresh problem you described in the comments, make the action that you forward to sends a redirect (instead of rendering a view), e.g.

class PaymentController {

  def create = {

    Integer paymentId = // Create a payment and get it's ID    
    redirect(action: 'show', id: paymentId)    
  }

  def show = {
    def id = params.id
    // show the payment with this ID
  }
}


  1. Pass them via session parameters

or

  1. Make the HTTP request server-side instead of a redirect and then just render the result to the client


The flash object can be used to achieve this (although I would leave the implementation as is).

Copy all your params to flash.

params.each {
  flash[it.key]=it.value
}

Use the flash map instead of params map in the 'create' action.


i didn't try it but i think it will solve your problem.

redirect(controller: "payment", action = [POST:"create"], params: params)


Here's another suggestion :-)

Keep the redirect from Load to Payment as is. Check in Payment action if you receive a call with params, then save those in session or flash and redirect to same action without params. Restore params from session/flash using putAll and proceed with action.

Kind of like a Post-Redirect-Get in Payment, but actually a GetWithParams-Redirect-Get. Results in clean URL and no Refresh problems.

I use it in a app for clean URLs and for keeping states in different parts of the app (but since you don't need the latter you should remove it or keep it in flash).

0

精彩评论

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

关注公众号