2014-03-03 47 views
2

我不明白爲什麼我的字段值的用戶類正在消失,當我試圖在窗體之間導航。如何在表單之間導航時修復擦除字段?

@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='[email protected]',密碼='12345',年齡= 21,權限= [delete_user]}。

感謝您的任何幫助。

+0

你看着衝刺網絡的流程? http://projects.spring.io/spring-webflow/ – cfrick

回答

1

因爲在permissionForm.jsp中,表單只有權限字段的輸入,因此在提交它時,它將傳遞一個User對象,只有權限字段被填充,剩下的將爲空。 如果你需要有在其餘字段值,添加隱藏的輸入是這樣的:

<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"; 
    } 
} 
+0

是的。它的好方法。感謝名單。但是有沒有其他解決方案沒有隱藏的參數? – user3279337

+0

是的,在第一次提交表單時,您可以將用戶對象存儲在會話中,然後在其他提交中,您可以使用從提交表單中獲得的新值修改會話中的用戶。 –

+0

我將它添加到安塞爾 –