尝试更新对象时出现问题

时间:2016-06-03 09:53:52

标签: spring-mvc

我正面临更新对象的prblm,因为当我试图在 updateApp 函数上显示我的对象id时,我得到0表示它不是 editApp 功能

上的相同对象

控制器

@RequestMapping(value = { "/edtApp-{id}" }, method = RequestMethod.GET)
    public String editApp(@PathVariable int id, ModelMap model) {
        PIL_P_APPLCTN application = appService.findById(id);
        logger.info("=========>"+application.getId()+" "+application.getAPPLCTN_CD());
        List<PRM_CONTEXTE> cntxt = cntxtService.findAllOBJECTS();
        model.addAttribute("cntxt", cntxt);
        model.addAttribute("application", application);
        model.addAttribute("edit", true);
        model.addAttribute("loggedinuser", getFullName());
        return "formApp";
    }

    @RequestMapping(value = { "/edtApp-{id}" }, method = RequestMethod.POST)
    public String updateApp(@Valid @ModelAttribute("application") PIL_P_APPLCTN application, BindingResult result, ModelMap model,
            @PathVariable int id) {


        logger.info("====**=====>"+application.getId()+" "+application.getAPPLCTN_CD());
        logger.info("====**=====>"+application.getId()+" "+application.getAPPLCTN_DS());
        logger.info("====**=====>"+application.getId()+" "+application.getAPPLCTN_ID());
        logger.info("====**=====>"+application.getId()+" "+application.getDECLG_IDNTFNT_NU());
        logger.info("====**=====>"+application.getId()+" "+application.getVersion());
        logger.info("====**=====>"+application.getId()+" "+application.getActive());
        List<PRM_CONTEXTE> cntxt = cntxtService.findAllOBJECTS();
        if (result.hasErrors()) {
            model.addAttribute("cntxt", cntxt);
            return "formApp";
        }
appService.updateOBJECT(application);
    return "redirect:/appli";
    }
  

EDIT1

<form:form method="POST" modelAttribute="application" class="form-horizontal form-label-left" >
                    <form:input type="hidden" path="id" id="id"/>


                      <div class="form-group">
                        <label class="control-label col-md-3 col-sm-3 col-xs-12" for="active">ACTIVE</label>
                        <div class="col-md-6 col-sm-6 col-xs-12">
                          <div class="">                            
                              <form:checkbox id="active" path="active" name="active" class="js-switch"  />
                         </div>
                          <form:errors path="active" cssClass="alerttt"/>
                        </div>
                      </div>

                      <div class="item form-group">
                        <label class="control-label col-md-3 col-sm-3 col-xs-12" for="APPLCTN_CD">APPLCTN CD </label>
                        <div class="col-md-6 col-sm-6 col-xs-12">
                          <form:input type="text" path="APPLCTN_CD" id="APPLCTN_CD" name="APPLCTN_CD" class="form-control col-md-7 col-xs-12"/>
                       </div>
                        <form:errors path="APPLCTN_CD" cssClass="alerttt" />
                      </div>

                      <div class="item form-group">
                        <label class="control-label col-md-3 col-sm-3 col-xs-12" for="APPLCTN_ID">APPLCTN ID 
                        </label>
                        <div class="col-md-6 col-sm-6 col-xs-12">
                          <form:input type="text" path="APPLCTN_ID" id="APPLCTN_ID" name="APPLCTN_ID"   class="form-control col-md-7 col-xs-12"/>
                       </div>
                        <form:errors path="APPLCTN_ID" cssClass="alerttt"/>
                      </div>

                      <div class="item form-group">
                        <label class="control-label col-md-3 col-sm-3 col-xs-12" for="APPLCTN_DS">APPLCTN DS 
                        </label>
                        <div class="col-md-6 col-sm-6 col-xs-12">
                          <form:input type="text" path="APPLCTN_DS" id="APPLCTN_DS" name="APPLCTN_DS"   class="form-control col-md-7 col-xs-12"/>                                                   
                        </div>
                        <form:errors path="APPLCTN_DS" cssClass="alerttt"/>
                      </div>
                      <div class="item form-group">
                        <label class="control-label col-md-3 col-sm-3 col-xs-12" for="DECLG_IDNTFNT_NU">DECLG IDNTFNT NU
                        </label>
                        <div class="col-md-6 col-sm-6 col-xs-12">
                          <form:input type="text" path="DECLG_IDNTFNT_NU" id="DECLG_IDNTFNT_NU" name="DECLG_IDNTFNT_NU"  class="form-control col-md-7 col-xs-12"/>
                       </div>
                        <form:errors path="DECLG_IDNTFNT_NU" cssClass="alerttt"/>
                      </div>

                      <c:if test="${edit}">
                       <div class="item form-group">
                          <label class="control-label col-md-3 col-sm-3 col-xs-12" for="version">VERSION
                          </label>
                            <div class="col-md-6 col-sm-6 col-xs-12">
                              <form:input type="text" path="version" id="version" name="version"  class="form-control col-md-7 col-xs-12"/>
                        </div>
                            <form:errors path="version" cssClass="alerttt"/>
                      </div>
                      </c:if>
                      <c:if test="${!edit}">
                       <div class="item form-group">
                          <label class="control-label col-md-3 col-sm-3 col-xs-12" for="version">VERSION
                          </label>
                            <div class="col-md-6 col-sm-6 col-xs-12">
                              <form:input disabled="true" type="text" path="version" id="version" name="version"  class="form-control col-md-7 col-xs-12"/>
                        </div>
                            <form:errors path="version" cssClass="alerttt"/>
                      </div>
                      </c:if>

                      <div class="item form-group">
                        <label class="control-label col-md-3 col-sm-3 col-xs-12" for="CONTEXT">CONTEXT</label>
                        <div class="col-md-6 col-sm-6 col-xs-12">
                          <form:select itemValue="id" path="CONTEXTE" itemLabel="CONTEXTE_CD" items="${cntxt}" class="select2_single form-control" tabindex="-1" />
                      </div>
                        <form:errors path="CONTEXTE" cssClass="alerttt"/>
                      </div>



                      <div class="ln_solid"></div>
                      <div class="form-group">
                        <div class="col-md-6 col-md-offset-3">
                            <c:choose>
                                <c:when test="${edit}">
                                    <button type="button" class="btn btn-primary" onClick="location.href='<c:url value='/appli' />'">Annuler</button>
                                    <input type="submit" class="btn btn-success" value="Modifier" />
                                </c:when>
                                <c:otherwise>
                                        <button type="button" class="btn btn-primary" onClick="location.href='<c:url value='/appli' />'">Annuler</button>
                                    <input type="submit" class="btn btn-success" value="Ajouter"/>
                                </c:otherwise>
                            </c:choose>
                        </div>
                      </div>
                    </form:form>

感谢任何建议..

1 个答案:

答案 0 :(得分:0)

通过更改pathVariable的名称来解决此问题,因为它与ID相同,并且在我的实体中添加了 set for id

    @RequestMapping(value = { "/edtApp-{i}" }, method = RequestMethod.GET)
            public String editApp(@PathVariable int i, ModelMap model) {
             //....
            }

            @RequestMapping(value = { "/edtApp-{i}" }, method = RequestMethod.POST)
            public String updateApp(@Valid @ModelAttribute("application") {
              //....
            }