gpt4 book ai didi

java - 如何将数据从一个jsp表单传输到另一个jsp表单

转载 作者:行者123 更新时间:2023-12-02 05:58:29 26 4
gpt4 key购买 nike

我正在使用 spring hibernate 编写一个简单的培训应用程序,其中我将用户数据保存在各个部分中,用户在注册期间和我的帐户中输入数据,我遇到了问题,我不知道如何从第一个表单传输数据到另一个jsp页面的第二个,传输到另一个 Controller 并作为一个人的数据保存到数据库

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

}

最佳答案

在这种情况下,每次从客户端(浏览器)向 Controller 提交信息时,您可以将用户信息存储在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");
}

}

关于java - 如何将数据从一个jsp表单传输到另一个jsp表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55993369/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com