当我尝试在表单之间导航时,我无法弄清楚为什么User类的字段值正在消失。
@Controller
public class WizardController {
@RequestMapping(value = "/", method = RequestMethod.GET)
public String userForm(@ModelAttribute("user") User user) {
return "form/userForm";
}
@RequestMapping(value = "/user", method = RequestMethod.POST)
public String processedForm(@ModelAttribute("user") User user, BindingResult result,
ModelMap model) {
if (result.hasErrors()) {
return "form/userForm";
}
ArrayList<String> p = new ArrayList<String>();
p.add("add_user");
p.add("delete_user");
model.addAttribute("permissions", p);
model.addAttribute("user", user);
return "form/permissionForm";
}
@RequestMapping(value = "/show", method = RequestMethod.POST)
public String show(@ModelAttribute("user") User user, BindingResult result) {
if (result.hasErrors()) {
System.out.println("Error");
return "show";
} else {
System.out.println(user);
return "show";
}
}
}
我的permissionForm.jsp
<form:form action="/show" method="post" modelAttribute="user">
<form:errors path="*" cssClass="errorblock" element="div"/>
<p>
<form:select path="permissions">
<form:options items="${permissions}"/>
</form:select>
</p>
<p><input type="submit" value="Add"/></p>
</form:form>
userForm.jsp
<form:form action="/user" method="post" modelAttribute="user">
<form:errors path="*" cssClass="errorblock" element="div"/>
<p><form:input path="name" placeholder="Name"/></p>
<p><form:input path="age" placeholder="Age"/></p>
<p><form:input path="email" placeholder="Email"/></p>
<p><form:input path="password" placeholder="Password"/></p>
<p><input type="submit"></p>
</form:form>
在show.jsp上我正在渲染我的用户
${user}
和POJO班
public class User implements Serializable {
private String name;
private String email;
private String password;
private Integer age;
private List<String> permissions;
// getters, setters, toString
}
输出:用户{name ='null',email ='null',password ='null',age = null,permissions = [delete_user]}。我需要一个完整的对象,像这样:
用户{name ='John',email ='john @ gmail.com',密码='12345',年龄= 21,权限= [delete_user]}。
感谢您的帮助。
答案 0 :(得分:1)
因为在permissionForm.jsp中,表单只有权限字段的输入,因此在提交时,它将传递一个仅包含权限字段的User对象,其余的将为null。 如果您需要在其余字段中包含值,请添加隐藏的输入,如下所示:
<form:form action="/show" method="post" modelAttribute="user">
<form:errors path="*" cssClass="errorblock" element="div"/>
<p>
<form:select path="permissions">
<form:options items="${permissions}"/>
</form:select>
<form:hidden path="name"/>
<form:hidden path="age"/>
<form:hidden path="email"/>
</p>
<p><input type="submit" value="Add"/></p>
</form:form>
但是,对于密码字段,我不建议在JSP中添加隐藏字段,因为它会为任何能够读取html源的人公开密码。
另一个更好的解决方案是使用会话,通过在表单之间的会话中存储和修改用户,首先将用户存储在第一次提交中:
@RequestMapping(value = "/user", method = RequestMethod.POST)
public String processedForm(@ModelAttribute("user") User user, BindingResult result,
ModelMap model,HttpSession session) {
if (result.hasErrors()) {
return "form/userForm";
}
session.setAttribute("user",user);
ArrayList<String> p = new ArrayList<String>();
p.add("add_user");
p.add("delete_user");
model.addAttribute("permissions", p);
model.addAttribute("user", user);
return "form/permissionForm";
}
@RequestMapping(value = "/show", method = RequestMethod.POST)
public String show(@ModelAttribute("user") User user, BindingResult result,HttpSession session) {
User userInSession = (User)session.getAttribute("user");
userInSession.setPermissions(user.getPermissions());
if (result.hasErrors()) {
System.out.println("Error");
return "show";
} else {
System.out.println(userInSession);
return "show";
}
}