java Spring Web Flow 将模型对象从 Flow 传递到控制器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7082421/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 18:33:48  来源:igfitidea点击:

Spring Web Flow Passing Model Object from Flow to Controller

javaspringspring-mvcspring-webflow

提问by blong824

What is the proper way to pass a model object used in Spring Web Flow to a Controller?

将 Spring Web Flow 中使用的模型对象传递给控制器​​的正确方法是什么?

My use case is as follows:

我的用例如下:

I have a flow and the end state displays a model object that contains some calculated results. That works good. I also have a link on the page to generate a pdf to display the results. That too works fine if I manually set the model object.

我有一个流程,最终状态显示一个包含一些计算结果的模型对象。这很好用。我在页面上还有一个链接,可以生成 pdf 来显示结果。如果我手动设置模型对象,那也能正常工作。

So how do I get the model object used in the flow to the controller? Or is there a cleaner way to view pdfs using webflow?

那么如何将流中使用的模型对象获取到控制器呢?或者是否有更简洁的方式使用 webflow 查看 pdf?

Thanks

谢谢

采纳答案by blong824

@John V. thank you your post did help me in the right direction. This is what I have working now:

@John V. 谢谢你的帖子确实帮助我朝着正确的方向前进。这就是我现在的工作:

in my flow.xml

在我的 flow.xml

<view-state id="summary" view="summary.jsp">
    <on-entry>
        <set name="result" value="conversationScope.result" />
        <evaluate expression="printPDF" />
    </on-entry>
    <transition on="startOver" to="startOver" />
</view-state>

in my webflowContext.xml file

在我的 webflowContext.xml 文件中

<bean id="printPDF" class="com.example.actions.PrintPDF"/>

PrintPDF.class

打印PDF.class

public class PrintPDF extends AbstractAction {

    @Override
    public Event doExecute(RequestContext context) {

        Result obj = (Result)context.getFlowExecutionContext().getConversationScope().get("result");
        HttpServletRequest req = (HttpServletRequest) context.getExternalContext().getNativeRequest();
        req.getSession().setAttribute("result", obj);
        return success();
    }

}

in my controller

在我的控制器中

@RequestMapping(method=RequestMethod.GET, value="/pdf")
public ModelAndView showPDF(ModelMap model, HttpServletRequest request) {
    Result result = (Result)request.getSession().getAttribute("result");
    model.addAttribute("result", result);
    return new ModelAndView("PDF", model);
}

PDF is defined as a bean in my spring-pdf-views.xml file

PDF 在我的 spring-pdf-views.xml 文件中被定义为一个 bean

<bean id="PDF" class="com.example.view.PDF">
    <property name="url" value="/pdf/example.pdf" />
</bean>

That class contains the following:

该类包含以下内容:

public class PDF extends AbstractPdfStamperView {

    @Override
    protected void mergePdfDocument(Map<String, Object> model, PdfStamper stamper, 
            HttpServletRequest request, HttpServletResponse response) throws Exception {

        Result result = (Result)model.get("result");

        AcroFields form = stamper.getAcroFields();
//map form fields

and finally the jsp has a link like

最后jsp有一个链接

<a href="/pdf.html">

I hope that can help someone else. I am not sure if that is the most efficient way of doing it but I am open to any suggestions.

我希望这可以帮助别人。我不确定这是否是最有效的方法,但我愿意接受任何建议。

回答by John Vint

There unfortunately is not an easy way to do this. Webflow maintains all objects and their states at different times within its own repository. So a model object at e1s2 will be a different physical object then e1s3 and so forth.

不幸的是,没有一种简单的方法可以做到这一点。Webflow 在其自己的存储库中在不同时间维护所有对象及其状态。因此,e1s2 处的模型对象将是与 e1s3 等不同的物理对象。

The easiest way I can think of is to store the object in the session as part of an end step. You can then redirect the user to the controller and get/remove the object from the session.

我能想到的最简单的方法是将对象存储在会话中作为结束步骤的一部分。然后,您可以将用户重定向到控制器并从会话中获取/删除对象。

The alternative is to actually save the results in some persistent store (database for instance) and the link can have an ID which will allow you to pull the information and regenerate the results (if possible)

另一种方法是将结果实际保存在某个持久存储(例如数据库)中,并且链接可以有一个 ID,这将允许您提取信息并重新生成结果(如果可能)

Edit: Because placing flow control objects in the session can become an annoying process to involve yourself in this may not be the best solution, but here is an example on how to do it:

编辑:因为在会话中放置流控制对象可能会成为一个烦人的过程,让你自己参与这可能不是最好的解决方案,但这里有一个关于如何做的例子:

public class MainFlowController{
    ...rest of the flow's logic

    public void endFlow(RequestContext context){
       ModelObject obj = ...;
       HttpServletRequest req = (HttpServletRequest )context.getExternalContext().getNativeRequest();   
       req.getSession().setAttribute("endModelObject",obj );
    }
}

Here you are assigning the ModelObject to the session and would need to pull it back with endModelObject

在这里,您将 ModelObject 分配给会话,并且需要使用 endModelObject 将其拉回

The RequestContext is a webflow owned object and you would pass in this will get you the pdf byte array into the session. You can assign that using the action-stateelement from webflow

RequestContext 是一个 webflow 拥有的对象,您可以传入它,这将使您将 pdf 字节数组放入会话中。您可以使用action-statewebflow 中的元素分配它

<action-state id="setPDF">
    <evaluate expression="mainFlowController.endFlow(flowRequestContext)"/>;
    <transition to="endFlow"/>
</action-state>
<end-state id="endFlow" view="end.jsp"/>

Since its now in the session the link would have to hit the controller you want and pull from the session directly.

由于它现在在会话中,因此链接必须点击您想要的控制器并直接从会话中拉出。