Spring4 Webflow的Thymeleaf2瓷砖的Ajax:一切正常,除了 [英] Spring4 Webflow Thymeleaf2 Tiles Ajax: Everything works except

查看:1393
本文介绍了Spring4 Webflow的Thymeleaf2瓷砖的Ajax:一切正常,除了的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Spring4 Thymeleaf2瓷砖和Ajax的配置工作,除了最后一步。我的HTML网页不刷新。我知道这个方法被调用,其结果是从我的服务方法返回。我觉得我的错误是我对待在瓷砖的HelloWorld身份证的方式。

我会告诉我的整个配置,也有在网络上几个xml配置的例子,<一个href="https://github.com/sunnydyal/spring-webflow-thymeleaf-ajax-fragment-sample/tree/master/spring-webflow-thymeleaf-ajax-fragment-sample/src/main/webapp/WEB-INF"相对=nofollow>其中之一是错误的。

帮助在最后一步将是深深的AP preciated。

配置的关键部分:

Webflow的配置

&LT;! - Webflow的配置 - &GT; &LT;的Webflow:流注册ID =flowRegistry 基本路径=/ WEB-INF /流/ admin的流动建设者,服务=flowBuilderServices&GT; &LT;的Webflow:流的位置ID =testflow路径=testflow.xml/&GT; &LT;的Webflow:流的位置ID =xzcreatetesttype路径=xzcreatetesttype.xml/&GT; &LT; /的Webflow:流动注册表&GT; &LT;的Webflow:流执行者ID =flowExecutor 流动注册表=flowRegistry/&GT; &LT;的Webflow:流量商,服务ID =flowBuilderServices 鉴于工厂创始人=mvcViewFactoryCreator验证=验证发展=真/&GT;     &LT; bean类=org.springframework.webflow.mvc.servlet.FlowHandlerAdapter&GT; &LT;属性名=flowExecutorREF =flowExecutor/&GT; &LT;属性名=saveOutputToFlashScopeOnRedirect值=真/&GT; &LT; /豆&GT; &LT; bean类=org.springframework.webflow.mvc.servlet.FlowHandlerMapping&GT; &LT;属性名=flowRegistryREF =flowRegistry/&GT; &LT;属性名=订单的价值= - 1/&GT; &LT; /豆&GT;

查看配置

&LT;! - 观点的配置 - &GT; &LT;豆ID =templateResolver 类=org.thymeleaf.templateresolver.ServletContextTemplateResolver&GT; &LT;属性名=preFIX值=/ WEB-INF / HTML //&GT; &LT;HTML属性名=后缀值= /&GT; &LT;属性名=templateMode值=HTML5/&GT; &LT;属性名=缓存的价值=FALSE/&GT; &LT;属性名=订单值=0&GT;&LT; /性&gt; &LT; /豆&GT; &LT;! - Thymeleaf模板引擎 - &GT; &LT;豆ID =templateEngine级=org.thymeleaf.spring4.SpringTemplateEngine&GT; &LT;属性名=templateResolverREF =templateResolver/&GT; &LT;属性名=additionalDialects&GT; &LT;集&gt; &LT; bean类=org.thymeleaf.extras.tiles2.dialect.TilesDialect/&GT; &LT; /集&gt; &LT; /性&gt; &LT; /豆&GT; &LT;! - 解析控制器对瓷砖返回逻辑视图名称;一个看法 命名为解决将被视为一个瓦片定义的名称 - &GT; &LT;豆ID =tilesViewResolver级=org.thymeleaf.spring4.view.AjaxThymeleafViewResolver&GT; &LT;属性名=viewClass类 值=org.thymeleaf.extras.tiles2.spring4.web.view.FlowAjaxThymeleafTilesView/&GT; &LT;属性名=templateEngineREF =templateEngine/&GT; &LT;属性名=订单的价值=1/&GT; &LT;属性名=的characterEncoding值=UTF-8/&GT; &LT; /豆&GT; &LT;豆ID =的TilesConfigurer 类=org.thymeleaf.extras.tiles2.spring4.web.configurer.ThymeleafTilesConfigurer&GT; &LT;属性名=定义&GT; &LT;列表&gt; &LT;值GT; / WEB-INF / ** / views.xml&LT; /值GT; &LT; /列表&gt; &LT; /性&gt; &LT; /豆&GT;

