Spring MVC - passing variables from one page to anther

后端 未结 3 1511
野的像风
野的像风 2020-12-19 17:01

I need help. I am working on a project where I have multiple pages and multiple forms; each page has one form. I just need to be able to pass values from one jsp to another.

3条回答
  •  不思量自难忘°
    2020-12-19 17:27

    You could also use session attributes, either manually, such as:

    public ModelAndView handleRequest(HttpServletRequest request){
          request.getSession().getAttribute("nameOfAttribute");
    }
    

    Apologies for writing this as an annotated controller, I do not recall if xml config controllers offer this feature...

    There is no Spring code involved for that. The other option is to use the @SessionAttribute annotation on your controller:

    @Controller 
    @SessionAttributes("nameOfAttribute")
    public class MyController{
    //your session attribute can be accessed in controller methods using @ModelAttribute
    public ModelAndView handleRequest(@ModelAttribute("nameOfAttribute")){
          session.getAttribute("nameOfAttribute");
    }
    

    Note

    You will need to clear the session attribute when you are done with it:

    request.getSession().setAttribute("nameOfAttribute", null);
    

提交回复
热议问题