【问题标题】:Spring4 Webflow Thymeleaf2 Tiles Ajax: Everything works exceptSpring 4 Webflux Thymeleaf 2 Tiles Ajax:一切正常,除了
【发布时间】:2015-11-27 08:05:10
【问题描述】:

除了最后一步,我的 Spring4 Thymeleaf2 Tiles 和 Ajax 配置正常工作。我的 html 页面没有刷新。我知道该方法被调用,结果是从我的服务方法返回的。我认为我的错误是我对待 helloworld 磁贴中的“id”的方式。

我将展示我的整个配置,因为网上很少有 xml 配置示例,one of them 是错误的。

对最后一步的帮助将不胜感激。

配置的关键部分:

WebFlow 配置

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

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

服务配置

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

<!--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-->
<?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 模板 **

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

**用于 Ajax 的 HTML Tiles **

  <!-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>
**服务方式**
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;
}

【问题讨论】:

    标签: ajax spring spring-webflow thymeleaf tiles


    【解决方案1】:

    知道了!

    Thymeleaf sandbox tilesthis post(线程底部)一样非常有帮助

    我做了一些更改。

    1. 我将 ajax 片段设置为独立的 html 文件 hw4.html。注意:我在这个测试中调用了两次日期。一次直接通过 thymeleaf(确认 tile 刷新),一次来自 bean(确认 bean 在范围内并刷新)。这样我可以确认这不是我的 bean 或范围界定的问题。

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

    还要注意没有 th:fragment 或 tiles:fragment。这不再是必需的。

    views.xml 如下所示:

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

    瓷砖模板应该使用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>

    表单图块几乎没有变化:

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

    其余的与问题中的几乎相同。我没有更改应用程序上下文。

    所以,这行得通。我花了大约一周的时间来处理百里香和 ajax。希望这篇文章能让其他人更容易理解。

    我应该强调这是一个新手的努力。欢迎专家在这篇文章中提出改进或错误的建议

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-02-19
      • 1970-01-01
      • 2018-05-11
      • 1970-01-01
      • 2015-12-22
      • 2013-07-02
      • 2016-06-25
      • 1970-01-01
      相关资源
      最近更新 更多