如果我们有一个 Controller ,我们称之为文档,它有两种方法,一种上载文件,另一种显示上载的文件。
我想在上载方法中定义一个新字符串,该方法检查文件的大小并在该字符串中存储一个特定的类型名称。
但是我想用另一个方法访问该字符串,该方法是可以显示它的列表方法。
这是我的代码:
Class DocumentController {
def list() {
//Here I would like to access that String to show it on the page
[fileSizeType: fileSizeType]
}
def upload {
//define the new String variable
String fileSizeType = ""
if(fileSize < 1000) {
fileSizeType = "type1.."
} else {
fileSizeType = "type2.."
}
}
}
在gsp页面中,我想通过以下方式访问字符串:
<td><g:link>\${fileSizeType}</g:link></td>
当我尝试上面的代码时,我得到此错误:
No such property: fileSizeType for class: file_down.DocumentController
最佳答案
在参数中传递参数时,您需要重定向到列表操作。
def upload() {
// simplify with ternary expression
def fileSizeType = (fileSize < 1000) ? "type1.." : "type2.."
redirect action:'list', params:[fileSizeType: fileSizeType]
}
// in your list action
def list() {
[fileSizeType: params.fileSizeType]
}
关于grails - 如何在一个 Controller 中将字符串从一种方法访问到另一种方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29753719/