当控制器操作 运行 时,显示用户消息
while controller action running, show user message
虽然 readcsv 操作是 运行,但处理数据需要几分钟时间,具体取决于文件大小,因此在执行时我想知道如何向用户显示消息以告诉他们数据正在进行中..thx
def readcsv() {
redirect(action: "list")
flash.message = "okokokok"
def list = []
def dir = new File("C:/Users/User/Desktop/Summarize_20141212/ONE_FILE")
dir.eachFileRecurse (FileType.FILES) { file ->
list << file
}
list.each {
File file = new File(it.path)
def sql = groovy.sql.Sql.newInstance("jdbc:postgresql://localhost:5432/new",
'postgres', 'sa', "org.postgresql.Driver")
def linecount = 0
file.eachLine() { line ->
if (line.trim().size() == 0) {
return null
} else {
def field = []
def tokens = line.split(',(?=([^\"]*\"[^\"]*\")*[^\"]*$)')
file.name.lastIndexOf('.').with { it != -1 ? file.name[0..<it] : file.name }
linecount++
field.push(file.name)
for (String t : tokens) {
field.push(t)
}
while (field.size() < 10) {
field.push("")
}
if (linecount > 1) {
sql.execute('insert into read_csv(version,col1,col2,col3,col4,col5,col6,col7,col8,col9,col10) ' +
'VALUES (0,?,?,?,?,?,?,?,?,?,?)', field)
System.out.println(field);
}
}
}
}
}
您真的应该将 csv reader 的功能移动到一个服务中,并让您的控制器调用它。可能是使用异步:
class MyController {
def myService
def readcsv() {
def asyncProcess = new Thread({
myService.readCSV(filename, input2,input3)
} as Runnable )
asyncProcess.start()
forward (action: 'list')
//render "Your file is being processed"
//forward (controller:"someController", action: 'someAction', model [param1: params.param1 param2: param2:params.param2 ])
}
在这里阅读异步:http://grails.org/doc/latest/guide/async.html
除了你的前锋,你还可以使用以下变体
渲染:渲染然后加载到您希望显示的实际页面而不是当前操作...所以在这种情况下列表
render (view: 'list', model: [params:params])
或者甚至
连锁:http://grails.org/doc/2.4.x/ref/Controllers/chain.html
chain(action: "details", model: [book: shawshankRedemption])
最后重定向
redirect (controller: 'admin', action: 'welcome')
redirect(url: request.getHeader('referer'))
redirect(action: "show", id: some.id)
redirect(action: "list", params: params)
这里是在处理过程中实际显示渲染消息的代码 运行,如果我想在处理后显示 def list() 页面,怎么办?如果我只是在最后添加 redict 渲染消息将不再显示..
....
def readcsv() {
def asyncProcess = new Thread({
def list = []
def dir = new File("C:/Users/User/Desktop/Summarize_20141212/ONE_FILE")
dir.eachFileRecurse(FileType.FILES) { file ->
list << file
}
list.each {
File file = new File(it.path)
def sql = groovy.sql.Sql.newInstance("jdbc:postgresql://localhost:5432/new",
'postgres', 'sa', "org.postgresql.Driver")
def linecount = 0
file.eachLine() { line ->
if (line.trim().size() == 0) {
return null
} else {
def field = []
def tokens = line.split(',(?=([^\"]*\"[^\"]*\")*[^\"]*$)')
file.name.lastIndexOf('.').with { it != -1 ? file.name[0..<it] : file.name }
linecount++
field.push(file.name)
for (String t : tokens) {
field.push(t)
}
while (field.size() < 10) {
field.push("")
}
if (linecount > 1) {
sql.execute('insert into read_csv(version,col1,col2,col3,col4,col5,col6,col7,col8,col9,col10) ' +
'VALUES (0,?,?,?,?,?,?,?,?,?,?)', field)
System.out.println(field);
}
}
}
}
} as Runnable )
asyncProcess.start()
render "Your file is being processed"
}
虽然 readcsv 操作是 运行,但处理数据需要几分钟时间,具体取决于文件大小,因此在执行时我想知道如何向用户显示消息以告诉他们数据正在进行中..thx
def readcsv() {
redirect(action: "list")
flash.message = "okokokok"
def list = []
def dir = new File("C:/Users/User/Desktop/Summarize_20141212/ONE_FILE")
dir.eachFileRecurse (FileType.FILES) { file ->
list << file
}
list.each {
File file = new File(it.path)
def sql = groovy.sql.Sql.newInstance("jdbc:postgresql://localhost:5432/new",
'postgres', 'sa', "org.postgresql.Driver")
def linecount = 0
file.eachLine() { line ->
if (line.trim().size() == 0) {
return null
} else {
def field = []
def tokens = line.split(',(?=([^\"]*\"[^\"]*\")*[^\"]*$)')
file.name.lastIndexOf('.').with { it != -1 ? file.name[0..<it] : file.name }
linecount++
field.push(file.name)
for (String t : tokens) {
field.push(t)
}
while (field.size() < 10) {
field.push("")
}
if (linecount > 1) {
sql.execute('insert into read_csv(version,col1,col2,col3,col4,col5,col6,col7,col8,col9,col10) ' +
'VALUES (0,?,?,?,?,?,?,?,?,?,?)', field)
System.out.println(field);
}
}
}
}
}
您真的应该将 csv reader 的功能移动到一个服务中,并让您的控制器调用它。可能是使用异步:
class MyController {
def myService
def readcsv() {
def asyncProcess = new Thread({
myService.readCSV(filename, input2,input3)
} as Runnable )
asyncProcess.start()
forward (action: 'list')
//render "Your file is being processed"
//forward (controller:"someController", action: 'someAction', model [param1: params.param1 param2: param2:params.param2 ])
}
在这里阅读异步:http://grails.org/doc/latest/guide/async.html
除了你的前锋,你还可以使用以下变体
渲染:渲染然后加载到您希望显示的实际页面而不是当前操作...所以在这种情况下列表
render (view: 'list', model: [params:params])
或者甚至
连锁:http://grails.org/doc/2.4.x/ref/Controllers/chain.html
chain(action: "details", model: [book: shawshankRedemption])
最后重定向
redirect (controller: 'admin', action: 'welcome')
redirect(url: request.getHeader('referer'))
redirect(action: "show", id: some.id)
redirect(action: "list", params: params)
这里是在处理过程中实际显示渲染消息的代码 运行,如果我想在处理后显示 def list() 页面,怎么办?如果我只是在最后添加 redict 渲染消息将不再显示.. ....
def readcsv() {
def asyncProcess = new Thread({
def list = []
def dir = new File("C:/Users/User/Desktop/Summarize_20141212/ONE_FILE")
dir.eachFileRecurse(FileType.FILES) { file ->
list << file
}
list.each {
File file = new File(it.path)
def sql = groovy.sql.Sql.newInstance("jdbc:postgresql://localhost:5432/new",
'postgres', 'sa', "org.postgresql.Driver")
def linecount = 0
file.eachLine() { line ->
if (line.trim().size() == 0) {
return null
} else {
def field = []
def tokens = line.split(',(?=([^\"]*\"[^\"]*\")*[^\"]*$)')
file.name.lastIndexOf('.').with { it != -1 ? file.name[0..<it] : file.name }
linecount++
field.push(file.name)
for (String t : tokens) {
field.push(t)
}
while (field.size() < 10) {
field.push("")
}
if (linecount > 1) {
sql.execute('insert into read_csv(version,col1,col2,col3,col4,col5,col6,col7,col8,col9,col10) ' +
'VALUES (0,?,?,?,?,?,?,?,?,?,?)', field)
System.out.println(field);
}
}
}
}
} as Runnable )
asyncProcess.start()
render "Your file is being processed"
}