在grails中重定向 [英] redirect in grails
问题描述
控制器:
code> def upload(){
def f = request.getFile('myFile')
if(f == null | f.empty){
flash.default =文件不能为空
errors.getFieldError(不能为空)
返回
}
def fName = f.getOriginalFilename()
def picture = new Picture (orgName:fName,urlOrg:http:// localhost /+ fName)
f.transferTo(new File('/ Users / sagarmichael / Desktop / photoUpload /'+ fName))
println save)
redirect(action:'test',params:[url1:picture.urlOrg])
}
def test( ){
System.out.println(params.url1)
[url1:params.url1]
}
我希望这会发送url1到我的视图中,称为测试,我有这个:
< img src =$ {url1}/>
我希望这会在屏幕上显示图像。
我有apache2配置设置正确,当我去
http:// localhost /< imageName>
工作正常。
我得到这是在浏览器的url栏:
http:// localhost:8080 / FYP / profile / test? url1 = http%3A%2F%2Flocalhost%2Flonglogo3.png
有什么想法?
$如果您使用重定向
方法并使用 params $ b 它们会出现在网址中。为了省略这个,你需要用另一种方式来设计你的代码。
看看你的代码,看起来你需要上传一些图片,需要显示结果。我建议你只传递图像的名称到你的其他方法,并挂载链接动态(并不总是本地主机,赖特?)。输出网址为:
http:// localhost:8080 / FYP / profile / test?image = longlogo3.png
如果您还需要忽略图像的名称,则必须将其存储在会话中。
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?
解决方案 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.
这篇关于在grails中重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!