我試圖設置一個域級的默認值。我試過但不工作動態默認屬性grails
class myDomainClass{
int idUser = (${session.user.id}!= null)?${session.user.id}:0;
}
我該怎麼辦?
我試圖設置一個域級的默認值。我試過但不工作動態默認屬性grails
class myDomainClass{
int idUser = (${session.user.id}!= null)?${session.user.id}:0;
}
我該怎麼辦?
它不起作用的原因可能是因爲session
(HTTP會話)僅在控制器和GSP中可用,而不是域類。取而代之的是,獲取當前用戶的服務,並注入域類
class UserService() {
int currentUserId() {
GrailsWebRequest webRequest = WebUtils.retrieveGrailsWebRequest()
def session = webRequest.session
session.user?.id
}
}
class myDomainClass {
static transients = ['userService']
UserService userService
int idUser = userService.currentUserId ?: 0
}
此實現訪問來自服務,不推薦使用會話這一點,但它應該工作。當然,你可以只使用相同的方法從域類訪問會話,但是這是比上面
class myDomainClass{
int idUser = {->
GrailsWebRequest webRequest = WebUtils.retrieveGrailsWebRequest()
def session = webRequest.session
session.user?.id ?: 0
}()
}
如果(用戶){ session.user = user}我在登錄頁面設置會話 – Cotrariello
我試過了代碼發佈但不工作我recive一個sessionFactory錯誤 – Cotrariello
@Cotrariello我在我的代碼中修正了錯誤(我忘了'瞬變'),請再試一次 –
|Running Grails application
Error |
2013-12-18 15:50:17,476 [localhost-startStop-1] ERROR context.GrailsContextLoader - Error initializing the application: Error creating bean with name 'transactionManagerPostProcessor': Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'transactionManager': Cannot resolve reference to bean 'sessionFactory' while setting bean property 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory': Invocation of init method failed; nested exception is org.hibernate.InstantiationException: could not instantiate test objectsmat.AnalisiIZS
Message: Error creating bean with name 'transactionManagerPostProcessor': Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'transactionManager': Cannot resolve reference to bean 'sessionFactory' while setting bean property 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory': Invocation of init method failed; nested exception is org.hibernate.InstantiationException: could not instantiate test objectsmat.AnalisiIZS
Line | Method
->> 262 | run in java.util.concurrent.FutureTask
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
| 1145 | runWorker in java.util.concurrent.ThreadPoolExecutor
| 615 | run . . . in java.util.concurrent.ThreadPoolExecutor$Worker
^ 744 | run in java.lang.Thread
Caused by BeanCreationException: Error creating bean with name 'transactionManager': Cannot resolve reference to bean 'sessionFactory' while setting bean property 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory': Invocation of init method failed; nested exception is org.hibernate.InstantiationException: could not instantiate test objectsmat.AnalisiIZS
->> 262 | run in java.util.concurrent.FutureTask
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
| 1145 | runWorker in java.util.concurrent.ThreadPoolExecutor
| 615 | run . . . in java.util.concurrent.ThreadPoolExecutor$Worker
^ 744 | run in java.lang.Thread
Caused by BeanCreationException: Error creating bean with name 'sessionFactory': Invocation of init method failed; nested exception is org.hibernate.InstantiationException: could not instantiate test objectsmat.AnalisiIZS
->> 262 | run in java.util.concurrent.FutureTask
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
| 1145 | runWorker in java.util.concurrent.ThreadPoolExecutor
| 615 | run . . . in java.util.concurrent.ThreadPoolExecutor$Worker
^ 744 | run in java.lang.Thread
Caused by InstantiationException: could not instantiate test objectsmat.AnalisiIZS
->> 262 | run in java.util.concurrent.FutureTask
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
| 1145 | runWorker in java.util.concurrent.ThreadPoolExecutor
| 615 | run . . . in java.util.concurrent.ThreadPoolExecutor$Worker
^ 744 | run in java.lang.Thread
Caused by InvocationTargetException: null
->> 262 | run in java.util.concurrent.FutureTask
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
| 1145 | runWorker in java.util.concurrent.ThreadPoolExecutor
| 615 | run . . . in java.util.concurrent.ThreadPoolExecutor$Worker
^ 744 | run in java.lang.Thread
Caused by NullPointerException: null
->> 19 | <init> in smat.AnalisiIZS
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
| 262 | run in java.util.concurrent.FutureTask
| 1145 | runWorker in java.util.concurrent.ThreadPoolExecutor
| 615 | run in java.util.concurrent.ThreadPoolExecutor$Worker
^ 744 | run . . . in java.lang.Thread
Error |
Forked Grails VM exited with error
看起來這是您問題的根源: org.hibernate.InstantiationException:無法實例化測試對象,無法實例化測試對象嗎?答案 –
是什麼意思?我可以做什麼? – Cotrariello
向我們展示此類的源代碼將是一個好的開始 –
類AnalisiIZS {
Campione campione;
Date dataAnalisiCampione;
String specieIsolata1;
String codiceIsolato1;
String specieIsolata2;
String codiceIsolato2;
String specieIsolata3;
String codiceIsolato3;
String specieIsolata4;
String codiceIsolato4;
String nomeUser;
static constraints = {
dataAnalisiCampione(blank:true)
specieIsolata1(blank:true)
codiceIsolato1(blank:true)
specieIsolata2(blank:true)
codiceIsolato2(blank:true)
specieIsolata3(blank:true)
codiceIsolato3(blank:true)
specieIsolata4(blank:true)
codiceIsolato4(blank:true)
campione(blank:true)
nomeUser(display:false)
}
String toString(){
return dataAnalisiCampione;
}
什麼錯誤信息的解決方案,甚至有吸引力的少你得到 –
查找帖子 – Cotrariello