SSM的CRM系統項目開發遇到的問題彙總

目錄

 

1.mapper實例化問題

2.日期格式轉換問題

3.表單自動提交和頁面刷新問題

4.通過ajax異步請求後臺,返回的JSON數據格式問題

4.導入項目時遇到多個版本JDK問題


 

1.mapper實例化問題

如下:

嚴重: Servlet.service() for servlet [springmvc] in context with path [/crm] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause
java.lang.NullPointerException
	at com.luckylas.crm.service.SalePlanService.addSP(SalePlanService.java:36)
	at com.luckylas.crm.controller.SalePlanController.addSP(SalePlanController.java:42)

原因:

      Mapper對象沒有添加@Autowire註解進行實例化

2.日期格式轉換問題

如下:

org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 2 errors
Field error in object 'student' on field 'create_date': rejected value [2018-10-13]; codes [typeMismatch.student.create_date,typeMismatch.create_date,typeMismatch.java.util.Date,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [student.create_date,create_date]; arguments []; default message [create_date]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'create_date'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [java.util.Date] for value '2018-10-13'; nested exception is java.lang.IllegalArgumentException]

原因

        實體類日期格式爲java.util.Date,前端傳遞的日期格式爲String,因此類型不匹配。

解決方式

      1.重寫一個實體類日期字段的get方法,如下所示:

 public void setSpBegintime(String timeStr) {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    try {
        this.spBegintime = sdf.parse(timeStr);
    } catch (ParseException e) {
        e.printStackTrace();
    }
}

      2.在Controller中添加如下方法:

@InitBinder
public void init(WebDataBinder binder) {
    binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), true));
}

 

3.表單自動提交和頁面刷新問題

      在添加銷售計劃執行記錄的時候,點擊提交按鈕後,頁面刷新報錯如下:

         點擊執行銷售計劃按鈕前頁面地址如下:

        編輯提交的內容,如下:

     點擊提交按鈕後,後臺新增計劃記錄成功,頁面重新刷新報錯如下:

        其中詳細信息如下:

java.lang.IllegalStateException: Optional int parameter 'spId' is present but cannot be 
translated into a null value due to being declared as a primitive type. Consider declaring it as 
object wrapper for the corresponding primitive type.

       其中表單入下:

<form style="width:100%;" id="formEditSPE" class="form-horizontal" role="form">
    <div class="form-group" style="margin-top:10px;">
        <label for="firstname" class="col-sm-2 control-label">銷售計劃ID:</label>
        <div class="col-sm-10">
            <input type="text" id="spePid" class="form-control" name="spePid" readonly="readonly">
        </div>
    </div>
    <div class="form-group" style="margin-top:10px;">
        <label for="firstname" class="col-sm-2 control-label">執行日期:</label>
        <div class="col-sm-10">
            <input type="date" class="form-control" name="speExetime">
        </div>
    </div>
    <div class="form-group" style="margin-top:10px;">
        <label for="firstname" class="col-sm-2 control-label">執行情況:</label>
        <div class="col-sm-10">
            <textarea class="form-control" name="speExecase" rows="13"></textarea>
        </div>
    </div>
    <div class="form-group">
        <div class="col-sm-offset-2 col-sm-10">
            <button id="btnSubmitExeSP" class="btn btn-primary">提交</button>
        </div>
    </div>
</form>

     可以看到,添加執行計劃前瀏覽器和添加後的地址不一樣:

添加前:
http://localhost:8080/crm/salePlan/getSPBySPId.do?spId=1
添加後:
http://localhost:8080/crm/salePlan/getSPBySPId.do?spePid=1&speExetime=2019-09-30&speExecase=提交內容

       用於表單自動提交導致,將表單裏面的字段添加到請求地址中去了。取消表單自動提交方法如下:

  • 設置 form 的 οnsubmit="return false";
  • <button>按鈕中添加type="button"屬性

 

4.通過ajax異步請求後臺,返回的JSON數據格式問題

     後臺傳遞回來的數據result如下所示:

"{\"message\":\"操作成功!\",\"status\":\"1\"}"

      通過JSON.parse(result)後,才變成JSON字符串形式,數據如下:

{"message":"操作成功!","status":"1"}

       再通過JSON.parse(result)轉換一次,纔可以轉換成爲JSON對象格式。其問題在於沒有設置dataType。需要在ajax請求中設置dateType:"json",如下:

$.ajax({
    url:"${pageContext.request.contextPath}/salePlan/adjaxFailSP.do?spId="+spId,
    async:true,
    dataType:"json",
    success:function(result){
        result = JSON.parse(result);
        alert(result.message);
        location.reload(); 
    }
}); 

          設置後result是JSON字符串形式,只需要轉換一次即可轉換爲JSON對象格式。

4.導入項目時遇到多個版本JDK問題

問題報錯描述

The type java.lang.String cannot be resolved. It is indirectly referenced from required .class files

原因:多個JDK版本衝突

解決辦法:點擊項目右鍵->properties->Java Bulid Path  將原有的JRE System Library刪除  ,添加新的即可。

 

5.導入項目中文亂碼

      編碼問題。右鍵選擇properties,選擇resources屬性,在頁面下方可以看到Text file encoding項,重新選擇編碼即可。

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章