Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Webflow Streaming

I have problem with the execution of streaming actions (http://docs.spring.io/autorepo/docs/webflow/2.3.x/reference/html/actions.html#streaming-actions) in Spring Webflow/JSF application. I have seen that some people solve it using the MVC controllers, but I would like to use it as follows.

I am using Tomcat 7.01, Spring WebFlow 2.3.1 and Primefaces 4.0.

The problem is that when I want to download a file, nothing happens. The code is executed, but nothing happens in browser. Does anyone know what could be wrong, maybe some other setting in some xml, ...?

Thanks a lot!

My action looks like:

@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();
    }
}

My transition definition inside the view state looks like:

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

And the commandlink to execute the action:

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

And the server outputs the following log after I click the 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[org.springframework.webflow.persistence.JpaFlowExecutionListener@fc6c341, org.springframework.faces.webflow.FlowFacesContextLifecycleListener@242bb032, org.springframework.webflow.security.SecurityFlowExecutionListener@5c30045e]
FlowExecutionImpl:249 - Resuming in org.springframework.webflow.mvc.servlet.MvcExternalContext@3bdea0ee
ViewState:230 - Event 'printCSV' returned from view [JSFView = '/WEB-INF/flows/export/export.xhtml']
ActionExecutor:49 - Executing [EvaluateAction@6f32e75a expression = printCSVAction, resultExpression = [null]]
AnnotatedAction:142 - Putting action execution attributes map[[empty]]
ActionExecutor:49 - Executing si.zitnik.test.register.action.PrintCSVAction@450b3634
ActionExecutor:53 - Finished executing si.zitnik.test.register.action.PrintCSVAction@450b3634; result = success
AnnotatedAction:149 - Clearing action execution attributes map[[empty]]
ActionExecutor:53 - Finished executing [EvaluateAction@6f32e75a expression = printCSVAction, resultExpression = [null]]; result = success
Transition:213 - Executing [Transition@2eb3f775 on = printCSV, to = [null]]
DefaultFlowExecutionRepository:121 - Putting flow execution '[FlowExecutionImpl@5643cdce flow = 'account', flowSessions = list[[FlowSessionImpl@4ae5fba4 flow = 'account', state = 'export', scope = map['uiUtils' -> si.zitnik.test.register.ui.utils.UIUtils@3dae0070, 'user' -> si.zitnik.test.register.model.User@c1750ef]], [FlowSessionImpl@44f2d11e flow = 'export', state = 'search', scope = map['viewScope' -> map['flowSerializedViewState' -> [FlowSerializedView@212f3aff 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
like image 896
user3012806 Avatar asked Nov 10 '22 01:11

user3012806


1 Answers

The problem is that the p:commandLink uses ajax by default. To submit a form in a non-ajax fashion, we must include ajax="false" or use some other element (e.g., h:commandButton).

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

It works now!

like image 151
user3012806 Avatar answered Nov 14 '22 21:11

user3012806