当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);
}
}
}
}
}
答案 0 :(得分:1)
您应该将csv阅读器的功能真正转移到服务中并让控制器调用它。可能性可能是使用async:
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)
答案 1 :(得分:0)
这里是在进程运行时实际显示渲染消息的编码,如果我想在处理后显示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"
}