服务配置

&LT;! - 在businesservice和auservice不相关的当前问题。未使用 - &GT;     &LT;豆ID =adminUserViewService级=jake.prototype2.service.user.AdminUserViewServiceImpl&GT; &LT;属性名=的businessServiceREF =的businessService/&GT; &LT;属性名=auServiceREF =auService/&GT; &LT; /豆&GT;

Javascript的配置

&LT;! - 一般配置 - &GT; &LT; - !以下​​是需要访问所需的Ajax调用JavaScript库 - &GT; &LT; MVC:资源映射=/资源/ **位置=/,类路径:/ META-INF /网络资源//&GT; &LT; MVC:注解驱动/&GT; &LT; MVC:默认的servlet处理程序/&GT;

&LT;!--- Webflow的流动配置 - &GT; &LT;流的xmlns =htt​​p://www.springframework.org/schema/webflow 的xmlns:XSI =htt​​p://www.w3.org/2001/XMLSchema-instance XSI:的schemaLocation =htt​​p://www.springframework.org/schema/webflow       http://www.springframework.org/schema/webflow/spring-webflow-2.4.xsd"> &LT;! - 在这里,我实例,仅仅具有获取和设置日期的方法和java.util.date领域豆 - &GT; &LT; VAR名称=指明MyDate 类=jake.prototype2.controller.viewwrapper.ViewWrapperHelloWorld/&GT; &LT;! - 所谓的通过控制器的第一个看法,但我可以建立在XML配置 - &GT; &LT;查看状态ID =hellotlv1&GT; &LT;关于=下一步=标准/&GT转变; &LT; /视图状态&GT;      &LT;! - 这证明瓷砖的工作。两个区域都集成到一个模板 - &GT; &LT;查看状态ID =标准&GT; &LT;关于=下一步=standardext/&GT转变; &LT; /视图状态&GT; &LT;! - 这就是我们所说的AJAX方法。首先,我们更新了指明MyDate豆,然后我们渲染为一个Ajax片段 - &GT; &LT;查看状态ID =standardext&GT; &LT;关于=下一步=xzlogin/&GT转变; &LT;转型期=doHelloWorld加入&GT; &LT;! - 我更新的日期。我已经使用这个设计之前,它的工作原理,即在作为参数传递变量, 更新值并返回查看通过的Webflow - &GT; &LT;评估前pression =adminUserViewService.doHelloWorld(指明MyDate,flowRequestContext) 结果=指明MyDate/&GT; &LT;呈现碎片=的HelloWorld/&GT; &LT; /转换&GT; &LT; /视图状态&GT; ..........

瓷砖配置

&LT;! - 瓷砖配置 - &GT; &LT; XML版本=1.0编码=UTF-8&GT?; &LT;!DOCTYPE瓷砖,定义PUBLIC - // Apache软件基金会// DTD瓷砖配置2.1 // EN http://tiles.apache.org/dtds/tiles-config_3_0.dtd"> &LT;瓷砖 - 定义&GT; &LT;! - 如果只是用瓷砖视图解析所有页面必须考虑到,无论他们是否使用瓦片定义 该路径的瓷砖已经结束,ablve在templateResolver配置中指定的路径的路径。因此,由于模板配置 指定WEB-INF / html和因为存在单个view.xml用和在HTML文件夹的所有HTML文件(模板和片段),没有进一步的 路径规范要求在这种情况下。 Webflow的将寻找定义的名称作为过渡到价值 - &GT; &LT;定义名称=hellotl模板=hellotltemplateType =thymeleaf/&GT; &LT;定义名称=hellotlv1模板=hellotlv1templateType =thymeleaf/&GT; &所述;! - standard.html是模板。 HW和HW2是插入到模板的瓷砖。请注意,没有必要指定后缀的.html在这里 - &GT; &LT;定义名称=标准模板=标准templateType =thymeleaf&GT; &LT;把属性名=种皮值=HW ::内容类型=thymeleaf/&GT; &LT;把属性名=TESTB值=HW2 ::测试TYPE =thymeleaf/&GT; &LT;把属性名=HelloWorld的价值=HW :: HelloWorld的类型=thymeleaf/&GT; &LT; /定义&GT; &LT;! - standardext是我们尝试Ajax调用。 - &GT; &LT;定义名称=standardext扩展=标准templateType =thymeleaf&GT; &LT;把属性名=种皮值=HW3 ::内容类型=thymeleaf/&GT; &LT;把属性名=TESTB值=HW2 ::空白/&GT; &LT;把属性名=HelloWorld的价值=HW3 :: HelloWorld的/&GT; &LT; /定义&GT; &LT; /瓦,定义&GT;

