SpringMVC错误,商品添加信息HTTP Status 400 – Bad Request
记录一个自己在做商品信息显示与传递数据的时候出现的错误,
HTTP Status 400 – Bad Request
Type Status Report
Description The server cannot or will not process the request due to something that is perceived to be a client error (e.g., malformed request syntax, invalid request message framing, or deceptive request routing).
简单的说我在商品信息里有个Date createDate的属性类型。但是在前台传数据的时候是个String类型,因此会出现这个错误,可能是这个错误吧,我当时没复制,找了个网上问题相似的。我最开始解决这问题是在
SpringMVC.xml里配置了
<bean id="conversionServer"
class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<!-- 日期转换器 -->
<property name="converters">
<list>
<bean class="com.mju.band3.Converter.DateConverter" />
</list>
</property>
</bean>
这个是DateConverter代码
package com.mju.band3.Converter;
import org.springframework.core.convert.converter.Converter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateConverter implements Converter<String, Date> {
private String datePattern="yyyy-MM-dd";
@Override
public Date convert(String s) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(datePattern);
try {
return simpleDateFormat.parse(s);
} catch (ParseException e) {
throw new IllegalArgumentException("无效的日期格式");
}
}
}
然而他出现了一个我弄了好几天都没弄好的问题,也就是我的JSP所有样式失效,而且也蹦掉,怀疑是jar版本问题,因为我再练习这个项目的时候出现了N多个版本问题的错误,
反正我是不知道为啥,那位大神能指点迷津,
那么我就去换了个方法,觉得这个方法也不错。贴上我的Bean
package com.mju.band3.Bean;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Item {
private Integer id;
private String name;
private float price;
private String detail;
private String pic;
private Date createtime;
public Item(Integer id, String name, float price, String detail, String pic, Date createtime) {
this.id = id;
this.name = name;
this.price = price;
this.detail = detail;
this.pic = pic;
this.createtime = createtime;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
public String getDetail() {
return detail;
}
public void setDetail(String detail) {
this.detail = detail;
}
public String getPic() {
return pic;
}
public void setPic(String pic) {
this.pic = pic;
}
public Date getCreatetime() {
return createtime;
}
public void setCreatetime(String str) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date createtime;
try {
createtime = sdf.parse(str);
this.createtime = createtime;
} catch (Exception e) {
e.printStackTrace();
}
}
public Item() {
super();
}
@Override
public String toString() {
return "Item{" +
"id=" + id +
", name='" + name + '\'' +
", price=" + price +
", detail='" + detail + '\'' +
", pic='" + pic + '\'' +
", createtime=" + createtime +
'}';
}
}
着重看setCreatetime方法,完美解决,贴出来分享下小白吧,我自己也是小白。
明天找机会再分享下上传文件显示的问题,也当做给自己成长记录,同时做做笔记吧。
兴许以后会来看自己的代码会觉得可笑,但也挺值得回味的。