【发布时间】:2014-06-02 21:39:34
【问题描述】:
我正在使用 Grails 开发一个 Web 应用程序。我的环境规格是:
- Groovy Grails 工具套件 (GGTS) 3.6.0.M1-e4.4-win32-x86_64
- 休眠 4.3.5.3
- Groovy 2.3
- Grails 2.4.0
- Windows 8.1 Pro N
我启动应用程序,然后在 grails 启动页面上选择这个控制器:
class UploadController {
def index() { }
def upload() {
forward(controller: "parser", action: "takeUploadedFiles")
}
}
只有这个控制器不会发生任何事情。对应的 .gsp 就是这个上传表单。我用它一次上传多个文件。
<%@ page contentType="text/html;charset=UTF-8" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"/>
<meta name="layout" content="main"/>
<title>Inhalte einlesen</title>
</head>
<body>
<div class="body">
<g:uploadForm controller="upload" action="upload" autocomplete="off">
<label for="files">Bitte alle HTML-Dateien einer Kategorie auswählen:</label>
<input type="file" id="files" name="files" multiple="multiple" />
<input type="hidden" id="MAX_FILE_SIZE" name="MAX_FILE_SIZE" value="30000" />
<g:submitButton name="add" class="save button medium" value="Hochladen" />
</g:uploadForm>
</div>
</body>
</html>
UploadController 转发到 ParserController。这是 ParserController:
import org.springframework.web.multipart.commons.CommonsMultipartFile
class ParserController {
def multipartToFileService
def parserService
def index() { }
def takeUploadedFiles() {
List fileList = request.getFiles('files') // 'files' is the name of the input
fileList.each { file -> multipartToFileService.convert(file) }
redirect(action: "parse")
}
def parse() {
parserService.parse()
}
}
所以我上传了一个 .html。 此时我得到了这个异常:
2014-06-02 23:04:33,127 [tomcat-http--9] ERROR servlet.GrailsDispatcherServlet - HandlerInterceptor.afterCompletion threw exception
java.lang.IllegalStateException: No value for key [org.hibernate.internal.SessionFactoryImpl@6398813a] bound to thread [tomcat-http--9]
at grails.plugin.cache.web.filter.PageFragmentCachingFilter.doFilter(PageFragmentCachingFilter.java:198)
at grails.plugin.cache.web.filter.AbstractFilter.doFilter(AbstractFilter.java:63)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:745)
这个异常不会停止应用程序,即使解析完成得很好。 但我不明白这个例外,我发现它可能有事可做 使用我在 PaserController 中注入的事务服务。 如果没有服务注入,我不会收到异常(当然,我会收到 MissingPropertyException):
import org.springframework.web.multipart.commons.CommonsMultipartFile
class ParserController {
// def multipartToFileService
// def parserService
def index() { }
def takeUploadedFiles() {
List fileList = request.getFiles('files') // 'files' is the name of the input
fileList.each { file -> multipartToFileService.convert(file) }
redirect(action: "parse")
}
def parse() {
parserService.parse()
}
}
任何想法如何避免这种 IllegalStateException?
【问题讨论】: