Spring4 Webflow Thymeleaf2 Tiles Ajax: все работает, кроме
Моя конфигурация плиток и Ajax Spring4 Thymeleaf2 работает, кроме самого последнего шага. Моя HTML-страница не обновляется. Я знаю, что метод вызывается, и результат возвращается из моего метода обслуживания. Я думаю, что моя ошибка заключается в том, как я отношусь к "идентификатору" в плитке helloworld.
Я покажу всю свою конфигурацию, так как в Интернете есть несколько примеров конфигурации xml, и один из них неправильный.
Помощь на последнем этапе будет высоко ценится.
Ключевые части конфигурации:
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>
Представления 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>
Сервис Конфиг
<!--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-->
<?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>
** HTML Tiles для 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>
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;
}
1 ответ
Понял!
Плитки с песочницей Thymeleaf были очень полезными, как и этот пост (нижняя часть темы)
Я сделал несколько изменений.
- Я сделал фрагмент ajax отдельным html-файлом, hw4.html. ПРИМЕЧАНИЕ: я называю дату дважды в этом тесте. Один раз непосредственно через тимелист (который подтверждает обновление плитки) и один раз из боба (который подтверждает, что бин находится в области действия и обновляется). Таким образом я мог подтвердить, что это не было проблемой с моим бобом или определением объема.
<div id="data"> <h3 th:text="${#dates.createNow()}"></h3>
<span th:text="${mydate}? ${mydate.date}">
the date goes here</span></div>
Также обратите внимание, что нет th: фрагмент или плитки: фрагмент. Это больше не нужно.
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>
Шаблон плиток должен использовать плитки: заменить
<!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>
Остальное почти так же, как в вопросе. Я не вносил изменения в контекст приложения.
Итак, это работает. У меня ушло около недели, чтобы проработать тимелист и аякс. Надеюсь, что этот пост будет намного проще для других.
Я должен подчеркнуть, что это усилия новичка. Эксперты могут предложить предлагаемые улучшения или ошибки в этом посте