无法处理提交的外键

时间:2017-03-05 16:21:51

标签: spring spring-mvc

实体Pta有3个一对多的关系:

master

我想在PTA中插入一条记录。

页面jsp的控制器:

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

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

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

    @ManyToOne
    @JoinColumn(name = "obj_code")
    private Objectif objectif;

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

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

    public Pta() {
        super();
    }

    public Pta(Integer code) {
        super();
        this.code = code;
    }

    // getters and setters

}

@Entity
@Table(name = "objectif")
public class Objectif {

    @Id
    @SequenceGenerator(name="s_objectif", sequenceName="pta.s_objectif", allocationSize=1)
    @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="s_objectif")
    @Column(name = "obj_code")
    private int code;

    @Column(name="obj_intitule")
    private String lib;

    @ManyToOne
    @JoinColumn(name = "prog_code")
    private Programme programme;

    @ManyToOne
    @JoinColumn(name = "nat_obj_code")
    private NatureObjectif nature_objectif;

    public Objectif() {
        super();
    }

    public Objectif(int code) {
        super();
        this.code = code;
    }

    public Objectif(int code, String lib) {
        super();
        this.code = code;
        this.lib = lib;
    }

    // getters and setters

}

@Entity
@Table(name = "structure")
public class Structure {

    @Id()
    @Column(name="struct_code") 
    private String code;

    @Column(name="struct_sigle")
    private String sigle;

    @Column(name="struct_lib")
    private String lib;

    public Structure() {
        super();
    }

    public Structure(String code) {
        super();
        this.code = code;
    }

    // getters and setters

}

@Entity
@Table(name = "exercice")
public class Exer {

    @Id()
    @Column(name="exer_code")   
    private String exerCode;

    @Column(name="exer_lib")
    private String exerLibelle;

    public Exer(){
        super();
    }

    public Exer(String exer_code) {
        super();
        this.exerCode = exer_code;
    }

    // getters and setters

}

jsp:

modelView.addObject("exercices",exerDao.list());
modelView.addObject("structures",structureDao.list());
HashMap<String, String> criteres = new HashMap<String, String>();
criteres.put("nat_obj_code", "DdP");
modelView.addObject("docs_perf", objectifDao.lireParCritere(criteres));
criteres.clear();
criteres.put("nat_obj_code", "ODD");
modelView.addObject("odds", objectifDao.lireParCritere(criteres));
criteres.clear();
criteres.put("nat_obj_code", "PRO");
modelView.addObject("produits", objectifDao.lireParCritere(criteres));
modelView.addObject("action", request.getContextPath().concat("/elaboration/savePtaEtDetails"));
modelView.addObject("pta_formulaire", new Pta());
return modelView;

