在service类前加上@Transactional,声明这个service所有方法需要事务管理。
解决方案 »
- 本人菜鸟,刚刚做的一个Struts结合Hibernate的例子,报错,望大神进来指教
- 求救,myeclipse问题
- 求个javaScript提示框 类似CSDN 的鼠标放到用户上就提示出来!!!
- tomcat1.4.2不能正常启动
- MyEclipse 启动tomcat无反应,关闭后弹出提示 a java exception has occurred
- 微信IM 即时通讯发送视频如何保证文件大小?
- webwork里有没象struts里 <html:link action="" 这样的标签?
- 公司要用JSF+Spring+Hibernate做项目,哪位有这些的资料给我看看吧
- 如何在JBoss下配置JMS?
- 求教数据校验
- 客户端连接到oracle数据库出现12514错误
- 老看到有人写 Bytes.toBytes(String类型的字符串) 为啥老看到有人把字符串转成字节数组呢 有啥好处啊
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<context:annotation-config />
<context:component-scan base-package="edu.cs.tnt.sim..*">
<context:exclude-filter type="annotation"
expression="org.springframework.stereotype.Controller" />
</context:component-scan>
<context:property-placeholder location="classpath*:jdbc.properties" />
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean> <bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan" value="edu.cs.tnt.sim"></property> <property name="mappingLocations">
<list>
</list>
</property> <property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<!-- <prop key="current_session_context_class">thread</prop> -->
</props>
</property>
</bean>
<!-- 事务配置 -->
<bean id="txManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="create*" propagation="REQUIRED" />
<tx:method name="insert*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="del*" propagation="REQUIRED" />
<tx:method name="remove*" propagation="REQUIRED" />
<tx:method name="put*" propagation="REQUIRED" />
<tx:method name="get*" propagation="REQUIRED" />
<tx:method name="count*" propagation="REQUIRED" />
<tx:method name="find*" propagation="REQUIRED" />
<tx:method name="list*" propagation="REQUIRED" />
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="daoMethods"
expression="execution(* edu.cs.tnt.sim..*Dao.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="daoMethods" />
</aop:config>
</beans>
只是觉得java里面能正常运行,配置应该没什么问题,,所以就没上配置。。
只是觉得java里面能正常运行,配置应该没什么问题,,所以就没上配置。。
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>Archetype Created Web Application</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath*:Beans.xml
</param-value>
</context-param> <listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<!-- Spring配置文件结束 -->
<filter>
<filter-name>Encoding</filter-name>
<filter-class>
org.springframework.web.filter.CharacterEncodingFilter
</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>Encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- Spring MVC配置开始 -->
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet> <servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
<!-- Spring MVC配置结束-->
</web-app>
public class UserController {
@Autowired
UserService userService; @RequestMapping("login")
public String Login(@RequestParam("username") String username,
@RequestParam("pwd") String pwd, HttpServletRequest request) {
if(userService.Login(username, pwd)){
return "loginsuccess";
}
else {
return "loginfail";
}
}
}jsp页面中表单(没有其他代码):<form action="login.html" method="post">请输入姓名:<input type="text" name="username" />
请输入密码:<input type="text" name="pwd" /><input type="submit" value="提交" /><br /></form>这样的话就会报 no session。如果controller中不涉及数据访问部分就可以正常运行。测试代码:
public class test { public static void main(String[] args){
BeanFactory factory=new ClassPathXmlApplicationContext("classpath*:Beans.xml");
UserService userService=(UserService) factory.getBean("userServiceImpl");
// User user=new User();
// user.setClazz("软件115");
// user.setEmail("[email protected]");
// user.setPwd("zp1228");
// user.setSuperuser(1);
// user.setUsername("zp1");
// System.out.println(userService.Register(user));
System.out.println(userService.Login("zp", "zp1228"));
}
}java代码可以正常的得出结果,不会no session。
我试了一下,切面不指定dao的话,还是会有这样的错误。而且指定了用java代码还是可以正常使用service,不会no session。可以看看楼上我贴的代码,,实在是搞不懂怎么回事了
错误报的是No Session found for current thread,终点是不是current thread?是不是在tomcat中就是其他thread了?
dao里面的getCurrentSession()这一句报错。
严重: Servlet.service() for servlet [spring] in context with path [/sim_web] threw exception [Request processing failed; nested exception is org.hibernate.HibernateException: No Session found for current thread] with root cause
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.cs.tnt.sim.user.dao.UserDao.getSession(UserDao.java:20)
at edu.cs.tnt.sim.user.dao.UserDao.findByName(UserDao.java:31)
at edu.cs.tnt.sim.user.service.impl.UserServiceImpl.Login(UserServiceImpl.java:19)
at edu.cs.tnt.sim.user.controller.UserController.Login(UserController.java:21)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:176)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:440)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:428)
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:953)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:855)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:646)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:829)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1040)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:315)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
<context:component-scan base-package="edu.cs.tnt.sim">试试
嘿嘿,我这也是才开始学这个,所以也不知道这个问题是不是有点小白。
.OpenSessionInViewFilter这东西我一开始就没见过。
http://docs.spring.io/spring/docs/4.0.3.RELEASE/javadoc-api//org/springframework/orm/hibernate4/support/OpenSessionInViewFilter.html