การตอบสนอง Ajax ไม่ทำงานกับ Spring Webflow

ฉันกำลังพยายามสร้างแอปพลิเคชันโดยใช้ jQuery Ajax และ Spring WebFlow ฉันสามารถส่งค่าไปยังคอนโทรลเลอร์ได้ แต่ไม่ได้รับทั้งหน้าเป็นการตอบกลับแทนที่จะเป็นเฉพาะ <script>

การโทร Ajax โดยใช้ jquery

$.ajax({
    type:"POST",
    data:country,
    url:$("#welcomeForm").attr("action")+"&_eventId_country&ajaxSource_country"+"&countryName="+country,
    success:function(states){
        console.log(states);
    }
});

โฟลว์.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 ฉันสามารถส่งคำขอไปยังผู้ควบคุมได้ แต่ไม่สามารถรับการตอบกลับ (รับทั้งหน้า) หรือแสดงการตอบกลับในหน้านั้นได้


person Vaskar Ray Karmakar    schedule 25.01.2014    source แหล่งที่มา
comment
หากคุณได้รับการตอบกลับทั้งหน้า ดูเหมือนว่าคุณกำลังเรียกวิธีการควบคุมที่ไม่ถูกต้อง คุณได้ลองแก้ไขข้อบกพร่องแล้วหรือยัง? stateS คืออะไร ตัวแปรคลาสบางตัวคืออะไร states มีไว้เพื่ออะไรทั้งในโค้ด JavaScript และ Java บนคอนโทรลเลอร์จะไม่ถูกบันทึกทุกที่และในการเรียก AJAX คุณกำลังแจ้งเตือนตัวแปรที่ไม่ได้กำหนดไว้ในฟังก์ชัน   -  person t0mppa    schedule 26.01.2014
comment
คุณสามารถโพสต์การตอบกลับแบบเต็มหน้าที่คุณได้รับและ URL ที่โพสต์ได้หรือไม่   -  person Angular University    schedule 01.02.2014
comment
มันเป็นเพียงหน้า html   -  person Vaskar Ray Karmakar    schedule 03.02.2014


คำตอบ (1)


ใน WebMvcConfig ตรวจสอบให้แน่ใจว่าคุณได้กำหนดค่า AjaxThymeLeafViewResolver อย่างถูกต้อง ต้องระบุชื่อ bean เป็น ทุกประการ @Bean(name = "thymeleafViewResolver")

ทำไม ฉันเดาว่าถ้าไม่ได้ตั้งชื่อ AjaxThymeleafViewResolver bean ชื่อ bean เริ่มต้นจะเป็นเพียง ajaxThymeleafViewResolver หรือบางอย่างในขณะที่ spring สนใจเฉพาะ bean ที่เรียกว่า thymeleafViewResolver ซึ่งคล้ายกับอินเทอร์เฟซและการใช้งานที่เป็นรูปธรรม เช่น คุณกำลังพูดกับสปริงว่าเราต้องการกำหนดค่า thymeleafViewResolver ให้เป็นอินสแตนซ์ของ AjaxThymeleafViewResolver เพื่อให้การจัดการ ajax แบบพิเศษสำหรับแฟรกเมนต์บางส่วน

นี่คือการกำหนดค่าของฉัน นี่เป็นรายละเอียดที่ละเอียดอ่อนจริงๆ และพบได้ในหนึ่งในคำแนะนำ mvc ของ thymeleaf spring

<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

การแมป xml นั้นลงใน java โดยใช้ spring boot มีดังต่อไปนี้

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

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

มันบอกว่าเมื่อคุณโพสต์ ajax ไปยังจุดสิ้นสุดของ Spring Web Flow flowExecutionUrl โดยจัดหา _eventId=updateData เป็น 1 ของฟิลด์ฟอร์มที่ต่อเนื่องกัน ฯลฯ Spring Web Flow จะส่งมาร์กอัป html กลับมาสำหรับส่วน hotelData ด้านบนเท่านั้น โดยอาจเป็นราคาโรงแรมใหม่ ซึ่งก็คือ ทำให้พร้อมใช้งานผ่านการผูกโมเดลของ hotel

เคล็ดลับสำหรับมือโปร:

ระมัดระวังตัวเลือกการตรวจสอบและผูกให้มาก มันคุ้มค่าที่จะเล่นกับพวกเขาถ้าไม่มีอะไรได้ผลสำหรับคุณจนถึงจุดนี้ ตัวอย่างเช่น หากคุณตั้งค่าการเชื่อมโยงเป็นเท็จ จะไม่มีฟิลด์ฟอร์มที่ส่งในคำขอ ajax จะถูกผูกกับโมเดล hotel ที่ระบุในสถานะมุมมอง flow.xml ตามข้างต้น

transition on="updateData" validate=true | false 
transition on="updateData" bind=true | false
person reversebind    schedule 21.02.2018