简体   繁体   中英

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? 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. 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))

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.

@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. I could support @JB Nizel's suggestion of employing HTTP Session, but you may not want to make the session "heavy"; after all, it is the next most persistent scope after application-scope.

Spring Web Flow may be the answer. Flow-scope will allow you to build up form-state as the user progresses from one form to the next. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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