标签:desc idea 哈哈 not splay RoCE ring control -name
此文转载自:https://blog.csdn.net/weixin_43883917/article/details/113098996
注意:表单提交的任何数据类型全部都是字符串类型,但是后台定义Integer类型,数据也可以封装上,说明Spring框架内部会默认进行数据类型转换。
1、自定义类型转换器,实现Converter的接口
StringToDateConverter类:
package com.Keafmd.utils;
import org.springframework.core.convert.converter.Converter;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* Keafmd
*
* @ClassName: StringToDateConverter
* @Description: 把字符串转换成日期的转换器
* @author: 牛哄哄的柯南
* @date: 2021-01-24 19:27
*/
public class StringToDateConverter implements Converter<String,Date> {
@Override
public Date convert(String s) {
if(s==null){
throw new RuntimeException("请传入数据");
}
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
try {
//把字符串转为日期
return dateFormat.parse(s);
} catch (ParseException e) {
throw new RuntimeException("数据类型转换错误");
}
}
}
2、注册自定义类型转换器,在springmvc.xml配置文件中编写配置。
<!--配置自定义类型转换器-->
<bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean">
<property name="converters">
<set>
<bean class="com.Keafmd.utils.StringToDateConverter"/>
</set>
</property>
</bean>
<!-- 开启SpringMVC框架注解支持,注解配置spring开启注解mvc的支持 -->
<mvc:annotation-driven conversion-service="conversionService"></mvc:annotation-driven>
User类:
package com.Keafmd.domain;
import java.io.Serializable;
import java.util.Date;
/**
* Keafmd
*
* @ClassName: User
* @Description:
* @author: 牛哄哄的柯南
* @date: 2021-01-24 16:14
*/
public class User implements Serializable {
private String uname;
private Integer age;
private Date birthday;
public String getUname() {
return uname;
}
public void setUname(String uname) {
this.uname = uname;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
@Override
public String toString() {
return "User{" +
"uname=‘" + uname + ‘\‘‘ +
", age=" + age +
", birthday=" + birthday +
‘}‘;
}
}
控制器代码:
package com.Keafmd.controller;
import com.Keafmd.domain.Account;
import com.Keafmd.domain.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
/**
* Keafmd
*
* @ClassName: ParamController
* @Description:
* @author: 牛哄哄的柯南
* @date: 2021-01-24 15:57
*/
@Controller
@RequestMapping("/param")
public class ParamController {
/**
* 自定义类型转换器
* @param user
* @return
*/
@RequestMapping("/saveUser")
public String testParam(User user){
System.out.println(user);
return "success";
}
}
jsp代码:
<%--
Created by IntelliJ IDEA.
User: Keafmd
Date: 2021/1/24
Time: 15:55
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>请求参数绑定</title>
</head>
<body>
<%--自定义类型转换器--%>
<form action="param/saveUser" method="post">
用户姓名:<input type="text" name="uname" /><br/>
用户年龄:<input type="text" name="age" /><br/>
用户生日:<input type="text" name="birthday" /><br/>
<input type="submit" value="提交">
</form>
</body>
</html>
浏览器输入:
输出结果:
User{uname=‘牛哄哄的柯南‘, age=21, birthday=Sat Jan 01 00:00:00 CST 2000}
以上就是SpringMVC中自定义(日期)类型转换器的全部内容。
看完如果对你有帮助,感谢点赞支持!
如果你是电脑端的话,看到右下角的 “一键三连” 了吗,没错点它[哈哈]
加油!
共同努力!
Keafmd
标签:desc idea 哈哈 not splay RoCE ring control -name
原文地址:https://www.cnblogs.com/phyger/p/14330903.html