简体   繁体   English

在Spring中的控制器之间持久化对象数据

[英]Persisting object data between controllers in Spring

I have an object called Request which is the main object of my portal that stores all the information of a request, the user, their form selections, etc. How to I persist all the previous information in between the different forms? 我有一个名为Request的对象,这是我门户网站的主要对象,用于存储请求,用户,他们的表单选择等的所有信息。如何在不同的表单之间保留所有先前的信息? In each .GET I have to set the request object, and then in each .POST, the only information that is passed to it is what is in the forms on the .GET pages. 在每个.GET中,我必须设置请求对象,然后在每个.POST中,唯一传递给它的信息就是.GET页上的表单中的信息。 So on each page I have to have hidden fields such as 因此,在每个页面上我都必须具有隐藏字段,例如

<form:input path='requestId' style='display:none' />

<form:input path='currentUserId' style='display:none' />

<form:input path="step" style='display:none' />

I need these fields, and would also like to have the rest of the fields in the request object that are not on the form without having to repeat that for each and every field in my object. 我需要这些字段,并且还希望让请求对象中的其余字段不在表单上,​​而不必为对象中的每个字段重复该字段。

@RequestMapping(value = "/review", method = RequestMethod.GET)
public ModelAndView showCorReview(@RequestParam(value = "requestId") String requestId,
                                  @CookieValue(value = "EMP_ID", defaultValue = "168") int userId)
{
    Request request = requestManager.getRequestById(Integer.parseInt(requestId));

    request.setCurrentUserId(userId);

    String pageTitle = "2.1: Initiate New Service Request -- (Review)";
    ModelAndView mav = new ModelAndView();
    mav.setViewName("newRequest/review");
    mav.addObject("title", pageTitle);
    mav.addObject("request", request);
    mav.addObject("cpm", userManager.getUserById(request.getCpm()).getName());
    return mav;
}

@RequestMapping(value = "/review", method = RequestMethod.POST)
public String saveReview(Request request, @RequestParam(value = "commentData", required = false) String[] additionalComments)
{
    if (additionalComments != null)
        commentLogManager.addCommentLog(additionalComments, request);

    if (request.getRejectReason() == "")
    {
        request.setCpm(admin.getCPM(request.getContract()).getId());
        request.setCor(admin.getCOR(request.getContract()).getId());
        requestManager.updateRequest(request);           
    }
    else
    {
        if (request.getSubmitType().equals("return"))
        {
            request.setNextStep(1);
            requestManager.moveRequestToStep(request);
        }
    }
    return worksheetUrl + request.getId();
}

Alternatately I could also in the .POST do the Request request = requestManager.getRequestById(Integer.parseInt(requestId)) 另外,我也可以在.POST中执行Request request = requestManager.getRequestById(Integer.parseInt(requestId))

Then use setters on all the form fields, but again, I would prefer the data to actually persist on it's own without explicitly calling that. 然后在所有表单字段上使用setter,但同样,我希望数据实际上独立存在而无需显式调用。

@Tim, if I understood your requirement correctly, you have a sequence of forms and you would like to transfer information from one form to the next without having to hit a database or copy over request variables from one form to another. @Tim,如果我正确理解了您的要求,则您有一系列表格,并且您希望将信息从一种表格转移到另一种表格,而不必访问数据库或将请求变量从一种表格复制到另一种表格。 I could support @JB Nizel's suggestion of employing HTTP Session, but you may not want to make the session "heavy"; 我可以支持@JB Nizel提出的使用HTTP会话的建议,但是您可能不希望使会话“沉重”。 after all, it is the next most persistent scope after application-scope. 毕竟,它是继application-scope之后的第二个最持久的作用域。

Spring Web Flow may be the answer. Spring Web Flow可能是答案。 Flow-scope will allow you to build up form-state as the user progresses from one form to the next. Flow-scope允许您随着用户从一种表单前进到另一种表单而建立表单状态。 Plus you don't have to worry about form-scoped variables when the flow finishes, unlike session variables that you don't want to have lingering around. 另外,您无需担心流程结束时表单范围的变量,这与您不想徘徊的会话变量不同。

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

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