** HTML模板**

&LT;! - 这是模板瓷砖 - &GT; &LT;!DOCTYPE HTML&GT; &LT; HTML的xmlns =htt​​p://www.w3.org/1999/xhtml       的xmlns:TH =htt​​p://www.thymeleaf.org       的xmlns:瓦=htt​​p://www.thymeleaf.org       的xmlns:秒=htt​​p://www.thymeleaf.org       LANG =EN&GT;              &LT; HEAD&GT; &LT;冠军&GT;测试布局和LT; /标题&GT; &LT; /头&GT; &LT;身体GT; &LT;格砖:包括=种皮&GT; dodododo&LT; / DIV&GT; 到处出现本文 &LT;格砖:包括=TESTB&GT; kkkkkkk&LT; / DIV&GT; &LT;格砖:包括=HelloWorld的&GT;的Hello World&LT; / DIV&GT; &LT; /身体GT; &LT; / HTML&GT;

** HTML瓷砖阿贾克斯**

&LT;! - 这些都是用于Ajax调用&GT的两个区域;     &LT;!DOCTYPE HTML&GT;     &LT; HTML的xmlns =htt​​p://www.w3.org/1999/xhtml 的xmlns:TH =htt​​p://www.thymeleaf.org 的xmlns:瓦=htt​​p://www.thymeleaf.org 的xmlns:秒=htt​​p://www.thymeleaf.org郎=EN&GT;     &LT; HEAD&GT;     &LT; /头&GT;     &LT;身体GT; &LT;格砖:片段=内容&GT; &LT;脚本类型=文/ JavaScript的日:SRC =@ {/资源/体育馆/ dojo.js中}&GT;&LT; / SCRIPT&GT; &LT;脚本类型=文/ JavaScript的日:SRC =@ {/资源/春/ Spring.js}&GT;&LT; / SCRIPT&GT; &LT;脚本类型=文/ JavaScript的 日:SRC =@ {/资源/弹簧/弹簧的dojo.js}&GT;&LT; / SCRIPT&GT; &LT;形式ID =triggerform方法=邮报行动=&GT; &LT;输入类型=提交ID =HelloWorld的名字=_ eventId_doHelloWorld 值=立即更新! /&GT; &LT; /形式GT; &LT;脚本类型=文/ JavaScript的&GT; Spring.addDecoration(新Spring.AjaxEventDecoration({ formId:triggerform, elementId:的helloWorld, 事件:'的onclick })); &LT; / SCRIPT&GT; &LT; / DIV&GT; &LT; - 如果我不包括地砖:!片段,我得到一个EXCETION Apache的瓷砖 - &GT; &LT; D​​IV ID =数据砖:片段=HelloWorld的&GT; &LT;跨度日:文本=$ {指明MyDate} $ {mydate.date}?&GT; &LT;! - 因为该日期与Bean初始化,我总是看到日期在这里,但它不更新 - &GT; 日期的推移此处&lt; / SPAN&GT;&LT; / DIV&GT;     &LT; /身体GT;     &LT; / HTML&GT;

**服务方法**

 进口java.util.Date;
进口的java.util.HashMap;
进口的java.util.List;
进口org.springframework.beans.factory.annotation.Autowired;
    进口org.springframework.webflow.execution.RequestContext;
