使用 springMVC 请求Action 返回400编码


使用 springMVC 请求Action 返回400编码

 环境:j2ee web网站,使用spring MVC框架,页面时间选择控件使用的是 easyui-datebox
 
 问题描述:
在请求指定 action方法时,方法参数为自定义类型
public class Person {
	private Integer id;
	private String name;
	private Integer age;
	private Date birth;
	
	
	public Date getBirth() {
		return birth;
	}
	public void setBirth(Date birth) {
		this.birth = birth;
	}
	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 Integer getAge() {
		return age;
	}
	public void setAge(Integer age) {
		this.age = age;
	}
	
	
	@Override
	public String toString() {
		return "Person [id=" + id + ", name=" + name + ", age=" + age + "]";
	}
	
	
	
	
}
当运行网站,在浏览器上填写用户数据时候,选择生日日期。提交数据后提示400错误请求。

问题跟踪:
尝试了在浏览器地址直接填写请求数据,没有日期数据是正确显示的。
问题解决:
统一前台和后台的时间格式,实体对象可以指定格式:例如:@DateTimeFormat(pattern = "yyyy-MM-dd") 或者其他时间格式。
 

备注:有任何问题,请多多指教!

热爱编程,热爱生活!
Spring MVC 中处理编码问题可以通过以下几个步骤来完成: 1. 设置请求编码:在 Spring MVC 中,可以通过配置 `CharacterEncodingFilter` 来设置请求编码。将以下代码添加到你的 `WebApplicationInitializer` 或者 `web.xml` 文件中: ```java public class MyWebApplicationInitializer implements WebApplicationInitializer { @Override public void onStartup(ServletContext servletContext) throws ServletException { CharacterEncodingFilter encodingFilter = new CharacterEncodingFilter(); encodingFilter.setEncoding("UTF-8"); encodingFilter.setForceEncoding(true); FilterRegistration.Dynamic filter = servletContext.addFilter("encodingFilter", encodingFilter); filter.addMappingForUrlPatterns(null, false, "/*"); } } ``` 这样配置后,所有的请求都会使用 UTF-8 编码。 2. 设置响应编码:为了确保响应也使用正确的编码,可以通过在 `@RequestMapping` 注解或者控制器方法上添加 `produces` 属性来指定响应编码。例如: ```java @GetMapping(value = "/example", produces = "text/html;charset=UTF-8") public String exampleMethod() { // 方法内容 } ``` 3. 处理表单提交编码:如果你的表单提交存在编码问题,可以在表单页面的 `<form>` 标签中添加 `accept-charset` 属性来指定编码。例如: ```html <form action="/submit" method="post" accept-charset="UTF-8"> <!-- 表单内容 --> </form> ``` 4. 配置视图解析器:如果你使用了视图解析器来渲染视图,可以在视图解析器中设置编码。例如: ```java @Bean public ViewResolver viewResolver() { InternalResourceViewResolver viewResolver = new InternalResourceViewResolver(); viewResolver.setPrefix("/WEB-INF/views/"); viewResolver.setSuffix(".jsp"); viewResolver.setContentType("text/html;charset=UTF-8"); return viewResolver; } ``` 通过以上步骤,你可以在 Spring MVC 中处理编码问题,确保请求和响应使用正确的编码
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值