简体   繁体   中英

how to transfer data from one jsp form to another

I am writing a simple training application using spring hibernate where I save user data in parts, the user enters data during registration and in my account, I have a problem, I don't know how to transfer data from the first form to the second on another jsp page, to transfer to another controller and save to database as data of one person

registration.jsp

    <spring:form name="myForm" action="save-user" method="post"
            modelAttribute="user" onsubmit="return validateForm()">
            <spring:hidden path="id" />


        <div class="center">
            емайл: <br>
            <spring:input path="userEmail" />
            <br> <br>
        </div>

        <div class="center">
            логин: <br>
            <spring:input path="userLogin" />
            <br> <br>
        </div>

        <div class="center">
            пароль: <br>
            <spring:input path="password" />
            <br> <br>
        </div>

        <div class="center">
            <input type="submit" value="зарегистрироваться">
        </div>
    </spring:form>

    <form action="registration" method="post"></form>
RegistrationController{

    @RequestMapping(value = "/save-user", method = RequestMethod.POST)
    public ModelAndView saveUser(UserBean user) {
        userService.saveUser(user);
        Integer id = user.getId();
        String email = user.getUserEmail();
        String login = user.getUserLogin();
        String password = user.getPassword();
                Map<String, String> result = new HashMap<String, String>();
        result.put("email", email);
        result.put("id", String.valueOf(id));
        result.put("login", login);
        result.put("password", password);
        return new ModelAndView("privateroom", result);
    }
}
privateroom.jsp

 <spring:form name="myForm_1" action="save-user-two" method="post"
            modelAttribute="user" onsubmit="return validateForm()">
             <spring:hidden path="${id}" />


        <div class="center">
            имя: <br>
            <spring:input path="userName" />
            <br> <br>
        </div>

        <div class="center">
            фамилия: <br>
            <spring:input path="userSurname" />
            <br> <br>
        </div>

        <div class="center">
            дата рождения: <br>
            <spring:input path="userDateOfBirth" />
            <br> <br>
        </div>
        <spring:hidden path="${email}"/>
        <spring:hidden path="${login}"/>
            <spring:hidden path="${password}"/>


        <div class="center">
            <input type="submit" value="дополнить данные">
        </div>
    </spring:form>

PrivateRoomController{

@RequestMapping(value = "/save-user-two", method = RequestMethod.POST)
    public ModelAndView saveUser(UserBean user) {
        userService.saveUser(user);

        return new ModelAndView("privateroom");
    }

}

In this case you can store the user information in Session every time the information submitted from the client (browser) to controller.

Note: I recommend not to send back the information to client browser to be stored in hidden fields, especially should not send back the password to client browser. You can send the remaining (email, id, login) if you really need to show them on the next page.

RegistrationController {

  @RequestMapping(value = "/save-user", method = RequestMethod.POST)
  public ModelAndView saveUser(UserBean user, HttpSession session) {

    // Store user object in the user's session
    session.setAttribute("user", user);

    // Return back some information for showing purpose only
    Map<String, String> result = new HashMap<String, String>();
    result.put("email", user.getEmail());
    result.put("id", String.valueOf(user.getId()));
    result.put("login", user.getLogin());
    return new ModelAndView("privateroom", result);
  }

}

During the last submit, you can retrieve back the user information from Session and save it using userService.saveUser.

PrivateRoomController {

    @RequestMapping(value = "/save-user-two", method = RequestMethod.POST)
    public ModelAndView saveUser(UserBean user, HttpSession session) {

        // Retrieve user object in the user's session
        UserBean userFromSession = session.getAttribute("user");

        // Update some additional values
        userFromSession.setUserName(user.getUserName());
        userFromSession.setUserSurename(user.getUserSurename());
        userFromSession.setUserDateOfBirth(user.getUserDateOfBirth());

        // Save to database at once
        userService.saveUser(userFromSession);

        return new ModelAndView("privateroom");
    }

}

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