2017-03-26 56 views
0

我要提交包含reflexive列一個表格一個表格時:錯誤枚舉場張貼含有反身場

@RequestMapping(value = "/ajaxElaborationActivite", method = RequestMethod.GET) 
@ResponseBody 
public ModelAndView ajaxElaborationActivite(HttpServletRequest request, HttpSession session) { 

    ModelAndView modelView = new ModelAndView("elaboration/elaborationActivite"); 

    Integer pta_pta_code = Integer.parseInt(request.getParameter("pta_pta_code")); 

    modelView.addObject("action", request.getContextPath().concat("/elaboration/insertActivite")); 

    modelView.addObject("responsables", structureDao.list()); 

    Pta activite = new Pta(); 
    activite.setParent(ptaDao.get(pta_pta_code)); 
    activite.setOwner(Integer.parseInt(String.valueOf(session.getAttribute("user_code")))); 

    modelView.addObject("activite_formulaire", activite); 

    return modelView; 

} 

實體:

@Entity 
@Table(name = "pta") 
public class Pta { 

    @Id() 
    @SequenceGenerator(name="s_pta", sequenceName="s_pta", allocationSize=1) 
    @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="s_pta") 
    @Column(name="pta_code")  
    private Integer code; 

    @Column(name="pta_intitule") 
    @Lob 
    private String lib; 

    @Column(name="pta_desc") 
    @Lob 
    private String descr; 

    @Column(name="owner") 
    private Integer owner; 

    @Column(name="creation") 
    @DateTimeFormat(pattern = "dd/MM/yyyy") 
    private Date creation; 

    @Column(name="modification") 
    @DateTimeFormat(pattern = "dd/MM/yyyy") 
    private Date modification; 

    @ManyToOne 
    @JoinColumn(name = "class_pta_code") 
    private ClassePta classePta; 

    @ManyToOne 
    @JoinColumn(name = "pta_pta_code") 
    private Pta parent; 

    @ManyToOne 
    @JoinColumn(name = "struct_code") 
    private Structure structure; 

    @ManyToOne 
    @JoinColumn(name = "exer_code") 
    private Exer exercice; 

    @ManyToOne 
    @JoinColumn(name = "cdmt_code") 
    private Cdmt cdmt; 

    @ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL) 
    @JoinTable(name = "pta_pnd" , joinColumns = {@JoinColumn(name = "pta_code")} , inverseJoinColumns = {@JoinColumn(name = "pnd_code")}) 
    private Set<Pnd> pnds = new HashSet<Pnd>(); 

    @ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL) 
    @JoinTable(name = "pta_programme" , joinColumns = {@JoinColumn(name = "pta_code")} , inverseJoinColumns = {@JoinColumn(name = "prog_code")}) 
    private Set<Pmo> pmos = new HashSet<Pmo>(); 

    @ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL) 
    @JoinTable(name = "pta_effet_pmo" , joinColumns = {@JoinColumn(name = "pta_code")} , inverseJoinColumns = {@JoinColumn(name = "obj_code")}) 
    private Set<Objectif> effets_pmo = new HashSet<Objectif>(); 

    @ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL) 
    @JoinTable(name = "pta_produit_pmo" , joinColumns = {@JoinColumn(name = "pta_code")} , inverseJoinColumns = {@JoinColumn(name = "obj_code")}) 
    private Set<Objectif> produits_pmo = new HashSet<Objectif>(); 

    @ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL) 
    @JoinTable(name = "pta_objectif_ddp" , joinColumns = {@JoinColumn(name = "pta_code")} , inverseJoinColumns = {@JoinColumn(name = "obj_code")}) 
    private Set<Objectif> ddps = new HashSet<Objectif>(); 

    public Pta() { 
     super(); 
    } 

    public Pta(Integer code) { 
     super(); 
    } 

    // getters and setters 

} 

形式:

<form:form cssClass="form-horizontal" servletRelativeAction="${action}" method="post" commandName="activite_formulaire"> 

    <form:hidden path="code"/> 

    <form:hidden path="parent"/> 

    <form:hidden path="owner"/> 

    <div class="form-group col-sm-12"> 
     <label class="col-sm-3 control-label">Code</label> 
     <div class="col-sm-9"> 
     <form:input path="lib" cssClass="validate[required] col-sm-4" /> 
     </div> 
    </div> 

    <div class="form-group col-sm-12"> 
     <label class="col-sm-3 control-label">Libell&eacute;</label> 
     <div class="col-sm-9"> 
     <form:textarea path="descr" cssClass="validate[required] col-sm-9" style="padding-left: 1px;" /> 
     </div> 
    </div> 

    <div class="form-group col-sm-12"> 
     <label class="col-sm-3 control-label">Responsable</label> 
     <div class="col-sm-9"> 
     <form:select path="structure" cssClass="col-sm-4"> 
      <form:option value="" label=" -- S&eacute;lectionner -- "/> 
      <form:options items="${responsables}" itemValue="code" itemLabel="lib" /> 
     </form:select> 
     </div> 
    </div> 

</form:form> 

<script type="text/javascript"> 

$(document).ready(function() { 

    $("#activite_formulaire").validationEngine(); 

}); 

