2014-01-25 72 views
0

我正在嘗試使用jQuery Ajax和Spring WebFlow構建應用程序。我可以送價值控制器,但使用jQuery沒有得到全部頁面的響應,而不是具體的<script>Ajax響應不適用於Spring Webflow

使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

如果您將整頁作爲響應,聽起來像是您調用了錯誤的控制器方法。你嘗試過調試嗎?還有什麼'stateS',一些類變量? JavaScript和Java代碼中的「州」是什麼?在控制器上,它不會被保存在任何地方,並且在AJAX調用中,您正在提醒未在函數中定義的變量。 – t0mppa

+0

你可以發佈你收到的整個頁面回覆以及發佈的網址嗎? –

+0

它只是一個html頁面 –

回答

0

WebMvcConfig,請確保您已正確配置AjaxThymeLeafViewResolver。豆名必須提供爲正確@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

映射該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=...>因爲文檔不清晰海事組織。

<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的序列化表單字段等1提供_eventId=updateData,Spring Web Flow的只會發回的HTML標記的hotelData片段以上大概是通過hotel的模型綁定提供的新酒店價格。

專業提示:

要非常小心驗證和綁定選項。如果沒有任何東西能爲你工作,直到這一點,它的價值與他們一起玩耍。例如,如果將綁定設置爲false,那麼在ajax請求中發送的表單字段將不會綁定到上述的flow.xml視圖狀態中指定的hotel模型。

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