redirect in grails

匿名 (未验证) 提交于 2019-12-03 02:41:02

问题:

I am redirecting an uploaded image in grails like so:

Controller:

    def upload() {     def f = request.getFile('myFile')     if (f == null | f.empty) {         flash.default = "file cannot be empty"         errors.getFieldError("cannot be empty")         return     }     def fName = f.getOriginalFilename()     def picture = new Picture(orgName: fName, urlOrg: "http://localhost/"+fName)     f.transferTo(new File('/Users/sagarmichael/Desktop/photoUpload/'+fName))     println("saved")     redirect(action: 'test', params: [url1:picture.urlOrg] )   }  def test(){     System.out.println(params.url1)             [url1:params.url1]  } 

I would expect this to then send url1 to my view called test where I have this:

<img src="${url1}"/> 

I would expect this to then show the image on screen. I have the apache2 config set correctly and when i go to

http://localhost/<imageName>  

it works correctly.

What I am getting is this in the url bar on the browser:

http://localhost:8080/FYP/profile/test?url1=http%3A%2F%2Flocalhost%2Flonglogo3.png 

any ideas?

回答1:

If you use the redirect method with params they will appear in the url. To omit this, you will need to think in another way to design your code.

Looking at your code, it appears that you do the upload of some image and need to show the result. I suggest you pass just the name of the image to your other method and mount the link dynamic (not always will be localhost, wright?). The output url will be:

http://localhost:8080/FYP/profile/test?image=longlogo3.png 

If you need to omit the name of the image too you will have to store this in the session.



回答2:

You are mixing model and URL params. If you really need to have it like this, you have to put the content of param 'url1' into the model in the test() action:

def test() {     return [ 'url1': params.url1 ] } 

I hope, you get the point, I definitely recommend to redesign the code ;-)



回答3:

Try

render(view:"test", model: [url1:picture.urlOrg]) 

Or

chain(action: "test", model: [url1:picture.urlOrg]) 


文章来源: redirect in grails
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!