2014-09-12 101 views
3

我在Spring Webflow/JSF應用程序中執行流操作(http://docs.spring.io/autorepo/docs/webflow/2.3.x/reference/html/actions.html#streaming-actions)時遇到問題。我已經看到有些人使用MVC控制器來解決它,但我想按如下方式使用它。Spring Webflow流

我使用的是Tomcat 7.01,Spring WebFlow 2.3.1和Primefaces 4.0。

問題是,當我想下載一個文件時,沒有任何反應。代碼被執行,但瀏覽器中沒有任何反應。有誰知道什麼可能是錯的,也許一些其他設置在一些XML,...?

非常感謝!

我的行動看起來像:

@Component 
public class PrintCSVAction extends AbstractAction { 
private static final String CSV_FILENAME = "export.csv"; 

public Event doExecute(RequestContext context) throws IOException { 
     HttpServletResponse response = (HttpServletResponse) context.getExternalContext().getNativeResponse(); 

     response.setHeader("Content-Disposition", "attachment; filename=\"" + CSV_FILENAME + "\""); 
     OutputStream out = response.getOutputStream(); 
     response.setContentType("text/csv; charset=UTF-8"); 
     out.write(new String("blablablab\n").getBytes("utf-8")); 
     out.flush(); 
     out.close(); 

     context.getExternalContext().recordResponseComplete(); 
     return success(); 
    } 
} 

視圖狀態在我的轉換定義是這樣的:

<view-state id="search" view="export.xhtml"> 
    <transition on="home" to="finish" /> 
    <transition on="printCSV"> 
     <evaluate expression="printCSVAction" /> 
    </transition> 
</view-state> 

而且commandlink爲執行動作:

<p:commandLink action="printCSV"> 
    <p:graphicImage value="/css/images/csv.png" /> 
</p:commandLink> 

而服務器在單擊命令鏈接後輸出以下日誌:

FlowHandlerMapping:108 - Mapping request with URI '/app/account' to flow with id 'account' 
FlowExecutorImpl:161 - Resuming flow execution with key 'e2s2 
SessionBindingConversationManager:67 - Locking conversation 2 
DefaultFlowExecutionRepository:106 - Getting flow execution with key 'e2s2' 
FlowDefinitionRegistryImpl:59 - Getting FlowDefinition with id 'account' 
FlowDefinitionRegistryImpl:59 - Getting FlowDefinition with id 'export' 
ConditionalFlowExecutionListenerLoader:87 - Loaded [3] of possible 3 listeners for this execution request for flow 'account', the listeners to attach are list[o[email protected]fc6c341, org.[email protected]242bb032, org[email protected]5c30045e] 
FlowExecutionImpl:249 - Resuming in [email protected]0ee 
ViewState:230 - Event 'printCSV' returned from view [JSFView = '/WEB-INF/flows/export/export.xhtml'] 
ActionExecutor:49 - Executing [[email protected] expression = printCSVAction, resultExpression = [null]] 
AnnotatedAction:142 - Putting action execution attributes map[[empty]] 
ActionExecutor:49 - Executing [email protected] 
ActionExecutor:53 - Finished executing [email protected]; result = success 
AnnotatedAction:149 - Clearing action execution attributes map[[empty]] 
ActionExecutor:53 - Finished executing [[email protected] expression = printCSVAction, resultExpression = [null]]; result = success 
Transition:213 - Executing [[email protected] on = printCSV, to = [null]] 
DefaultFlowExecutionRepository:121 - Putting flow execution '[[email protected] flow = 'account', flowSessions = list[[[email protected] flow = 'account', state = 'export', scope = map['uiUtils' -> [email protected], 'user' -> [email protected]]], [[email protected] flow = 'export', state = 'search', scope = map['viewScope' -> map['flowSerializedViewState' -> [[email protected] viewId = '/WEB-INF/flows/export/export.xhtml']]]]]]' into repository 
DefaultFlowExecutionRepository:128 - Adding snapshot to group with id 2 
SessionBindingConversationManager:78 - Putting conversation attribute 'scope' with value map['flashScope' -> map['messagesMemento' -> map[[empty]]]] 
SessionBindingConversationManager:99 - Unlocking conversation 2 

回答

1

問題是,p:commandLink默認使用ajax。要以非Ajax方式提交表單,我們必須包含ajax =「false」或使用其他元素(例如h:commandButton)。

<p:commandLink action="printCSV" ajax="false"> 
    <p:graphicImage value="/css/images/csv.png" /> 
</p:commandLink> 

它現在有效!