当SpringMVC在进行参数转换的时候出错时,浏览器就会提示400错误
自定义参数类型转换器
首先定义一个Book类做model,基本数据类型和数组类型都可以自动转换,由于springMVC默认可以识别的日期字符串格式为:yyyy/MM/dd hh:mm:ss,当格式不同时,就需要日期类型转换器。
book模板
默认情况下,变量名必须和表单中的name一致。如果不一致的话,可以通过@RequestParam(String name)注解进行配置,添加了这个注解后,默认就是必填参数,required=false之后就是非必填参数,或者添加一个默认值defaultValue
package com.qfedu.demo.model;
import java.util.Arrays;
import java.util.Date;
public class Book {
private String name;
private String author;
private Double price;
private String[] tags;
private Date publishDate;
@Override
public String toString() {
return "Book{" +
"name='" + name + '\'' +
", author='" + author + '\'' +
", price=" + price +
", tags=" + Arrays.toString(tags) +
", publishDate=" + publishDate +
'}';
}
public Date getPublishDate() {
return publishDate;
}
public void setPublishDate(Date publishDate) {
this.publishDate = publishDate;
}
public String[] getTags() {
return tags;
}
public void setTags(String[] tags) {
this.tags = tags;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public Double getPrice() {
return price;
}
public void setPrice(Double price) {
this.price = price;
}
}
book控制器
package com.qfedu.demo.controller;
import com.qfedu.demo.model.Book;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class BookController {
@GetMapping("/book")
public String bookPage() {
return "addbook";
}
@PostMapping("/addBook")
@ResponseBody
//本质上还是key-value的形式
public void addBook(Book book) {
System.out.println("book = " + book);
}
}
日期类型转换器
package com.qfedu.demo.converter;
import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* 自定义日期类型转换器
*/
@Component
public class DateConverter implements Converter<String, Date> {
private SimpleDateFormat sdf = new SimpleDateFormat("y-M-d");
/**
* @param source 前端上传的日期字符串
* @return 将字符串转为日期之后的对象
*/
@Override
public Date convert(String source) {
try {
return sdf.parse(source);
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
}
配置文件spring-context.xml
这里需要类型类型转换器,注意在配置之后,还要再driven中添加
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="com.qfedu.demo"/>
<!--配置类型转换器后,还要在这里添加conversion-service,也就是配置的类型转换器-->
<mvc:annotation-driven conversion-service="conversionServiceFactoryBean"/>
<!--配置类型转换器-->
<bean class="org.springframework.format.support.FormattingConversionServiceFactoryBean"
id="conversionServiceFactoryBean">
<property name="converters">
<set>
<!--将自定义的日期类型转换器配置给FormattingConversionServiceFactoryBean-->
<ref bean="dateConverter"/>
</set>
</property>
</bean>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
中文乱码问题
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
<init-param>
<param-name>forceRequestEncoding</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>forceResponseEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
前端代码addbook.jsp
<html>
<head>
<title>Title</title>
</head>
<body>
<form action="/s/addBook" method="post">
<table>
<tr>
<td>图书名:</td>
<td><input type="text" name="name"></td>
</tr>
<tr>
<td>图书价格:</td>
<td><input type="text" name="price"></td>
</tr>
<tr>
<td>作者:</td>
<td><input type="text" name="author"></td>
</tr>
<tr>
<td>标签:</td>
<td>
<input type="checkbox" name="tags" value="通俗小说">通俗小说
<input type="checkbox" name="tags" value="小说">小说
<input type="checkbox" name="tags" value="散文">散文
<input type="checkbox" name="tags" value="诗歌">诗歌
</td>
</tr>
<tr>
<td>出版日期:</td>
<td><input type="date" name="publishDate"></td>
</tr>
<tr>
<td><input type="submit" value="提交"></td>
</tr>
</table>
</form>
</body>
</html>
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/14611.html