</script> 

以下是處理帖子的方法:

@RequestMapping(value = "/insertActivite", method = RequestMethod.POST) 
    public ModelAndView insertActivite(@ModelAttribute("activite_formulaire") Pta pta, @RequestParam String structure, @RequestParam String parent, 
             HttpServletRequest request, HttpSession session) { 

     if (session.getAttribute("user_code") != null) { // test session expirée , ou utilisateur non connecté 

      Integer pta_code = ptaDao.insert(pta, exerDao.getExerciceEncours().getExerCode(), structure, "", 2, Integer.parseInt(parent)); 

      // trace 

      Menu menuEncours = menuDao.getMenuTrace(request.getServletPath()); 

      menuEncours.setEnv(env); 

      String trace_txt = env.getProperty("pta.trace.ajout.activite"); 
      trace_txt = trace_txt.replace("%s", pta.getLib()); 

      menuDao.insertTrace(String.valueOf(session.getAttribute("user_code")), menuEncours.getId(), trace_txt); 

      // fin trace 

      ModelAndView modelView = new ModelAndView("redirect:/elaboration/"); 

      return modelView; 

     } else { 

      ModelAndView modelView = new ModelAndView("redirect:/"); 

      modelView.addObject("es", "1"); 

      return modelView; 

     } 

    } 

在運行時,我得到:The request sent by the client was syntactically incorrect

那麼,什麼是錯的?

回答

0

我發現:該解決方案是使控制器父的設置只是將數據傳遞給JSP:

@RequestMapping(value = "/ajaxElaborationActivite", method = RequestMethod.GET) 
@ResponseBody 
public ModelAndView ajaxElaborationActivite(HttpServletRequest request, HttpSession session) { 

    ModelAndView modelView = new ModelAndView("elaboration/elaborationActivite"); 

    Integer pta_pta_code = Integer.parseInt(request.getParameter("pta_pta_code")); 

    modelView.addObject("action", request.getContextPath().concat("/elaboration/insertActivite")); 

    modelView.addObject("responsables", structureDao.list()); 

    Pta activite = new Pta(); 
    activite.setOwner(Integer.parseInt(String.valueOf(session.getAttribute("user_code")))); 

    modelView.addObject("parent", pta_pta_code); // here it is 

    modelView.addObject("activite_formulaire", activite); 

    return modelView; 

} 

所以在JSP:

<form:form cssClass="form-horizontal" servletRelativeAction="${action}" method="post" commandName="activite_formulaire"> 

    <form:hidden path="code"/> 

    <input type="hidden" name="ascendant" value="${parent}"/> // here it is , it is mandatory to set the input name to be different to the entity's attribute name 

    <form:hidden path="owner"/> 

    <fieldset id="resume"><legend>R&eacute;sum&eacute; du PTA</legend> 
     <div class="form-group col-sm-12"> 
      <label class="col-sm-3 control-label">Intitul&eacute;</label> 
      <div class="col-sm-9"> 
      <span class="col-sm-12">${fieldset.lib}</span> 
      </div> 
     </div> 
    </fieldset> 

    <div class="form-group col-sm-12"> 
     <label class="col-sm-3 control-label">Code</label> 
     <div class="col-sm-9"> 
     <form:input path="lib" cssClass="validate[required] col-sm-4" style="padding-left: 1px;" /> 
     </div> 
    </div> 

    <div class="form-group col-sm-12"> 
     <label class="col-sm-3 control-label">Libell&eacute;</label> 
     <div class="col-sm-9"> 
     <form:textarea path="descr" cssClass="validate[required] col-sm-9" style="padding-left: 1px;" /> 
     </div> 
    </div> 

    <div class="form-group col-sm-12"> 
     <label class="col-sm-3 control-label">Responsable</label> 
     <div class="col-sm-9"> 
     <form:select path="structure" cssClass="col-sm-4"> 
      <form:option value=""/> 
      <form:options items="${responsables}" itemValue="code" itemLabel="lib" /> 
     </form:select> 
     </div> 
    </div> 

</form:form> 

而在控制器的方法處理表單提交:

@RequestMapping(value = "/insertActivite", method = RequestMethod.POST) 
    public ModelAndView insertActivite(@ModelAttribute("activite_formulaire") Pta activite, @RequestParam int ascendant, @RequestParam String structure, 
             HttpServletRequest request, HttpSession session) { 

      Integer pta_code = ptaDao.insert(activite, exerDao.getExerciceEncours().getExerCode(), structure, "", 2, ascendant); 

      // trace 

      Menu menuEncours = menuDao.getMenuTrace(request.getServletPath()); 

      menuEncours.setEnv(env); 

      String trace_txt = menuEncours.getTrace(); 

      trace_txt = trace_txt.replace("%s", activite.getLib()); 

      menuDao.insertTrace(String.valueOf(session.getAttribute("user_code")), menuEncours.getId(), trace_txt); 

      // fin trace 

      ModelAndView modelView = new ModelAndView("redirect:/elaboration/"); 

      return modelView; 

    } 
+0

th事實上,將JSP字段命名爲與bean屬性不同的名稱也適用於一般外鍵關係,而不僅僅是反身關係。 – pheromix