简体   繁体   中英

How to pass from one jsp to another jsp storing data in session with Spring

I want to know how to use sessionAttributes and ModelAttributes for my specific scenario described below:

I have two jsp pages with form fields. When I enter form field values in the first jsp page and click on next, these filled form fields values should be stored in the session attribute but not in the database. After moving to the second jsp page, again fill form fields and click on submit. This time first jsp (from the session attribute in the controller) and second jsp page form field values should be saved in the DB.

Please guide me how to achieve this. If have any sample code, that would be great help.

Simply store your data on HttpSession. You can use functions like this for managing session info from the controller:

  private static HttpSession getSession() {
    ServletRequestAttributes attr = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
   return attr.getRequest().getSession();
  }

  public static void clear(String name) {
    getSession().removeAttribute(name);
  }

  public static void clear() {
    getSession().invalidate();
  }

  public static Object load(String name) {
    return getSession().getAttribute(name);
  }

  public static void store(String name, Object value) {
    getSession().setAttribute(name, value);
  }

Yu can access session from jsp with:

<%= session.getAttribute("attributeName") %>

or with

${requestScope.attributeName}

You don't need to use Spring to achieve this. HttpSession is a J2EE standard. In the above code Spring is used to get to the session in a static way with this

ServletRequestAttributes attr = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();

To get the session you only need access to the HttpRequest.

In your controller, set value into Session Attributes.Below is the sample code.

@RequestMapping("/testing")
@Controller
public class TestController {
@RequestMapping(method = RequestMethod.GET)
public String testMethod(HttpServletRequest request){
    request.getSession().setAttribute("name", "value");
    return "testJsp";
  }
}

To access the same in JSP use

${sessionScope.name} .

This is covered in the Spring MVC docs at:

http://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html#mvc-ann-sessionattrib

@Controller
@RequestMapping("/step1")
@SessionAttributes("myModelObject")
public class ControllerOne{

    @GetMapping
    publid String loadForm(ModelMap modelMap){
        //presence of class level @SessionAttributes puts this is session
        modelMap.addAttribute("myModelObject", new MyModelObject());
        return "form1";
    }

    @PostMapping
    //model attribute retrieved from session
    public String handleSubmit(@ModelAttribute("myModelObject") MyModelObject myModelObject){
        return "/step2";
    }
}

@Controller
@RequestMapping("/step2")
public class ControllerTwo{

    @GetMapping
    publid String loadForm(){
        return "form2";
    }

    @PostMapping
    //model attribute retrieved from session
    public String handleSubmit(@ModelAttribute("myModelObject") MyModelObject myModelObject, SessionStatus sessionStatus){
        //process

        //clear down session 
        //http://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/bind/support/SessionStatus.html
        sessionStatus.setComplete();

        return "nextView"
    }
}

Aditioanally, Spring Web Flow has more advanced support for multi-step forms:

http://projects.spring.io/spring-webflow/

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