我使用 Spring MVC。我在从 jsp 页面触发我的服务时遇到问题 --> dao 方法。当我在浏览器中请求 jsp 时,它会显示:
2013-07-19 07:55:32,385 DEBUG [org.springframework.web.servlet.DispatcherServlet] - <DispatcherServlet with name 'appServlet' processing GET request for [/web/]>
2013-07-19 07:55:32,387 DEBUG [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping] - <Looking up handler method for path />
2013-07-19 07:55:32,388 DEBUG [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping] - <Did not find handler method for [/]>
2013-07-19 07:55:32,388 WARN [org.springframework.web.servlet.PageNotFound] - <No mapping found for HTTP request with URI [/web/] in DispatcherServlet with name 'appServlet'>
2013-07-19 07:55:32,388 DEBUG [org.springframework.web.servlet.DispatcherServlet] - <Successfully completed request>
2013-07-19 07:55:41,889 DEBUG [org.springframework.web.servlet.DispatcherServlet] - <DispatcherServlet with name 'appServlet' processing GET request for [/web/input]>
2013-07-19 07:55:41,889 DEBUG [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping] - <Looking up handler method for path /input>
2013-07-19 07:55:41,891 DEBUG [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping] - <Returning handler method [public java.lang.String edu.demidov.web.MainController.inputForm(edu.demidov.web.FormBackingObjectInput,org.springframework.ui.Model)]>
2013-07-19 07:55:41,891 DEBUG [org.springframework.beans.factory.support.DefaultListableBeanFactory] - <Returning cached instance of singleton bean 'mainController'>
2013-07-19 07:55:41,891 DEBUG [org.springframework.web.servlet.DispatcherServlet] - <Last-Modified value for [/web/input] is: -1>
2013-07-19 07:55:41,903 DEBUG [org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver] - <Resolving exception from handler [public java.lang.String edu.demidov.web.MainController.inputForm(edu.demidov.web.FormBackingObjectInput,org.springframework.ui.Model)]: org.hibernate.HibernateException: No Session found for current thread>
2013-07-19 07:55:41,905 DEBUG [org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver] - <Resolving exception from handler [public java.lang.String edu.demidov.web.MainController.inputForm(edu.demidov.web.FormBackingObjectInput,org.springframework.ui.Model)]: org.hibernate.HibernateException: No Session found for current thread>
2013-07-19 07:55:41,906 DEBUG [org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver] - <Resolving exception from handler [public java.lang.String edu.demidov.web.MainController.inputForm(edu.demidov.web.FormBackingObjectInput,org.springframework.ui.Model)]: org.hibernate.HibernateException: No Session found for current thread>
2013-07-19 07:55:41,907 DEBUG [org.springframework.web.servlet.DispatcherServlet] - <Could not complete request>
org.hibernate.HibernateException: No Session found for current thread
at org.springframework.orm.hibernate4.SpringSessionContext.currentSession(SpringSessionContext.java:97)
at org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:988)
at edu.demidov.dao.EducationWebDaoImpl.fetchAllInstitutionNames(EducationWebDaoImpl.java:161)
at edu.demidov.service.EducationWebServiceImpl.fetchAllInstitutionNamesService(EducationWebServiceImpl.java:82)
at edu.demidov.web.MainController.prepareModelNames(MainController.java:39)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:219)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
at org.springframework.web.method.annotation.ModelFactory.invokeModelAttributeMethods(ModelFactory.java:123)
at org.springframework.web.method.annotation.ModelFactory.initModel(ModelFactory.java:97)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:722)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:686)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:925)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:936)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:827)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:812)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:164)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:462)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:562)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:395)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:250)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:188)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:302)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
at java.lang.Thread.run(Thread.java:722)
我认为问题出在我的 xml 应用程序配置上。我在这里读到,发生这种类型的问题是因为事务不在适当的地方,不在服务层上,对我来说,这似乎是正确的,只是我使用 aop 样式来提供事务
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <!-- Using and configuring C3P0 proxy -->
<property name="driverClass"><value>org.h2.Driver</value></property>
<property name="jdbcUrl"><value>jdbc:h2:/home/vadim/workspace-sts/h2/EDUCATION</value></property>
<property name="user"><value>sa</value></property>
<property name="password"><value></value></property>
<property name="initialPoolSize"><value>3</value></property> <!-- Number of Connections a pool will try to acquire upon startup -->
<property name="minPoolSize"><value>1</value></property> <!-- Minimum connection pool size -->
<property name="maxPoolSize"><value>20</value></property> <!-- Max connection pool size -->
<property name="maxConnectionAge"><value>3600</value></property> <!-- Set max connection age to 1 hour, after it will release -->
<property name="maxIdleTime"><value>600</value></property> <!-- 10 minutes connection can stay unused before be discarded -->
<property name="checkoutTimeout"><value>240000</value></property> <!-- Each what time check for unused connections -->
</bean>
<!-- Declare Hibernate transaction manager. Realization of PlatformTransactionManager-->
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref ="sessionFactory"/>
</bean>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="fetch*" propagation="REQUIRED" read-only="true" />
<tx:method name="*"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="eduWebPointcuts" expression="execution(* edu.demidov.service.EducationWebServiceImpl.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="eduWebPointcuts"/>
</aop:config>
<!-- Looks for specific annotation configuration in this packages -->
<context:component-scan base-package="edu.demidov.dao, edu.demidov.service"/>
<!-- Declare annotation configuration -->
<context:annotation-config />
<!-- Build sessionFactory annotation base configuration -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref ="dataSource"/> <!-- Injecting datasource C3P0 to Hibernate sessionFactory datasource -->
<property name="packagesToScan" value="edu.demidov.dto"/> <!-- Package will be scanned for entity classes -->
<property name="hibernateProperties"> <!-- Configuring properties -->
<props>
<prop key="hibernate.dialect"> org.hibernate.dialect.H2Dialect</prop> <!-- Put dialect for particular database to use queries to this DB -->
<prop key="show_sql">true</prop>
<prop key="hibernate.c3p0.min_size">1</prop> <!-- Same to minPoolSize -->
<prop key="hibernate.c3p0.max_size">20</prop> <!--Same to maxPoolSize -->
<prop key="hibernate.c3p0.timeout">600</prop> <!-- Same to maxIdleTime -->
<prop key="hibernate.c3p0.max_statements">5</prop> <!-- Set number of cached statements used often -->
<prop key="hibernate.max_fetch_depth">3</prop> <!-- Sets a maximum "depth" for the outer join fetch tree for single-ended associations (one-to-one, many-to-one) -->
<prop key="hibernate.jdbc.fetch_size">30</prop> <!-- Declare how many records will get from database in one package -->
<prop key="hibernate.jdbc.batch_size">15</prop> <!-- When it's a lot of queries in short time passed to database this put queries in one batch and send them to database -->
<prop key="hibernate.show_sql">true</prop>
</props>
</property> <!-- End of configuration hibernateProperties -->
</bean> <!-- End of configuring org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean -->
我的
<aop:pointcut id="eduWebPointcuts" expression="execution(*
edu.demidov.service.EducationWebServiceImpl.*(..))"/>
绝对是在 EducationWebServiceImpl 类的每个方法上应该执行切入点的地方。
我有什么问题???
如果你需要这里是我的 dao 实现:
@SuppressWarnings("unchecked")
@Override
public List<String> fetchAllInstitutionNames() {
return sessionFactory.getCurrentSession().createQuery("select distinct institution.nameOfInstitution from InstitutionInForm institution").list();
}
@SuppressWarnings("unchecked")
@Override
public List<String> fetchAllInstitutionTypes() {
return sessionFactory.getCurrentSession().createQuery("select distinct institutionType.typeOfInstitution from InstitutionTypeInForm institutionType").list();
}
@SuppressWarnings("unchecked")
@Override
public List<Date> fetchAllDates() {
return sessionFactory.getCurrentSession().createQuery("select distinct newDate.particularDate from FormDate newDate").list();
}
我的jsp:
<form:form modelAttribute="fboiAttributes" action="add" method="post">
<table align="center">
<tr><td> <form:label path="formDate.particularDate"/> <form:select path="formDate.particularDate"> <form:options items="${listOfDates}"/> </form:select> </td></tr>
<tr><td> <form:label path="institutions.nameOfInstitution"/> <form:select path="institutions.nameOfInstitution"> <form:options items="${listOfNames}"/> </form:select> </td></tr>
<tr><td> <form:label path="institutionType.typeOfInstitution"/> <form:select path="institutionType.typeOfInstitution"> <form:options items="${listOfTypes}"/> </form:select> </td></tr>
<tr><td> <form:label path="formDescription.daySchedule"/> <form:input path="formDescription.daySchedule"/> </td></tr>
<tr><td> <form:label path="formDescription.workScheduale"/> <form:input path="formDescription.workScheduale"/> </td></tr>
<tr><td> <form:label path="formDescription.rotation"/> <form:input path="formDescription.rotation"/> </td></tr>
<tr><td> <form:label path="formDescription.numberOfKids"/> <form:input path="formDescription.numberOfKids"/> </td></tr>
<tr><td> <form:label path="formDescription.kidsUnder3YearsOld"/> <form:input path="formDescription.kidsUnder3YearsOld"/> </td></tr>
<tr><td> <form:label path="formDescription.kidsUpper3YearsOld"/> <form:input path="formDescription.kidsUpper3YearsOld"/> </td></tr>
<tr><td> <form:label path="formDescription.kidsGoSchoolDate"/> <form:input path="formDescription.kidsGoSchoolDate"/> </td></tr>
<tr><td> <form:label path="formDescription.kidsGoSchoolNumber"/> <form:input path="formDescription.kidsGoSchoolNumber"/> </td></tr>
<tr><td> <input type="submit"/> </td></tr>
</table>
</form:form>
我想说,这个jsp页面在渲染过程中触发dao方法从数据库中获取一些数据供用户选择。
我的主控制器:
private static final Logger logger = LoggerFactory.getLogger(MainController.class);
@Autowired
private EducationWebServiceImpl educationWebServiceImpl;
@ModelAttribute("listOfDates")
public List<Date> prepareModelDate() {
List<Date> listOfDates = educationWebServiceImpl.fetchAllDatesService();
return listOfDates;
}
@ModelAttribute("listOfNames")
public List<String> prepareModelNames() {
List<String> listOfNames = educationWebServiceImpl.fetchAllInstitutionNamesService();
return listOfNames;
}
@ModelAttribute("listOfTypes")
public List<String> prepareModelTypes() {
List<String> listOfTypes = educationWebServiceImpl.fetchAllInstitutionTypesService();
return listOfTypes;
}
@RequestMapping(value="/input", method=RequestMethod.GET)
public String inputForm(FormBackingObjectInput fboi, Model model) {
model.addAttribute("fboiAttributes", fboi);
return "input";
}
Servlet-context.xml
<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/" />
<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/classes directory. Goes first -->
<beans:bean id="xlsviewResolver" class="org.springframework.web.servlet.view.ResourceBundleViewResolver">
<beans:property name="order" value="1" />
<beans:property name="basename" value="views"/>
</beans:bean>
<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
<beans:bean id="jspviewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
<beans:bean class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<beans:property name="basename" value="/resources/locale/messages"/>
</beans:bean>
<context:component-scan base-package="edu.demidov.web, edu.demidov.service, edu.demidov.dao" />