设为首页 加入收藏

TOP

day10-中文乱码处理(一)
2023-07-25 21:44:01 】 浏览:52
Tags:day10- 文乱码

中文乱码处理

1.问题抛出

当表单提交的数据为中文时,会出现乱码:

(1)Monster.java

package com.li.web.datavalid.entity;

import org.hibernate.validator.constraints.Email;
import org.hibernate.validator.constraints.NotEmpty;
import org.hibernate.validator.constraints.Range;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.format.annotation.NumberFormat;

import javax.validation.constraints.NotNull;
import java.util.Date;

/**
 * @author 李
 * @version 1.0
 */
public class Monster {
    @NotNull(message = "id不能为空")
    private Integer id;

    @Email
    @NotEmpty(message = "邮件不能为空")
    private String email;

    @Range(min = 1, max = 100)
    @NotNull(message = "年龄age不能为空")
    private Integer age;

    @NotEmpty
    private String name;

    @DateTimeFormat(pattern = "yyyy-MM-dd")
    @NotNull(message = "生日不能为空")
    private Date birthday;

    @NumberFormat(pattern = "###,###.##")
    @NotNull(message = "工资不能为空")
    private Float salary;

    public Monster() {
    }

    public Monster(Integer id, String email, Integer age, String name, Date birthday, Float salary) {
        this.id = id;
        this.email = email;
        this.age = age;
        this.name = name;
        this.birthday = birthday;
        this.salary = salary;
    }

    public Integer getId() {
        return id;
    }

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    public Float getSalary() {
        return salary;
    }

    public void setSalary(Float salary) {
        this.salary = salary;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Monster{" +
                "id=" + id +
                ", email='" + email + '\'' +
                ", age=" + age +
                ", name='" + name + '\'' +
                ", birthday=" + birthday +
                ", salary=" + salary +
                '}';
    }
}

(2)MonsterHandler.java

package com.li.web.datavalid;

import com.li.web.datavalid.entity.Monster;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BeanPropertyBindingResult;
import org.springframework.validation.Errors;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * @author 李
 * @version 1.0
 */
@Controller
@Scope(value = "prototype")
public class MonsterHandler {

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public String save(@Valid Monster monster, Errors errors, Map<String, Object> map) {
        System.out.println("----monster----" + monster);
        //为了查看验证的情况,输出map和errors
        System.out.println("=======map=======");
        for (Map.Entry<String, Obje
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Linux环境中下载安装Maven 下一篇《分布式技术原理与算法解析》学..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目