gpt4 book ai didi

spring-mvc - 将 Spring WebFlow 与 Thymeleaf 集成

转载 作者:行者123 更新时间:2023-12-04 20:37:57 25 4
gpt4 key购买 nike

我正在使用 Thymeleaf 模板片段开发 SpringMVC 应用程序。我想添加简单的流程。这是我的项目结构和配置:

enter image description here

我的 spring-servlet.xml 文件:

    <?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 文件:
    <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>

并添加-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 -->

现在:
mvcViewFactoryCreator bean 中的属性 viewResolvers 设置为 viewResolver(所以它指向 InternalResourceViewResolver)。我的流程工作正常,但它不包含 thymleaf 模板片段,只有简单的页面,没有页眉和页脚。
正如我在 thymeleaf 文档中读到的: Integrating Thymeleaf and WebFlow
我应该添加 thymeleafViewResolver 并更改 mvcViewFactoryCreator 中的属性
<property name="viewResolvers" ref="viewResolver" />


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

在此配置中,整个应用程序工作正常(包括模板),但在尝试启动流程时,键入: http://localhost:8070/addQuiz?quizName我,得到以下错误:
`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`

你知道我哪里出错了吗?

最佳答案

好的,看来我已经解决了这个问题。有几个错误:
首先,我变了

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


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

之后我在 html 文件中发现了一个错误。现在,配置看起来工作正常。

关于spring-mvc - 将 Spring WebFlow 与 Thymeleaf 集成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32027060/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com