进口jake.prototype2.controller.utilities.SessionAndRequestParameters;
进口jake.prototype2.controller.viewwrapper.ViewWrapperHelloWorld;
进口jake.prototype2.controller.viewwrapper.ViewWrapperUserAdminLogin;
进口jake.prototype2.controller.welcome *。
进口jake.prototype2.model.business.Business;
进口jake.prototype2.model.user.UserAdmin;
进口jake.prototype2.model.user.UserStructureException;

公共类AdminUserViewServiceImpl实现AdminUserViewService
{
/ **不需要为你好WRLD功能的自动装配豆** /
@Autowired
私人的BusinessService businessService的;
@Autowired
私人AdminUserService auService;
/ *******服务方法************ /
/ **该方法采用的测试流程作为一个参数声明一个bean并将它作为返回的对象。
 *我不能保证这是因为我不知道该怎么Webflow的处理对象的最佳设计。
 *(从理论上说,它应该返回相同的附图)
 *的RequestContext不需要在这个例子中,但它是在许多方法一个有用的工具,如果需要访问
 * Webflow的或servlet环境中的对象。请注意,这是webflows,请求上下文
 * org.springframework.webflow.execution.RequestContext ** /
公共ViewWrapperHelloWorld doHelloWorld加入(ViewWrapperHelloWorld指明MyDate,RequestContext的CTX)
{
    SS.getLogger()调试(\ñ\ñ\ nReturning新日)。
    mydate.setDate(新日期(System.currentTimeMillis的()));
    返回指明MyDate;
}
 

解决方案

有了!

Thymeleaf沙箱瓷砖是因为是的这个帖子(螺纹底部)

我做了一些改动。

  1. 我做了ajax片段独立的HTML文件,hw4.html。注:我所说的两次日期在本次测试。一旦直接通过thymeleaf(这印证了瓷砖刷新),一旦从一个bean(这印证了bean是在范围和清新)。这样我可以证实,这是不符合我的豆或范围界定的问题。

&LT; D​​IV ID =数据&GT; &LT; H3日:文本=$ {#dates.createNow()}&GT;&LT; / H3&GT; &LT;跨度日:文本=$ {指明MyDate} $ {mydate.date}?&GT; 日期的推移此处&lt; / SPAN&GT;&LT; / DIV&GT;

还要注意的是,没有第:片段或切片:片段。此不再需要。

在views.xml看起来是这样的:

&LT; XML版本=1.0编码=UTF-8? &GT; &LT;!DOCTYPE瓷砖,定义PUBLIC - // Apache软件基金会// DTD瓷砖配置2.1 // EN http://tiles.apache.org/dtds/tiles-config_3_0.dtd"> &LT;瓷砖 - 定义&GT; &LT;! - 如果只是用瓷砖视图解析所有页面必须考虑到,无论他们是否使用瓦片定义 该路径的瓷砖已经结束,ablve在templateResolver配置中指定的路径的路径。因此,由于模板配置 指定WEB-INF / html和因为存在单个view.xml用和在HTML文件夹的所有HTML文件(模板和片段),没有进一步的 路径规范要求在这种情况下。 Webflow的将寻找定义的名称作为过渡到价值 - &GT; &LT;定义名称=hellotl模板=hellotltemplateType =thymeleaf/&GT; &LT;定义名称=hellotlv1模板=hellotlv1templateType =thymeleaf/&GT; &所述;! - standard.html是模板。 HW和HW2是插入到模板的瓷砖。请注意,没有必要指定后缀的.html在这里 - &GT; &LT;定义名称=标准模板=标准templateType =thymeleaf&GT; &LT;把属性名=种皮值=HW ::内容类型=thymeleaf/&GT; &LT;把属性名=TESTB值=HW2 ::测试TYPE =thymeleaf/&GT; &LT;把属性名=HelloWorld的价值=HW :: HelloWorld的类型=thymeleaf/&GT; &LT; /定义&GT; &LT;! - standardext是我们尝试Ajax调用。 - &GT; &LT;定义名称=standardext扩展=标准templateType =thymeleaf&GT; &LT;把属性名=种皮值=HW3 ::内容类型=thymeleaf/&GT; &LT;把属性名=TESTB值=HW2 ::空白TYPE =thymeleaf/&GT; &LT;把属性名=HelloWorld的价值=HW4TYPE =thymeleaf/&GT; &LT; /定义&GT; &LT; /瓦,定义&GT;

瓷砖模板应该用砖:更换

&LT;!DOCTYPE HTML&GT; &LT; HTML的xmlns =htt​​p://www.w3.org/1999/xhtml       的xmlns:TH =htt​​p://www.thymeleaf.org       的xmlns:瓦=htt​​p://www.thymeleaf.org       的xmlns:秒=htt​​p://www.thymeleaf.org       LANG =EN&GT;              &LT; HEAD&GT; &LT;冠军&GT;测试布局和LT; /标题&GT; &LT; /头&GT; &LT;身体GT; &LT;格砖:包括=种皮&GT; dodododo&LT; / DIV&GT; 到处出现本文 &LT;格砖:包括=TESTB&GT; kkkkkkk&LT; / DIV&GT; &LT;格砖:更换=HelloWorld的&GT;的Hello World&LT; / DIV&GT; &LT; /身体GT; &LT; / HTML&GT;

形式瓦pretty的很多改变:

&LT;格砖:片段=内容&GT; &LT;脚本类型=文/ JavaScript的日:SRC =@ {/资源/体育馆/ dojo.js中}&GT;&LT; / SCRIPT&GT; &LT;脚本类型=文/ JavaScript的日:SRC =@ {/资源/春/ Spring.js}&GT;&LT; / SCRIPT&GT; &LT;脚本类型=文/ JavaScript的 日:SRC =@ {/资源/弹簧/弹簧的dojo.js}&GT;&LT; / SCRIPT&GT; &LT;形式ID =triggerform方法=邮报行动=&GT; &LT;输入类型=提交ID =HelloWorld的名字=_ eventId_doHelloWorld 值=立即更新! /&GT; &LT; /形式GT; &LT;脚本类型=文/ JavaScript的&GT; Spring.addDecoration(新Spring.AjaxEventDecoration({ formId:triggerform, elementId:的helloWorld, 事件:'的onclick })); &LT; / SCRIPT&GT; &LT; / DIV&GT;

的其余部分是pretty的大致相同的问题。我没有做更改应用程序上下文。

那么,这个工程。我花了大约一个星期的工作,通过thymeleaf和Ajax。希望这篇文章将使它更容易为别人后面。

我要强调,这是一个新手的努力。专家们欢迎提供在这个岗位提出改善建议或错误

My Spring4 Thymeleaf2 Tiles and Ajax configuration is working except the very last step. My html page does not refresh. I know the method is called and the result is returned from my service method. I think my error is the way I treat the "id" in the helloworld tile.

I will show my entire configuration as there are few xml configuration examples on the web, and one of them is wrong.

Help on the last step would be deeply appreciated.

Key parts of configuration:

WebFlow Config

<!-- Webflow Config-->

<webflow:flow-registry id="flowRegistry"
		base-path="/WEB-INF/flows/admin" flow-builder-services="flowBuilderServices">
		<webflow:flow-location id="testflow" path="testflow.xml" />
		<webflow:flow-location id="xzcreatetesttype" path="xzcreatetesttype.xml" />
	</webflow:flow-registry>


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

	<webflow:flow-builder-services id="flowBuilderServices"
		view-factory-creator="mvcViewFactoryCreator" validator="validator" development="true" />
	
   
	<bean class="org.springframework.webflow.mvc.servlet.FlowHandlerAdapter">
		<property name="flowExecutor" ref="flowExecutor" />
		<property name="saveOutputToFlashScopeOnRedirect" value="true" />
	</bean>
		

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

Views Config

<!-- views config-->

	<bean id="templateResolver"
		class="org.thymeleaf.templateresolver.ServletContextTemplateResolver">
		<property name="prefix" value="/WEB-INF/html/" />
		<property name="suffix" value=".html" />
		<property name="templateMode" value="HTML5" />
		<property name="cacheable" value="false" />
		<property name="order" value="0"></property>
	</bean>


	<!-- Thymeleaf Template Engine -->
	<bean id="templateEngine" class="org.thymeleaf.spring4.SpringTemplateEngine">
		<property name="templateResolver" ref="templateResolver" />
		<property name="additionalDialects">
			<set>
				<bean class="org.thymeleaf.extras.tiles2.dialect.TilesDialect" />
			</set>
		</property>
	</bean>

	<!-- Resolves logical view names returned by Controllers to Tiles; a view 
		name to resolve is treated as the name of a tiles definition -->


	<bean id="tilesViewResolver" class="org.thymeleaf.spring4.view.AjaxThymeleafViewResolver">
		<property name="viewClass"
			value="org.thymeleaf.extras.tiles2.spring4.web.view.FlowAjaxThymeleafTilesView" />
		<property name="templateEngine" ref="templateEngine" />
		<property name="order" value="1" />
		<property name="characterEncoding" value="UTF-8" />
	</bean>
	
	<bean id="tilesConfigurer"
		class="org.thymeleaf.extras.tiles2.spring4.web.configurer.ThymeleafTilesConfigurer">
		<property name="definitions">
			<list>
				<value>/WEB-INF/**/views.xml</value>
			</list>
		</property>
	</bean>

Service Config

<!--the businesservice and auservice are not relevant to the current problem. Not used-->

    <bean id="adminUserViewService" class="jake.prototype2.service.user.AdminUserViewServiceImpl">
		<property name="businessService" ref="businessService" />
		<property name="auService" ref="auService" />
	</bean>

Javascript Config

<!--general config-->
<!-- The  following is required to  access the javascript libraries required for ajax calls-->
	<mvc:resources mapping="/resources/**" location="/, classpath:/META-INF/web-resources/" />
	<mvc:annotation-driven />
	<mvc:default-servlet-handler />

<!---WebFlow Flow config-->


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

<!-- Here I instantiate a bean that just has  get and set date methods and a java.util.date field-->
<var name="mydate"
		class="jake.prototype2.controller.viewwrapper.ViewWrapperHelloWorld" />

	<!-- called the first view through a controller but I could set up in xml config  -->
	<view-state id="hellotlv1">
		
		<transition on="next" to="standard" />
	</view-state>

     <!-- This confirms that tiles are working. Two tiles are integrated onto a template  -->
	<view-state id="standard">	
		<transition on="next" to="standardext" />
	</view-state>
	
	<!--  This is where we call an ajax method. First we update the mydate bean, then we render as an ajax fragment -->
	<view-state id="standardext">
		
		<transition on="next" to="xzlogin" />
		<transition on="doHelloWorld">
		<!--  I update the date. I have used this design before and it works, i.e., passing variable in as an argument, 
		updating value and returning to view via webflow -->
		<evaluate expression="adminUserViewService.doHelloWorld(mydate,flowRequestContext)"
				result="mydate" />
			<render fragments="helloworld" />
		</transition>

	</view-state>

..........

Tiles Config

<!--Tiles Config-->
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE tiles-definitions PUBLIC
	"-//Apache Software Foundation//DTD Tiles Configuration 2.1//EN"
	"http://tiles.apache.org/dtds/tiles-config_3_0.dtd">
	
<tiles-definitions>
	
	
		<!-- If just using tiles view resolver all pages must be defined in view regardless of whether or not they use tiles
		The path to the tiles is the path over and ablve the path specified in templateResolver configuration. Hence since template configuration
		specifies WEB-INF/html and since there is a single view.xml and all html files (templates and fragments) in the html folder, no further 
		path specification is required in this case. Webflow will be looking for the definition name as the "to" value of a transition -->
		<definition name="hellotl" template="hellotl" templateType="thymeleaf"/>
		<definition name="hellotlv1" template="hellotlv1" templateType="thymeleaf"/>
		
		
	     <!-- standard.html is a template. hw and hw2 are tiles inserted into the template. Note there is no need to specify a suffix .html here -->
		<definition name="standard" template="standard" templateType="thymeleaf">
		<put-attribute name="testa" value="hw :: content" type="thymeleaf"/>	
		<put-attribute name="testb" value="hw2 :: test"  type="thymeleaf"/>
		<put-attribute name="helloworld" value="hw :: helloworld"  type="thymeleaf"/>
		</definition>
	
	<!--  standardext is where we attempt an ajax call.  -->
	<definition name="standardext" extends="standard" templateType="thymeleaf">
		<put-attribute name="testa" value="hw3 :: content" type="thymeleaf"/>		
		<put-attribute name="testb" value="hw2 :: blank"/>	
		<put-attribute name="helloworld" value="hw3 :: helloworld"/>		
	</definition>

	
</tiles-definitions>

**HTML Template **

<!--this is the template for tiles-->
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" 
      xmlns:th="http://www.thymeleaf.org"
      xmlns:tiles="http://www.thymeleaf.org"
      xmlns:sec="http://www.thymeleaf.org"
      lang="en">
     
      
<head>

<title>test layout</title>


</head>

<body >

<div tiles:include="testa">dodododo</div>

		This text appears everywhere
<div tiles:include="testb">kkkkkkk</div>

<div tiles:include="helloworld">hello world</div>



</body>


</html>

**HTML Tiles for Ajax **

  <!-these are the two tiles used for the Ajax call>
    <!DOCTYPE html>
    <html xmlns="http://www.w3.org/1999/xhtml"
	xmlns:th="http://www.thymeleaf.org"
	xmlns:tiles="http://www.thymeleaf.org"
	xmlns:sec="http://www.thymeleaf.org" lang="en">
    <head>
    </head>
    <body>
	<div tiles:fragment="content">

		<script type="text/javascript" th:src="@{/resources/dojo/dojo.js}"></script>
		<script type="text/javascript" th:src="@{/resources/spring/Spring.js}"></script>
		<script type="text/javascript"
			th:src="@{/resources/spring/Spring-Dojo.js}"></script>

		<form id="triggerform" method="post" action="">
			<input type="submit" id="helloWorld" name="_eventId_doHelloWorld"
				value="Update now!" />
		</form>

		<script type="text/javascript">
			Spring.addDecoration(new Spring.AjaxEventDecoration({
				formId : 'triggerform',
				elementId : 'helloWorld',
				event : 'onclick'
			}));
		</script>
	</div>
	
	
	<!--If I do not include a tiles:fragment, I get an excetion from apache tiles-->
	<div id="data"  tiles:fragment="helloworld"> <span th:text="${mydate}? ${mydate.date}">
	<!--because the date is initialized with the bean, I always see the date here, but it does not update-->
		the date goes here</span></div>
    </body>
    </html>

**Service Method **

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.webflow.execution.RequestContext;
import jake.prototype2.controller.utilities.SessionAndRequestParameters;
import jake.prototype2.controller.viewwrapper.ViewWrapperHelloWorld;
import jake.prototype2.controller.viewwrapper.ViewWrapperUserAdminLogin;
import jake.prototype2.controller.welcome.*;
import jake.prototype2.model.business.Business;
import jake.prototype2.model.user.UserAdmin;
import jake.prototype2.model.user.UserStructureException;

public class AdminUserViewServiceImpl implements AdminUserViewService
{   
/**The autowired beans are not required for the hello wrld function**/
@Autowired
private BusinessService businessService;    
@Autowired
private AdminUserService auService; 
/*******service methods************/
/**The method takes a bean declared in test flow as an argument and returns it as the return object. 
 * I cannot guarantee this is the best design as I don't know how webflow deals with objects. 
 * (In theory, it should be returning the same reference)
 * RequestContext is not needed in this example, but it is a helpful tool in many methods if you need access to
 * webflow or servlet environment objects. Note, this is webflows, request context
 * org.springframework.webflow.execution.RequestContext**/
public ViewWrapperHelloWorld doHelloWorld(ViewWrapperHelloWorld mydate, RequestContext ctx)
{       
    SS.getLogger().debug("\n\n\nReturning with new date");
    mydate.setDate(new Date(System.currentTimeMillis()));       
    return mydate;
}

解决方案

Got It!

Thymeleaf sandbox tiles was super helpful as was this post (bottom of thread)

I made a few changes.

  1. I made the ajax fragment a stand alone html file, hw4.html. NOTE: I call the date twice in this test. Once directly through thymeleaf (which confirms the tile refreshes) and once from a bean (which confirms the bean is in scope and refreshing). This way I could confirm that it was not a problem with my bean or scoping.

<div id="data"> <h3 th:text="${#dates.createNow()}"></h3>
	<span  th:text="${mydate}? ${mydate.date}">
		the date goes here</span></div>
	

Also note that there is no th:fragment or tiles:fragment. This is no longer necessary.

The views.xml looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE tiles-definitions PUBLIC
	"-//Apache Software Foundation//DTD Tiles Configuration 2.1//EN"
	"http://tiles.apache.org/dtds/tiles-config_3_0.dtd">
	
<tiles-definitions>
	
	
		<!-- If just using tiles view resolver all pages must be defined in view regardless of whether or not they use tiles
		The path to the tiles is the path over and ablve the path specified in templateResolver configuration. Hence since template configuration
		specifies WEB-INF/html and since there is a single view.xml and all html files (templates and fragments) in the html folder, no further 
		path specification is required in this case. Webflow will be looking for the definition name as the "to" value of a transition -->
		<definition name="hellotl" template="hellotl" templateType="thymeleaf"/>
		<definition name="hellotlv1" template="hellotlv1" templateType="thymeleaf"/>
		
		
	     <!-- standard.html is a template. hw and hw2 are tiles inserted into the template. Note there is no need to specify a suffix .html here -->
		<definition name="standard" template="standard" templateType="thymeleaf">
		<put-attribute name="testa" value="hw :: content" type="thymeleaf"/>	
		<put-attribute name="testb" value="hw2 :: test"  type="thymeleaf"/>
		<put-attribute name="helloworld" value="hw :: helloworld"  type="thymeleaf"/> 
		</definition>
	
	<!--  standardext is where we attempt an ajax call.  -->
	<definition name="standardext" extends="standard" templateType="thymeleaf">
		<put-attribute name="testa" value="hw3 :: content" type="thymeleaf"/>		
		<put-attribute name="testb" value="hw2 :: blank" type="thymeleaf"/>	
	   <put-attribute name="helloworld" value="hw4"  type="thymeleaf"/> 
	</definition>

	
</tiles-definitions>

The tiles template should use tiles:replace

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" 
      xmlns:th="http://www.thymeleaf.org"
      xmlns:tiles="http://www.thymeleaf.org"
      xmlns:sec="http://www.thymeleaf.org"
      lang="en">
     
      
<head>

<title>test layout</title>


</head>

<body >

<div tiles:include="testa">dodododo</div>

		This text appears everywhere
<div tiles:include="testb">kkkkkkk</div>

<div tiles:replace="helloworld">hello world</div>  



</body>


</html>

The form tile is pretty much unchanged:

<div tiles:fragment="content">

		<script type="text/javascript" th:src="@{/resources/dojo/dojo.js}"></script>
		<script type="text/javascript" th:src="@{/resources/spring/Spring.js}"></script>
		<script type="text/javascript"
			th:src="@{/resources/spring/Spring-Dojo.js}"></script>

		<form id="triggerform" method="post" action="">
			<input type="submit" id="helloWorld" name="_eventId_doHelloWorld"
				value="Update now!" />
		</form>

		<script type="text/javascript">
			Spring.addDecoration(new Spring.AjaxEventDecoration({
				formId : 'triggerform',
				elementId : 'helloWorld',
				event : 'onclick'
			}));
		</script>
		
		
	</div>

The rest is pretty much the same as in the question. I didn't make changes to the application context.

So, this works. It took me about a week to work through thymeleaf and ajax. Hopefully this post will make it much easier for others that follow.

I should stress that this is a neophyte effort. Experts are welcome to offer suggested improvements or errors in this post

这篇关于Spring4 Webflow的Thymeleaf2瓷砖的Ajax:一切正常,除了的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