搭建ssm框架遇到的问题:The request sent by the client was syntactically incorrect
2017-11-19 12:12
525 查看
今天我在搭建ssm框架的时候浏览器页面出现错误:The request sent by the client was syntactically incorrect.意思是客户端的请求不正确。
我前台请求的界面age不填;后台person类的age是一个int类型;
private int age;
然后我查看后台发现出现错误:
Field error in object 'person' on field 'age': rejected value [];
codes [typeMismatch.person.age,typeMismatch.age,typeMismatch.int,typeMismatch];但是我配置mapper的时候age这个字段在插入的时候设置了数据库的字段类型。然后当age为null的时候,就是0。这个是我后台的Controller:
@RequestMapping("/person/insert.action")
public String insert(Person person)
{
personService.insert(person);
return "redirect:/person/list.action";
}
然后我debug我发现我前台传过来我后台接收的时候person里面的age是"",不是null;我的第一反应我是不是吧这个字段的类型设置为String,然后就可以了。然后我吧Person类改成Private String age;但是这样也是不行的。Person能够正常赋值"",但是这个字段在如下:
<!-- 新增 -->
<insert id="insert" parameterType="cn.itcast.ssm.domain.Person">
<!-- insert into person (id,user_name,age,remark) -->
insert into person (id ,user_name,age,remark)
values
(#{id},
<!-- ( -->
#{name,jdbcType=VARCHAR},
#{age,jdbcType=INTEGER},
#{remark,jdbcType=VARCHAR})
</insert>执行插入的时候就出现错误,因为age这个字段要求的是INTEGER。所以我就在service层首先做一个判断,判断这个Person的age属性值是否为"",如果是,那么就设置为"0",如下:
if(person.getAge().equals(""))
{
person.setAge("0");
}
这样虽然传的是一个字符串"0",数据库要求的是integer,但是还是能够穿进去。但是始终觉得这种方法很烂。所以经过不懈的努力
终于知道为什么我是int类型的age的时候,页面传过来的值是""而匹配不上。原因是我设置的这个Person类的age属性的类型是int类型。我应该设置这个age属性的类型是INTEGER类型,如果是Integer类型,那么当我页面传过来的是"",在自动赋值的时候就赋的是null,然后在执行mybatis的insert的时候,虽然这个是null,但是可以赋值,因为有#{age,jdbcType=INTEGER},这样一来我的页面中age可以不写,然后Person类的age属性是Integer,数据库中这个字段是int类型。
这里面主要的两个配置:age是Integer,还有mapper.xml文件中#{age,jdbcType=INTEGER},jdbcType=INTEGER表示当age是null的时候,这个值也是允许的。就会按照数据库类类型给一个值。
在这里在简单提下Mybatis中的几个小点:
要求这个id是自增长。可以直接在数据库中的这个字段上加上自动递增,这个在设计表-》然后选上这个字段,就可以了。之后在页面插入的这个时候这个字段可以省去如下:
<insert id="insert" parameterType="cn.itcast.ssm.domain.Person">
<!-- insert into person (id,user_name,age,remark) -->
insert into person (user_name,age,remark)
values
<!-- (#{id}, -->
(
#{name,jdbcType=VARCHAR},
#{age,jdbcType=INTEGER},
#{remark,jdbcType=VARCHAR})
</insert>
我前台请求的界面age不填;后台person类的age是一个int类型;
private int age;
然后我查看后台发现出现错误:
Field error in object 'person' on field 'age': rejected value [];
codes [typeMismatch.person.age,typeMismatch.age,typeMismatch.int,typeMismatch];但是我配置mapper的时候age这个字段在插入的时候设置了数据库的字段类型。然后当age为null的时候,就是0。这个是我后台的Controller:
@RequestMapping("/person/insert.action")
public String insert(Person person)
{
personService.insert(person);
return "redirect:/person/list.action";
}
然后我debug我发现我前台传过来我后台接收的时候person里面的age是"",不是null;我的第一反应我是不是吧这个字段的类型设置为String,然后就可以了。然后我吧Person类改成Private String age;但是这样也是不行的。Person能够正常赋值"",但是这个字段在如下:
<!-- 新增 -->
<insert id="insert" parameterType="cn.itcast.ssm.domain.Person">
<!-- insert into person (id,user_name,age,remark) -->
insert into person (id ,user_name,age,remark)
values
(#{id},
<!-- ( -->
#{name,jdbcType=VARCHAR},
#{age,jdbcType=INTEGER},
#{remark,jdbcType=VARCHAR})
</insert>执行插入的时候就出现错误,因为age这个字段要求的是INTEGER。所以我就在service层首先做一个判断,判断这个Person的age属性值是否为"",如果是,那么就设置为"0",如下:
if(person.getAge().equals(""))
{
person.setAge("0");
}
这样虽然传的是一个字符串"0",数据库要求的是integer,但是还是能够穿进去。但是始终觉得这种方法很烂。所以经过不懈的努力
终于知道为什么我是int类型的age的时候,页面传过来的值是""而匹配不上。原因是我设置的这个Person类的age属性的类型是int类型。我应该设置这个age属性的类型是INTEGER类型,如果是Integer类型,那么当我页面传过来的是"",在自动赋值的时候就赋的是null,然后在执行mybatis的insert的时候,虽然这个是null,但是可以赋值,因为有#{age,jdbcType=INTEGER},这样一来我的页面中age可以不写,然后Person类的age属性是Integer,数据库中这个字段是int类型。
这里面主要的两个配置:age是Integer,还有mapper.xml文件中#{age,jdbcType=INTEGER},jdbcType=INTEGER表示当age是null的时候,这个值也是允许的。就会按照数据库类类型给一个值。
在这里在简单提下Mybatis中的几个小点:
要求这个id是自增长。可以直接在数据库中的这个字段上加上自动递增,这个在设计表-》然后选上这个字段,就可以了。之后在页面插入的这个时候这个字段可以省去如下:
<insert id="insert" parameterType="cn.itcast.ssm.domain.Person">
<!-- insert into person (id,user_name,age,remark) -->
insert into person (user_name,age,remark)
values
<!-- (#{id}, -->
(
#{name,jdbcType=VARCHAR},
#{age,jdbcType=INTEGER},
#{remark,jdbcType=VARCHAR})
</insert>
相关文章推荐
- spring mvc 数据绑定问题 提交表单提示HTTP status 400, The request sent by the client was syntactically incorrect
- The request sent by the client was syntactically incorrect-SpringMVC问题详解
- springmvc提交表单,格式问题:The request sent by the client was syntactically incorrect.
- 服务器,tomcat,网页请求错误400,The request sent by the client was syntactically incorrect的问题
- The request sent by the client was syntactically incorrect. 400 问题
- spring mvc 数据绑定问题 提交表单提示HTTP status 400, The request sent by the client was syntactically incorrect
- The request sent by the client was syntactically incorrect ().问题排查
- The request sent by the client was syntactically incorrect ()问题
- spring mvc 400,The request sent by the client was syntactically incorrect的问题
- POST 400 Bad Request The request sent by the client was syntactically incorrect
- 错误400-The request sent by the client was syntactically incorrect
- 错误The request sent by the client was syntactically incorrect ()的解决
- The request sent by the client was syntactically incorrect
- The request sent by the client was syntactically incorrect.
- 又见The request sent by the client was syntactically incorrect ()
- The request sent by the client was syntactically incorrect ().(404)
- Spring MVC "The request sent by the client was syntactically incorrect ()"解决办法
- Spring MVC "The request sent by the client was syntactically incorrect ()"解决办法
- springMVC 页面填写日期运行时发生异常!The request sent by the client was syntactically incorrect
- HTTP Status 400 - The request sent by the client was syntactically incorrect.