简体   繁体   中英

Passing A Value from One Controller Method to Another

OK, so I'm still quite new at Spring MVC. What I'm trying to do is create a page that accepts an attachment. That functionality works, what I need it to do is attach it to the profile of an existing "employee". To do this I've created 2 methods one to set up the page, and the values that the page needs to start. I then have a 2nd method to upload the file.

The first method carries the value of the Employee ID until the very end, and never loses it, but it never gets picked up by the 2nd related method. I have another pair of pages that allow editing of the employee's profile that functions in the same way, and the ID carries over using the same methodology I use here.

If anyone can point out what I can do to get the ID over to the 2nd controller method. I would greatly appreciate it.

Here is what I have in the controller, it has already accepted the employee ID, by this point.

   @RequestMapping(value = "/EmpFile")
public ModelAndView loadEmpFilePage(
@RequestParam(value = "EmployeeID", required = false) Integer EmployeeID,
@ModelAttribute("employee")Employee employee, BindingResult result){
    ModelAndView mav = new ModelAndView();

    if(EmployeeID< 1)
        EmployeeID= null;

    if(EmployeeID== null){
        if(employee!= null && employee.getEmployeeID() > 0){
            mav.addObject("employee", employee);
            mav.setViewName("employeeList");
        }
        else
            mav.setViewName("redirect:login.html");
        return mav;
    }   

    Employee employeeObject = employeeService.getEmployeeID(EmployeeID);
    employee.copyEmployee(employeeObject);
    mav.addObject("employee", employee);
    mav.setViewName("EmpFile");
    return mav;

Now here is the beginning of the 2nd method (to much irrelevant stuff to copy over), in the method I have to edit an employee the ID is already accessible from the start. So I'm wondering if there's something extra I have to do here, either at the end of one method or beginning of the next.

   @RequestMapping(value = "uploadFile")
public ModelAndView uploadFile(
        @RequestParam(value = "prefix", required = false) String prefix,
        @RequestParam(value = "first", required = false) String first,
        @RequestParam(value = "last", required = false) String last,
        @RequestParam(value = "FileAttach", required = false) MultipartFile FileAttach,
        @ModelAttribute("employee")Employee employee, BindingResult result) {

    if(result.hasErrors())
        return loadFilePage(employee.getEmployeeID(), employee, result);

I configured the jsp to link directly to the second method. That appears to have fixed it.

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