【问题标题】:Ajax response not working with Spring WebflowAjax 响应不适用于 Spring Webflow
【发布时间】:2014-02-16 20:49:21
【问题描述】:

我正在尝试使用 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。我可以向控制器发送请求,但无法获得响应(获取整个页面)或在页面中显示响应。

【问题讨论】:

  • 如果您收到整个页面作为响应,听起来您调用了错误的控制器方法。你试过调试吗?还有什么是stateS,一些类变量? JavaScript 和 Java 代码中的 states 是什么?在控制器上,它没有保存在任何地方,并且在 AJAX 调用中,您正在警告函数中未定义的变量。
  • 您能否发布您获得的完整页面回复以及发布的网址?
  • 只是一个html页面

标签: java jquery ajax spring-webflow


【解决方案1】:

WebMvcConfig 中,确保您已正确配置AjaxThymeLeafViewResolver。 bean 名称必须提供为 EXACTLY @Bean(name = "thymeleafViewResolver")

为什么?好吧,我想没有命名 AjaxThymeleafViewResolver bean,默认 bean 名称只是 ajaxThymeleafViewResolver 或其他东西,而 spring 只关心名为 thymeleafViewResolver 的 bean,类似于接口和具体实现。例如,您对 spring 说我们想要将 thymeleafViewResolver 配置为 AjaxThymeleafViewResolver 的实例,以便为部分片段提供专门的 ajax 处理。

这是我的配置。这是一个非常微妙的细节,可以在 thymeleaf spring 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;
    }
}

接下来你需要知道的是如何理解&lt;render fragments=...&gt;,因为文档并不清楚 IMO。

<view-state id="detail" view="bookingDetail" model="hotel">
    <transition on="updateData">
        <render fragments="hoteldata"/>
    </transition>
</view-state> 

hotelData 实际上指的是什么?查看 html thymeleaf 标记。

<div th:fragment="hotelData">
  <h1 th:text="|The price is ${hotel.price}|">
</div>

它的意思是,当您向 Spring Web Flow 端点 flowExecutionUrl 提供 _eventId=updateData 作为序列化表单字段之一等时,Spring Web Flow 只会发回 hotelData 片段的 html 标记以上大概是通过hotel的模型绑定提供的新酒店价格。

专业提示:

要非常小心 validate 和 bind 选项。如果到目前为止没有什么对你有用的话,和他们一起玩是值得的。例如,如果将 bind 设置为 false,则 ajax 请求中发送的任何表单字段都不会绑定到 flow.xml 视图状态中指定的 hotel 模型,如上。

transition on="updateData" validate=true | false 
transition on="updateData" bind=true | false

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-27
    • 1970-01-01
    相关资源
    最近更新 更多