我正在尝试使用jQuery Ajax和Spring WebFlow构建应用程序。我可以向控制器发送值,但不会将整个页面作为响应而不是特定的<script>
使用jquery进行Ajax调用
$.ajax({
type:"POST",
data:country,
url:$("#welcomeForm").attr("action")+"&_eventId_country&ajaxSource_country"+"&countryName="+country,
success:function(states){
console.log(states);
}
});
Flow.xml:
<?xml version="1.0" encoding="UTF-8"?>
<flow xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/webflow" xsi:schemaLocation="http://www.springframework.org/schema/webflow http://www.springframework.org/schema/webflow/spring-webflow-2.0.xsd">
<var class="com.model.Welcome" name="welcome"/>
<on-start>
<evaluate expression="springWebFlow.countryList()" result="flowScope.countries"/>
</on-start>
<view-state id="welcome" model="welcome" redirect="false" view="/WEB-INF/views/welcome.jsp">
<transition on="country" bind="false">
<evaluate expression="springWebFlow.stateList(flowRequestContext)" result="flowScope.states" result-type=""/>
</transition>
<transition on="welcome" to="actionState1"/>
</view-state>
<end-state commit="false" id="actionState1" view="/WEB-INF/views/myDetails.jsp"/>
</flow>
控制器:
public @ResponseBody List<State> stateList(RequestControlContext context) throws Exception {
List<State> states= new ArrayList<State>() ;
State stateName= new State();
String countryName= context.getRequestParameters().get("countryName");
if(countryName.equals("India")){
stateName.setStateName("Delhi");
states.add(stateName);
}
return states;
}
我不想使用Spring JavaScript而不使用Tiles。我可以向控制器发送请求,但无法获得响应(获取整页)或在页面中显示响应。
答案 0 :(得分:0)
在WebMvcConfig
中,确保您已正确配置AjaxThymeLeafViewResolver
。 bean名称必须以 EXACTLY @Bean(name = "thymeleafViewResolver")
的形式提供。
为什么呢?好吧,我想没有命名AjaxThymeleafViewResolver
bean,默认的bean名称只是ajaxThymeleafViewResolver
或者其他东西,而spring只关心一个名为thymeleafViewResolver
的bean,类似于接口和具体实现。例如,你要说我们要将thymeleafViewResolver
配置为AjaxThymeleafViewResolver
的实例,以便为部分片段提供专门的ajax处理。
这是我的配置。这是一个非常微妙的细节,它可以在百里香弹簧mvc指南中找到。
<bean id="thymeleafViewResolver" class="org.thymeleaf.spring4.view.AjaxThymeleafViewResolver">
<property name="viewClass" value="org.thymeleaf.spring4.view.FlowAjaxThymeleafView" />
<property name="templateEngine" ref="templateEngine" />
</bean>
http://www.thymeleaf.org/doc/tutorials/3.0/thymeleafspring.html#spring-webflow-integration
使用spring boot将xml映射到java中。
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
@Autowired
private WebFlowConfig webFlowConfig;
@Autowired
private SpringTemplateEngine springTemplateEngine;
@Bean
public FlowHandlerMapping flowHandlerMapping() {
FlowHandlerMapping handlerMapping = new FlowHandlerMapping();
handlerMapping.setOrder(-1);
handlerMapping.setFlowRegistry(webFlowConfig.flowRegistry());
return handlerMapping;
}
@Bean
public FlowHandlerAdapter flowHandlerAdapter() {
FlowHandlerAdapter handlerAdapter = new FlowHandlerAdapter();
handlerAdapter.setFlowExecutor(webFlowConfig.flowExecutor());
handlerAdapter.setSaveOutputToFlashScopeOnRedirect(true);
return handlerAdapter;
}
@Bean(name = "thymeleafViewResolver")
public AjaxThymeleafViewResolver ajaxThymeleafViewResolver() {
AjaxThymeleafViewResolver viewResolver = new AjaxThymeleafViewResolver();
viewResolver.setViewClass(FlowAjaxThymeleafView.class);
viewResolver.setTemplateEngine(springTemplateEngine);
return viewResolver;
}
}
接下来您需要了解的是如何理解<render fragments=...>
,因为文档不清楚IMO。
<view-state id="detail" view="bookingDetail" model="hotel">
<transition on="updateData">
<render fragments="hoteldata"/>
</transition>
</view-state>
hotelData
实际上指的是什么?看看html百万美元标记。
<div th:fragment="hotelData">
<h1 th:text="|The price is ${hotel.price}|">
</div>
它说当你对弹簧网络流端点flowExecutionUrl
做一个ajax帖子,提供_eventId=updateData
作为序列化表单字段之一等时,spring web flow只会发送回html标记为上面的hotelData
片段可能是通过hotel
的模型绑定提供的新酒店价格。
专业提示:
要非常小心验证和绑定选项。如果到目前为止没有什么能为你效力的话,它值得与它们一起玩。例如,如果将bind设置为false,则ajax请求中发送的任何表单字段都不会绑定到flow.xml视图状态中指定的hotel
模型。
transition on="updateData" validate=true | false
transition on="updateData" bind=true | false