从jsp表单返回的空值:选项到控制器

时间:2016-09-08 15:02:25

标签: spring forms jsp spring-mvc

路径选区的JSP页面返回的值为NULL。我选择了一个下拉列表来显示可以选择的所有可能的选区。我已经详细说明了下面的问题。 这是Person Class:

 @Entity
 @Table(name="person")
 public class person{


 @Id
 @Column(name = "NIC")
 private Integer NIC;
 @Column(name= "firstname")
 private String fname;
 @Column(name= "lastname")
 private String lname;
 @ManyToOne
 @JoinColumn(name= "constituency_id")
 private constituency constituency;
 @OneToOne
 @JoinColumn(name="NIC")
 private login log;

public person(){
    this.NIC = 1234;
    this.fname = "Undefined";
    this.lname = "Undefined";
}
public person(String fname, String lname, Integer NIC) {
   this.fname = fname;
   this.lname = lname;
   this.NIC = NIC;
}

public Integer getNIC() {
    return this.NIC;
}

public void setNIC(Integer NIC) {
    this.NIC = NIC;
}

public String getfname() {
    System.out.println(this.fname);
    return this.fname;
}

public void setfname(String name) {
    this.fname = name;
}

public String getlname() {
    return this.lname;
}

public void setlname(String lname) {
    this.lname = lname;
}

public constituency getConstituency() {
    return this.constituency;
}

public void setConstituency(constituency id) {
    this.constituency = id;
}

public login getlogin() {
    return this.log;
}

public void setlogin(login log) {
    this.log = log;
}

@Override
public String toString() {
    return this.lname + " : " + this.fname;
}

}

以下是选区组:

@Entity
@Table(name="constituency")

public class constituency {

@Column(name="address")
private String address;
@Column(name="name")
private String name;
@Id
@Column(name="constituency_id")
private Integer constituency_id;
@Column(name="noofvoters")
private Integer voters;
@OneToMany(mappedBy="constituency", cascade = {CascadeType.ALL})
private Set <person> persons;

public constituency(){
   this.constituency_id = 0;
   this.name = "Undefined";
   this.address = "Undefined";
   this.voters = 0;
}
public constituency(String name, String address, Integer voters, Integer id)     {
  this.name = name;
  this.address = address;
  this.voters = voters;
  this.constituency_id = id;
}

public String getname() {
    System.out.println(this.name);
   return this.name;
}

public void setname(String name) {
   this.name = name;
}

public String getaddress() {
    System.out.println(this.address);
   return this.address;
}

public void setaddress(String name) {
   this.address = name;
}

 public Integer getvoters() {
   return this.voters;
}

public void setvoters(Integer voters) {
   this.voters = voters;
}

public Integer getconstituency_id() {
   return this.constituency_id;
}

public void setconstituency_id(Integer id) {
   this.constituency_id = id;
}

@Override
 public String toString() {
   return this.name;
 }
}

以下是负责处理操作的控制器部分:

 @RequestMapping(value="/Add", method=RequestMethod.GET)
 public String add(Model model){
    List <constituency> constit = constituencyDAO.details();
    model.addAttribute("message", "Add a person for voting");
    model.addAttribute("per", new person());
    model.addAttribute("constituency", constit);
    return "Add";
}

@RequestMapping(value="/Add", method=RequestMethod.POST)
public ModelAndView addperson(@ModelAttribute("per") person per,   BindingResult bind){
    System.out.println("In controller");
    System.out.println(per.getfname()+" First-name");
    System.out.println(per.getConstituency()+" constituency");
    return hello();
}

最后,这是JSP页面的一部分,其格式为:select tag:

    <div class = "form-group">
        <label for = "element1" class="control-label col-xs-2">Constituency ID</label>
        <div class="col-xs-10">
        <form:select path="constituency" name="constituency">
            <form:option value="NONE" label="--- Select ---" />
            <form:options items="${constituency}" itemValue="constituency_id" itemLabel="name" />
        </form:select>
        <p>Constituency ID</p>
        </div>
    </div>

现在,问题在于即使JSP页面能够以下列形式成功显示constituency属性:options,当在控制器中检索“per”模型属性时,选区的值和仅选区的值为NULL

以下是Eclipse中的输出。

表格: The Form

RequestMethod.POST中的输出:

enter image description here

0 个答案:

没有答案