Spring MVC form: select Do not return an object in a POST operation

I have a question regarding retrieving an object from the list that was used to fill out the selection form.

Controller:

@RequestMapping(value="listStaffMembers", method=RequestMethod.GET) public String listStaffMembers(Model model){ model.addAttribute("staffList", this.personManager.getAllStaff()); Person person = new Person(); model.addAttribute("staffMember", person); model.addAttribute("staffListSize", this.personManager.getAllStaff().size()); for (Person persons : this.personManager.getAllStaff()) System.out.println("Query Name:" + persons.getName() + " Id:"+persons.getId()); return "staffList"; } 

A controller that does not receive a Person object:

 @RequestMapping(value="staffMemberCRUD", method=RequestMethod.POST) //ModelAndView public String staffMemberCRUD(@ModelAttribute("staffMember")Person person, Model model){ if(person!=null){ model.addAttribute("staffMember", person); String name = person.getName(); int id = person.getId(); System.out.println("id: "+ id); System.out.println("name: "+ name); return "staffForm"; }else{ System.out.println("Person not saved, no entry, throwing null pointer exception"); throw new NullPointerException(); } } 

Personality Class:

 @Entity(name="STAFF") @NamedQuery(name="staffDetails.byId", query="from STAFF")// where userId = ?, then use position 0, then value public class Person { @Id @GeneratedValue(strategy=GenerationType.AUTO) private int id; @Column(name="NAME") private String name; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Person(int personId, String name){ setId(personId); setName(name); } public Person(String name){ setName(name); } public Person(int Id){ setId(Id); } public Person(){ } } 

Something like the form below returns an object, and the identifier appears in the name variable for the person, although without the id value.

 <FORM ACTION="staffMemberCRUD" METHOD=POST ><!-- onSubmit="return dropdown(this.gourl) --> <table> <tr> <td> <select name="staffMember"> <option value="">Choose</option> <c:forEach items="${staffList}" var="prod"> <option value ="${prod.id}"><c:out value="${prod.id}"></c:out><c:out value="${prod.name}"></c:out></option> </c:forEach> </select> </td> </tr> </table> <INPUT TYPE=SUBMIT VALUE="Go"> </FORM> 

The Person class is also used to populate db with sleep mode, can this confuse the problem?

When testing the drop-down list listStaffMembers, only its message is populated, which does not currently work. I looked at the Spring documentation for this and it made me so far, any help is much appreciated as it took 3 days of my life so far without any positive results. Thanks a lot in advance, Dean

 Latest Update: 

Updated form: select, note that now the selection path sends the "id" message and the correct form tag: object-object in the following view after POST now has an id field filled in, which is less hacked than the ones mentioned before, so that again find them from the database:

 <form:form action="staffMemberCRUD" commandName="staffMember"><!-- onSubmit="return dropdown(this.gourl) --> <table> <tr> <td> <form:select path="id"> <form:option value="-1">Select your Staff Member</form:option> <form:options items="${staffList}" itemLabel="name" itemValue="id" /> </form:select> </td> </tr> </table> <INPUT TYPE=SUBMIT VALUE="Go"> </form:form> 

I know that PropertyEditor still remains in the future, when that happens, I will update again.

+4
source share

Source: https://habr.com/ru/post/1439929/


All Articles