1. 程式人生 > 其它 >maven工程過程中常見的錯誤(初學者筆記)

maven工程過程中常見的錯誤(初學者筆記)

技術標籤:學習問題彙總javamavenhtml

1.提交時日期格式沒有設定,如下錯誤:(解決方式:在entity(有些是bean或是pojo)包中的實體類中日期變數需要加註解:

    //設定時間格式,在時間提交到資料庫表時如果沒有這個會因為時間格式不一致而無法提交
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date hiredate;

下面是錯誤 ,上面是以下錯誤的解決方法

Field error in object 'employee' on field 'hiredate': rejected value [2020-12-07]; codes [typeMismatch.employee.hiredate,typeMismatch.hiredate,typeMismatch.java.util.Date,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [employee.hiredate,hiredate]; arguments []; default message [hiredate]]; default message [Failed to convert property value of type [java.lang.String] to required type [java.util.Date] for property 'hiredate'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [java.util.Date] for value '2020-12-07'; nested exception is java.lang.IllegalArgumentException]
	at org.springframework.web.method.annotation.ModelAttributeMethodProcessor.resolveArgument(ModelAttributeMethodProcessor.java:113)
	at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:78)
	at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:162)
	at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:129)
	at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110)
	at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:814)
	at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:737)
	at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
	at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959)
	at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893)
	at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:969)
	at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:871)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
	at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:845)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
	at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
	at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:121)
	at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
........

2.以下第一張圖是原因及解決方法分析(圖中的value是錯誤的,應該是addEmp才對),第二張圖是錯誤。當提交修改或者新增失敗時,從以下幾個方面檢查:

(1)註解,註解的value是否和前端提交的action一致,method是否一致,是否少了註解

(1)程式碼書寫是否有錯誤

/**
     * 框架中的配置,已經實現了提交表格資料的自動封裝
     * 註解:@RequestMapping(value = "/addEmp",method = RequestMethod.POST,produces = "text/html;charset=UTF-8")
     * value是前端頁面表單提交的路徑:action="${pageContext.request.contextPath}/addEmp"中的addEmp,兩者需要完全一致
     * method是前端提交的方法,produces是提交後返回值呈現的格式
     * 註解:@ResponseBody響應前端
     * @param employee
     * @return
     */
    @RequestMapping(value = "/addEm",method = RequestMethod.POST,produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String addEmp(Employee employee){
        if(employeeService.addEmp(employee)){
            //新增成功,返回列表頁:list.jsp
            return "<script>alert('新增成功!');location.href='/list';</script>";
        }else{
            //history.go(-1)表示返回上一頁,就是新增頁addEmp.jsp
            return "<script>alert('新增失敗!');history.go(-1);</script>";
        }
    }
HTTP Status 404 - /addEmp

type Status report
message /addEmp
description The requested resource is not available.

Apache Tomcat/7.0.47

3.注意前端的屬性名是否和物件的變數名完全一致,如下圖就不一致,提交就會失敗。把前端的引數和實體類的名字改成一樣就對了:bsalary改成bsaralry(注:正確單詞是bsalary,工資的意思)

 <td><input type="text" name="bsalary" id="bsalary"  value="${requestScope.employee.bsaralry}" /></td>

4.對於修改資料表之類,我們常常會遇到提交時失敗,還有一個可能原因是:我們提交的資料沒有完整,在前端設定中常常會遺漏隱藏的值,如下面的empid,如果漏了這一行,提交修改也無法成功

<table ....>

<%--這句不能漏,漏了就提交修改時沒有empid了--%>
                <tr><td><input type="hidden" name="empid" id="empid" value="${requestScope.employee.empid}" /></td></tr>

.....

</table>

5.提交方式失敗,把後端的提交方式再加一個模式就行了,把get和post都寫上

HTTP Status 405 - Request method 'GET' not supported

type Status report
message Request method 'GET' not supported
description The specified HTTP method is not allowed for the requested resource.

Apache Tomcat/7.0.47

解決方法如下:把method多加一個方法即可
    @RequestMapping(value = "/deleteEmp",method = {RequestMethod.GET,RequestMethod.POST},produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String deleteEmp(@RequestParam(value = "empid") Integer empid){.....}

6.引數名沒有匹配,提示錯誤

HTTP Status 400 - Required Integer parameter 'empid' is not present

type Status report
message Required Integer parameter 'empid' is not present
description The request sent by the client was syntactically incorrect.

Apache Tomcat/7.0.47

7.新增後,出現亂碼。原因:我在這裡多了一個逗號,把這個逗號去掉就好了

        <form action="${pageContext.request.contextPath}/addEmp", method="post">