无法从Thymeleaf获取事务同步的会话

时间:2018-08-17 16:07:37

标签: java spring hibernate spring-mvc thymeleaf

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

thymeleaf-servlet.xml

TEXT

控制器:

INSERT INTO aes_test (username, encrypted) VALUES ('evan', AES_ENCRYPT('evan', '69552E16F55C3E88CF3CBC44EB5F71B24DD0CF5CB3A7C65EA97BC69224CF42F1'));

我的应用程序可以在下一个URL http://localhost:8080/MyApp/tl上正常使用,但不能与http://localhost:8080/MyApp/products一起使用

这是我的html文件:

test.html:

SELECT username, AES_DECRYPT(`encrypted`, '69552E16F55C3E88CF3CBC44EB5F71B24DD0CF5CB3A7C65EA97BC69224CF42F1') FROM aes_test WHERE AES_DECRYPT(`encrypted`, '69552E16F55C3E88CF3CBC44EB5F71B24DD0CF5CB3A7C65EA97BC69224CF42F1') = 'evan'

products.html:

<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>

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

@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());
    }

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

<!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>

预先感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

如果您的productService在方法上使用@Transactional,我想您忘记在xml中添加事务注释

<tx:annotation-driven />

请将以上元素添加到您的xml

相关问题