简体   繁体   English

Spring Web Flow Passing Model Object from Flow to Controller

[英]Spring Web Flow Passing Model Object from Flow to Controller

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

My use case is as follows:我的用例如下:

I have a flow and the end state displays a model object that contains some calculated results.我有一个流程,最后 state 显示 model object 包含一些计算结果。 That works good.效果很好。 I also have a link on the page to generate a pdf to display the results.我在页面上还有一个链接可以生成 pdf 来显示结果。 That too works fine if I manually set the model object.如果我手动设置 model object,那也可以正常工作。

So how do I get the model object used in the flow to the controller?那么如何获得在流向 controller 的流程中使用的 model object? Or is there a cleaner way to view pdfs using webflow?或者有没有更简洁的方式来使用 webflow 查看 pdf?

Thanks谢谢

There unfortunately is not an easy way to do this.不幸的是,没有一种简单的方法可以做到这一点。 Webflow maintains all objects and their states at different times within its own repository. Webflow 在其自己的存储库中维护所有对象及其状态在不同时间。 So a model object at e1s2 will be a different physical object then e1s3 and so forth.因此,e1s2 处的 model object 将是不同的物理 object 然后 e1s3 等等。

The easiest way I can think of is to store the object in the session as part of an end step.我能想到的最简单的方法是将 object 作为结束步骤的一部分存储在 session 中。 You can then redirect the user to the controller and get/remove the object from the session.然后,您可以将用户重定向到 controller 并从 session 中获取/删除 object。

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:编辑:因为在 session 中放置流控制对象可能会成为一个烦人的过程,让自己参与其中可能不是最好的解决方案,但这里有一个如何做到这一点的示例:

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 分配给 session 并且需要使用 endModelObject 将其拉回

The RequestContext is a webflow owned object and you would pass in this will get you the pdf byte array into the session. RequestContext 是一个 webflow 拥有的 object 并且您将传递给您 pdf 字节数组到 session 中。 You can assign that using the action-state element from webflow您可以使用 webflow 中的action-state元素进行分配

<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.由于它现在位于 session 中,因此链接必须点击您想要的 controller 并直接从 session 中拉出。

@John V. thank you your post did help me in the right direction. @John V. 谢谢你的帖子确实帮助我朝着正确的方向前进。 This is what I have working now:这就是我现在的工作:

in my flow.xml在我的流程中。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在我的 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: class 包含以下内容:

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.我不确定这是否是最有效的方法,但我愿意接受任何建议。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM