maven工程过程中常见的错误1(初学者笔记)

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">

猜你喜欢

转载自blog.csdn.net/preston555/article/details/110796000