Spring Webflow 2 and bookmarkable URLs

此生再无相见时 提交于 2019-12-11 13:44:13

问题


Currently due to the Post/Redirect/Get pattern all flow urls are something like <site_url>/flow_name?execution=? and input GET parameters are not preserved. Thus the users can't copy the url, or bookmark it.

Any suggestions how could this be done neatly ?


回答1:


We can bookmark a SWF based application's URL by customising FlowHandlerAdapter of SWF API.

Here is a sample:

My SWF configuration file would have:

<bean id="flowController" class="org.springframework.webflow.mvc.servlet.FlowController">
      <property name="flowHandlerAdapter" ref="customFlowHandlerAdapter" /> 
     </bean>

     <bean id="customFlowHandlerAdapter" class="com.xyz.CustomFlowHandlerAdapter">
        <property name="flowExecutor" ref="flowExecutor" /> 
        <property name="flowUrlHandler" >
            <bean class="com.xyz.CustomURLFlowHandler" /> 
        </property>
     </bean>

My CustomFlowHandlerAdapter would have:

public class CustomFlowHandlerAdapter extends FlowHandlerAdapter {

...

@Override
    public ModelAndView handle(HttpServletRequest request,
            HttpServletResponse response, Object handler) throws Exception {
        FlowHandler flowHandler = (FlowHandler) handler;
        checkAndPrepare(request, response, false);
        String flowExecutionKey = this.getFlowUrlHandler()
                .getFlowExecutionKey(request);
        if (flowExecutionKey != null)
            try {
                ServletExternalContext context = createServletExternalContext(
                        request, response);
                FlowExecutionResult result = this.getFlowExecutor().resumeExecution(
                        flowExecutionKey, context);
                handleFlowExecutionResult(result, context, request, response,
                        flowHandler);
            } catch(org.springframework.webflow.execution.repository.NoSuchFlowExecutionException ex){
                response.sendRedirect(request.getRequestURI());
            } catch(org.springframework.webflow.execution.repository.BadlyFormattedFlowExecutionKeyException ex){
                response.sendRedirect(request.getRequestURI());
            } catch (FlowException e) {
                handleFlowException(e, request, response, flowHandler);
            }
....

Here Iam catching NoSuchFlowExecutionException and am redirecting to the exact flow URL without any parameters. Here you can capture and re-include your parameters

Thus I am able to bookmark my URL from any state(always flow starts from first) also I will be able to send my own parameters if required.




回答2:


you can always use and bookmark a link to one of your flow's start point.
for instance you can do <site_url>/flow_name?personId=123&projectId=456 assuming you have two inputs to your flow personId and projectId. But you need to know the url (you will have to give it to the users), you cannot use the one on your address bar.

even if you want to do that, you won't be able to use and bookmark a link to a specific state in your flow (unless you add some logic to the start of your flow to direct you to a specific event depending on the value of an input).



来源:https://stackoverflow.com/questions/13626336/spring-webflow-2-and-bookmarkable-urls

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!