Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Integrating Spring WebFlow with Thymeleaf

I am developing SpringMVC application using Thymeleaf Templates fragment. I want to add simple flow. This is my project structure and configuration:

enter image description here

My spring-servlet.xml file:

    <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">

    <!-- Deklaracja pakietów kontrolerów: -->
    <context:component-scan base-package="pl.etestownik.controller"
        scoped-proxy="targetClass" />

    <mvc:annotation-driven
        ignore-default-model-on-redirect="true" />

    <mvc:default-servlet-handler />


    <bean
        class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
        <property name="webBindingInitializer">
            <bean
                class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer" />
        </property>
    </bean>


    <!-- Thymeleaf konfiguracja resolverów: -->
    <bean id="templateResolver"
        class="org.thymeleaf.templateresolver.ServletContextTemplateResolver">
        <property name="prefix" value="/" /> 
        <property name="suffix" value=".html" />
        <property name="templateMode" value="HTML5" />
        <property name="characterEncoding" value="UTF-8" />
        <property name="cacheable" value="false" />
        <property name="order" value="0"></property>
    </bean>
<!--
     <bean id="thymeleafViewResolver" class="org.thymeleaf.spring4.view.ThymeleafViewResolver">
        <property name="templateEngine" ref="templateEngine" />
        <property name="characterEncoding" value="UTF-8" />
        <property name="contentType" value="text/html; charset=UTF-8" />
        <property name="order" value="1" />
    </bean>
 -->
    <bean id="thymeleafViewResolver" class="org.thymeleaf.spring4.view.AjaxThymeleafViewResolver">
        <property name="viewClass"  value="org.thymeleaf.spring4.view.FlowAjaxThymeleafView" />
        <property name="templateEngine" ref="templateEngine" />
        <property name="order" value="1" />
    </bean>


    <bean id="templateEngine" class="org.thymeleaf.spring4.SpringTemplateEngine">
        <property name="templateResolver" ref="templateResolver" />
        <property name="additionalDialects">
            <set>
                <bean class="nz.net.ultraq.thymeleaf.LayoutDialect" />
            </set>
        </property>
    </bean>

    <import resource="webflow.xml" />
        <!-- Spring WebFlow -->
    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass"
            value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/flows" />
        <property name="suffix" value=".html" />
        <property name="order" value="2"></property>
    </bean> 


</beans>

webflow.xml file:

    <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:webflow="http://www.springframework.org/schema/webflow-config"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd 
http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context-4.0.xsd 
http://www.springframework.org/schema/webflow-config 
http://www.springframework.org/schema/webflow-config/spring-webflow-config-2.4.xsd">


    <bean class="org.springframework.webflow.mvc.servlet.FlowHandlerMapping">
        <property name="flowRegistry" ref="flowRegistry" />
        <property name="order" value="0" />
    </bean>

    <bean class="org.springframework.webflow.mvc.servlet.FlowHandlerAdapter">
        <property name="flowExecutor" ref="flowExecutor" />
    </bean>

    <webflow:flow-registry id="flowRegistry"
        base-path="/flows" flow-builder-services="flowBuilderServices">
        <webflow:flow-location id="addQuiz"
            path="/adding-quiz/add-quiz-flow.xml"/>

    </webflow:flow-registry>

    <webflow:flow-executor id="flowExecutor"
        flow-registry="flowRegistry" />

    <webflow:flow-builder-services id="flowBuilderServices"
        view-factory-creator="mvcViewFactoryCreator" />

    <bean id="mvcViewFactoryCreator"
        class="org.springframework.webflow.mvc.builder.MvcViewFactoryCreator">
        <property name="viewResolvers" ref="thymeleafViewResolver" />
    </bean>

</beans>

and add-quiz-flow.xml:

<flow xmlns="http://www.springframework.org/schema/webflow"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/webflow 
  http://www.springframework.org/schema/webflow/spring-webflow-2.4.xsd"
start-state="quizName">

<view-state id="quizName" view="flows/adding-quiz/quizName" >
    <transition on="addQuestion" to="quizQuestion" />
</view-state>

<view-state id="quizQuestion" view="flows/adding-quiz/quizQuestion">
    <transition on="nextQuestion" to="quizQuestion" />
    <transition on="finish" to="finish" />
</view-state>

<end-state id="finish"/> <!--  Jakies "zapisano do bazy, czy cos -->

And now: Property viewResolvers in mvcViewFactoryCreator bean is set to viewResolver(so it is pointing at InternalResourceViewResolver).My flow works fine, but it is doesn't include thymleaf template fragment,There is only simple pages, without header and footer. As I read in thymeleaf docs : Integrating Thymeleaf and WebFlow I am supposed to add thymeleafViewResolver and change property in mvcViewFactoryCreator from

<property name="viewResolvers" ref="viewResolver" />

to

<property name="viewResolvers" ref="thymeleafViewResolver" />

In this configuration whole applcation works fine (templates are included), but when trying to start flow, typing:http://localhost:8070/addQuiz?quizName I'm, getting folowing error:

`org.thymeleaf.exceptions.TemplateInputException: Error resolving template "/adding-quiz/quizName", template might not exist or might not be accessible by any of the configured Template Resolvers`

Do you have any idea, where I'm going wrong?

like image 231
rpieniazek Avatar asked Aug 15 '15 16:08

rpieniazek


1 Answers

Ok, it seems that I've handled the problem. There were few mistakes: Firstly, I've changed

<view-state id="quizName" view="/adding-quiz/quizName">

to

<view-state id="quizName" view="/flows/adding-quiz/quizName">

After that I found a bug in html file. Now, configuration looks working properly.

like image 117
rpieniazek Avatar answered Nov 15 '22 12:11

rpieniazek