SSM的CRM系统项目开发遇到的问题汇总

目录

 

1.mapper实例化问题

2.日期转换问题

3.表单自动提交和页面刷新问题


 

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对象格式。

发布了131 篇原创文章 · 获赞 39 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/qq_35507234/article/details/101654089
今日推荐