Spring @ModelAttributes auto binding

192 Views Asked by At

Hi i am tring to use @ModelAttributes for auto binding from view to controller.

this jsp page should send all elements which is Personal Schedule VO.

<form name="insertFrm" action="myScheduleInsert" method="POST">
                    <ul>
                       <input type="hidden" class="form-control" name="perschd_num" >
                       <li>
                          <label class='control-label'>title</label>
                       <input type="text" class="form-control" name="perschd_title" >
                       </li>
                       <li>
                       <input type="hidden" class="form-control" name="perschd_writer" >
                       <li>
                          <label class='control-label'>start date</label>
                       <input type="date" id="fullYear" class="form-control" name="perschd_start_date"  value="">
                       </li>
                       <li>
                          <label class='control-label'>end date</label>
                       <input type="date"  class="form-control" name="perschd_end_date" >
                       </li>
                       <li>
                          <label class='control-label'>content</label>
                       <input type="text" class="form-control" name="perschd_cont" >
                       </li>
                 </ul>

                 </div>

               <!-- button-->  
               <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                    <input type="submit" class="btn btn-primary">
             </form>

and this is my controller for insert feature by using @ModelAttributes PerschdVO perschd and when i try print all out, all is null.

//insertSchedule
   @RequestMapping("/myScheduleInsert")
   public String myScheduleInsert(@ModelAttribute PerschdVO perschdVO ){

       String view="redirect:/professor/mypage/mySchedule";

       *System.out.println(perschdVO);*

       try {
        proService.insertPerschd(perschdVO);
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

       return view;
   }

and this is PerschdVO

public class PerschdVO {
private int perschd_num;  
private String perschd_title;  
private String perschd_cont; 
private Date perschd_date; 
private String perschd_start_date;  
private String perschd_end_date;  
private String perschd_writer;  //id


public String getPerschd_writer() {
    return perschd_writer;
}
public void setPerschd_writer(String perschd_writer) {
    this.perschd_writer = perschd_writer;
}
public int getPerschd_num() {
    return perschd_num;
}
public void setPerschd_num(int perschd_num) {
    this.perschd_num = perschd_num;
}
public String getPerschd_title() {
    return perschd_title;
}
public void setPerschd_title(String perschd_title) {
    this.perschd_title = perschd_title;
}
public String getPerschd_cont() {
    return perschd_cont;
}
public void setPerschd_cont(String perschd_cont) {
    this.perschd_cont = perschd_cont;
}
public Date getPerschd_date() {
    return perschd_date;
}
public void setPerschd_date(Date perschd_date) {
    this.perschd_date = perschd_date;
}
public String getPerschd_start_date() {
    return perschd_start_date;
}
public void setPerschd_start_date(String perschd_start_date) {
    this.perschd_start_date = perschd_start_date;
}
public String getPerschd_end_date() {
    return perschd_end_date;
}
public void setPerschd_end_date(String perschd_end_date) {
    this.perschd_end_date = perschd_end_date;
}

why this elements from jsp does not match with this? Eventhough i matched all name in jsp file to PerschdVO's get() names.

and if i fix like this

public String myScheduleInsert(
            Principal who,
            @RequestParam("perschd_start_date")String perschd_start_date,
            @RequestParam("perschd_end_date")String perschd_end_date,
            @RequestParam("perschd_title")String perschd_title,
            @RequestParam("perschd_cont")String perschd_cont
            ){
        PerschdVO perschdVO = new PerschdVO();
    ...}

than the data from jsp could be sent to @Controller, but still can't get informations about using @ModelAttributes. cos if i use that always 400 bad request happened.

0

There are 0 best solutions below