有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java无法从Thymeleaf获取transactionsynchronized会话

我以前使用SpringMVC,jsp页面也可以,但现在我尝试将我的应用程序与Thymeleaf集成

Thymeleafservlet。xml

<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:mvc="http://www.springframework.org/schema/mvc"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.1.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
        ">

    <mvc:annotation-driven/>    
    <context:annotation-config />
    <context:component-scan base-package="com.application" />

    <bean id="templateResolver" class="org.thymeleaf.templateresolver.ServletContextTemplateResolver">
        <property name="prefix" value="/WEB-INF/thymeleaf/" />
        <property name="suffix" value=".html" />

    </bean>

    <bean id="templateEngine" class="org.thymeleaf.spring4.SpringTemplateEngine">
        <property name="templateResolver" ref="templateResolver" />
    </bean>

    <bean id="viewResolver" class="org.thymeleaf.spring4.view.ThymeleafViewResolver">
        <property name="templateEngine" ref="templateEngine" />
    </bean>

    <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
        <property name="basenames">
            <!-- messages.properies -->
            <list><value>messages</value></list>
        </property>
    </bean>

    <bean id="dataSource" class="org.springframework.jdbc.datasource.SingleConnectionDataSource">
            <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
            <property name="url" value="jdbc:mysql://localhost:3306/database"></property>
            <property name="password" value="paswd"></property>
            <property name="username" value="root"></property>
            <property name="suppressClose" value="true"></property>
        </bean>

    <bean id="dataSourceProxy"
        class="org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy"
        p:targetDataSource-ref="dataSource" />


    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"
        p:dataSource-ref="dataSourceProxy" p:configLocation="classpath:/hibernate.cfg.xml" />

            <bean id="transactionManager"
        class="org.springframework.orm.hibernate5.HibernateTransactionManager"
        p:sessionFactory-ref="sessionFactory" />
</beans>

控制器:

@Controller
public class ProductController {

    @Autowired
    private ProductService productService;

    @RequestMapping("/tl")
    public String tl() {
        return "test";
    }

    /* List all the products on the db */
    @RequestMapping(value = "/products", method = RequestMethod.GET)
    public ModelAndView getAll(Model model) {

        return new ModelAndView("products", "list", productService.list());
    }

我的应用程序可以与下一个URL http://localhost:8080/MyApp/tl一起工作,但不能与http://localhost:8080/MyApp/products一起工作

这里是我的html文件:

测试。html:

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" th:lang="${#locale.language}">
    <head>
        <meta charset="UTF-8" />
        <title th:text="#{title}">hello html5</title>
    </head>
    <body>
        <h1 th:text="#{hello}">hello world</h1>
        <p th:text="#{hola}">hola mundo</p>
    </body>
</html>

产品。html:

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
    <head>
        <meta charset="UTF-8"/>
        <title>Spring Thymeleaf</title>
    </head>
    <body>


        <table border="1">
            <tr th:each="product: ${list}">
                <td th:text="${product.id}" />
                <td th:text="${product.name}" />

            </tr>
        </table>

    </body>
</html>

当我尝试访问http://localhost:8080/MyApp/products时,会出现下一个错误:

GRAVE: El Servlet.service() para el servlet [thymeleaf] en el contexto con ruta [/MyApp] lanzó la excepción [Request processing failed; nested exception is org.hibernate.HibernateException: Could not obtain transaction-synchronized Session for current thread] con causa raíz
org.hibernate.HibernateException: Could not obtain transaction-synchronized Session for current thread
    at org.springframework.orm.hibernate5.SpringSessionContext.currentSession(SpringSessionContext.java:133)
    at org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:456)
    at com.system.app.model.dao.impl.GenericDaoHibernate.getSession(GenericDaoHibernate.java:51)
    at com.system.app.model.dao.impl.GenericDaoHibernate.list(GenericDaoHibernate.java:90)
    at com.system.app.model.service.impl.ProductServiceImpl.listViajantes(ProductServiceImpl.java:49)
    at com.system.app.controller.ProductController.getAll(ProductController.java:41)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205)
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:133)
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:116)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738)
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:963)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:897)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:292)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:528)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1099)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:670)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1520)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1476)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:748)

我拥有Spring和Hibernate所需的所有依赖项(在与Thymeleaf集成之前,我的应用程序工作正常),我将这些依赖项添加到Thymeleaf中:

<dependency>
    <groupId>org.thymeleaf</groupId>
    <artifactId>thymeleaf</artifactId>
    <version>3.0.9.RELEASE</version>
</dependency>
    <dependency>
    <groupId>org.thymeleaf</groupId>
    <artifactId>thymeleaf-spring4</artifactId>
    <version>3.0.9.RELEASE</version>
</dependency>

提前感谢你的帮助


共 (1) 个答案

  1. # 1 楼答案

    我猜如果productService在方法上有@Transactional,您可能会忘记在xml中添加事务注释

    <tx:annotation-driven />
    

    请将上述元素添加到xml中