Spring MVC形式:选择在POST操作中不返回对象

Dea*_*ean 5 forms post spring jsp annotations

我有一个关于从用于填充选择表单的列表中检索对象的问题。

控制器:

@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";
}
Run Code Online (Sandbox Code Playgroud)

未收到Person对象的控制器:

@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();
    }
}
Run Code Online (Sandbox Code Playgroud)

人员类别:

@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(){

    }
}
Run Code Online (Sandbox Code Playgroud)

下面的表单如何检索对象,其ID出现在person的name变量中,但没有id值。

<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>
Run Code Online (Sandbox Code Playgroud)

Person类还用于使用hibernate填充数据库,这会使问题混淆吗?

测试listStaffMembers下拉列表时,将填充该列表,而该帖子当前不起作用。我已经看过Spring文档,这已经使我走了这么远,任何帮助都非常感激,因为到目前为止我花了3天的时间没有取得积极的结果。谢谢你,院长

Latest Update:
Run Code Online (Sandbox Code Playgroud)

更新了form:select,注意select路径现在发布了'id',并且正确的form:form标记(POST之后的下一个视图中的人员对象)现在填充了id字段,这比前面提到的要少再次从数据库中找到它们:

<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>
Run Code Online (Sandbox Code Playgroud)

我知道PropertyEditor仍然是前进的道路,当这种情况发生时,我将再次更新。