如何将数据从一种jsp格式传输到另一种

时间:2019-05-05 15:05:32

标签: java spring-mvc jsp

我正在使用Spring Hibernate编写一个简单的培训应用程序,其中我将用户数据部分保存,用户在注册期间输入数据,并且在我的帐户中,我遇到了问题,我不知道如何从第一种形式传输数据到另一个jsp页面上的第二个,以转移到另一个控制器并将其作为一个人的数据保存到数据库中

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");
    }

}

1 个答案:

答案 0 :(得分:0)

在这种情况下,每次从客户端(浏览器)提交给控制器的信息时,您都可以在Session中存储用户信息。

注意:我建议不要将信息发送回客户端浏览器以存储在隐藏字段中,尤其不要将密码发送回客户端浏览器。如果确实需要在下一页显示它们,则可以发送其余的(电子邮件,ID,登录名)。

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);
  }

}

在上一次提交期间,您可以从Session中检索用户信息并使用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");
    }

}