在控制器目标中:

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

    <form:hidden path="code"/>

        <div class="row">

            <div class="col-sm-8">

                <div class="form-group">
                    <label class="col-sm-4 control-label">Exercice</label>
                    <div class="col-sm-4">
                        <form:select path="exercice" cssClass="validate[required]">
                             <form:option value="" label=" -- Sélectionner -- "/>
                             <form:options items="${exercices}" itemValue="exerCode" itemLabel="exerLibelle" />
                        </form:select>
                    </div>
                 </div>
                 <div class="form-group">
                    <label class="col-sm-4 control-label">Document de performance</label>
                    <div class="col-sm-4">
                        <form:select path="objectif" style="width:500px;" cssClass="validate[required]">
                        <form:option value="" label=" -- Sélectionner -- "/>
                        <form:options items="${docs_perf}" itemValue="code" itemLabel="lib" />
                        </form:select>
                    </div>
                 </div>
                 <div class="form-group">
                    <label class="col-sm-4 control-label">ODD</label>
                          <div class="col-sm-4">
                                <select id="odd" name="odd" style="width:500px;">
                                   <option value=""> -- S&eacute;lectionner -- </option>
                                   <c:forEach items="${odds}" var="odd">
                                        <option value="${odd.code}">${odd.lib}</option>
                                   </c:forEach>
                                </select>
                          </div>
                        </div>
                        <div class="form-group">
                          <label class="col-sm-4 control-label">Axes du PND</label>
                          <div class="col-sm-4">
                                <select id="pnd" name="pnd" style="width:500px;" multiple></select>
                          </div>
                        </div>
                        <div class="form-group">
                          <label class="col-sm-4 control-label">Programmes</label>
                          <div class="col-sm-4">
                                <select id="programme" name="programme" style="width:500px;" multiple></select>
                          </div>
                        </div>
                        <div class="form-group">
                          <label class="col-sm-4 control-label">Effets</label>
                          <div class="col-sm-4">
                                <select id="effet" name="effet" style="width:500px;" multiple></select>
                          </div>
                        </div>
                        <div class="form-group">
                          <label class="col-sm-4 control-label">Produits PMO</label>
                          <div class="col-sm-4">
                                <select id="produit" name="produit" style="width:500px;" multiple>
                                   <option value=""> -- S&eacute;lectionner -- </option>
                                   <c:forEach items="${produits}" var="produit">
                                        <option value="${produit.code}">${produit.lib}</option>
                                   </c:forEach>
                                </select>
                          </div>
                        </div>
                        <div class="form-group">
                          <label class="col-sm-4 control-label">Actions PMO</label>
                          <div class="col-sm-4">
                                <select id="action" name="action" style="width:500px;" multiple></select>
                          </div>
                        </div>
                        <div class="form-group">
                          <label class="col-sm-4 control-label">Activit&eacute;</label>
                          <div class="col-sm-4">
                                <select id="activite" name="activite" style="width:500px;">
                                   <option value=""> -- S&eacute;lectionner -- </option>
                                </select>
                          </div>
                        </div>
                        <div class="form-group">
                          <label class="col-sm-4 control-label">R&eacute;sultat attendu</label>
                          <div class="col-sm-4">
                                <select id="resultat" name="resultat" style="width:500px;">
                                   <option value=""> -- S&eacute;lectionner -- </option>
                                </select>
                          </div>
                        </div>
                        <div class="form-group">
                          <label class="col-sm-4 control-label">Indicateur</label>
                          <div class="col-sm-8">
                                <select id="indicateur" name="indicateur" style="width:500px;">
                                   <option value=""> -- S&eacute;lectionner -- </option>
                                </select>
                          </div>
                        </div>
                        <div class="form-group">
                            <div class="col-sm-12">
                              <fieldset>
                                <legend><label class="control-label">Programmation physique</label></legend>
                                <div class="row">
                                    <div class="col-sm-3">
                                        <div class="form-group">
                                            <label class="col-sm-1 control-label">T1</label>
                                              <div class="col-xs-10">
                                                    <input class="form-control" type="text" name="t1">
                                              </div>
                                        </div>
                                    </div>
                                    <div class="col-sm-3">
                                        <div class="form-group">
                                            <label class="col-sm-1 control-label">T2</label>
                                              <div class="col-xs-10">
                                                    <input class="form-control" type="text" name="t2">
                                              </div>
                                        </div>
                                    </div>
                                    <div class="col-sm-3">
                                        <div class="form-group">
                                            <label class="col-sm-1 control-label">T3</label>
                                              <div class="col-xs-10">
                                                    <input class="form-control" type="text" name="t3">
                                              </div>
                                        </div>
                                    </div>
                                    <div class="col-sm-3">
                                        <div class="form-group">
                                            <label class="col-sm-1 control-label">T4</label>
                                              <div class="col-xs-10">
                                                    <input class="form-control" type="text" name="t4">
                                              </div>
                                        </div>
                                    </div>

                                    </div>
                              </fieldset>
                        </div>
                    </div>
                    <div class="form-group">
                        <div class="col-sm-12">
                          <fieldset>
                            <legend><label class="control-label">Programmation financi&egrave;re</label></legend>
                            <div class="row">
                                <div class="col-sm-12">
                                    <div class="form-group">
                                        <label class="col-sm-1 control-label">Montant</label>
                                          <div class="col-xs-3">
                                                <input class="form-control" type="text" name="progr_financiere">
                                          </div>
                                    </div>
                                </div>
                            </div>
                          </fieldset>
                        </div>
                    </div>
                </div>

                <div class="col-sm-4">
                    <div class="form-group">
                      <label class="col-sm-4 control-label">Structure</label>
                      <div class="col-sm-4">
                            <form:select path="structure" style="width:230px;" cssClass="validate[required]">
                               <form:option value="" label=" -- Sélectionner -- "/>
                               <form:options items="${structures}" itemValue="code" itemLabel="lib" />
                            </form:select>
                      </div>
                    </div>
                </div>

                <input class="btn btn-primary btn-sm" type="submit"/>

            </form:form>

在运行时提交表单时,我收到错误@RequestMapping(value = "/savePtaEtDetails", method = RequestMethod.POST) public ModelAndView savePtaEtDetails(@ModelAttribute("pta_formulaire") Pta pta, @RequestParam String exercice, @RequestParam int objectif, @RequestParam String structure) { System.out.println("eeeeeeeeeeeeeeee"); return new ModelAndView("redirect:/accueil"); } 。那有什么不对?

1 个答案:

答案 0 :(得分:0)

我找到了:)解决方案是在“主”实体的构造函数中设置属性:

public Exer(String exer_code) {
        super();
    }

public Structure(String code) {
        super();
    }

public Objectif(String code) {
        super();
    }