springMvc get参数转码

原创
2015/09/15 15:20
阅读数 1.2K
//字符串转参数转码
import java.beans.PropertyEditorSupport;
import java.io.UnsupportedEncodingException;

import org.apache.commons.lang.StringUtils;

public class CustomStringEditor extends PropertyEditorSupport{
	
	private String charset;

	public CustomStringEditor() {}
	
	public CustomStringEditor(String charset) {
		this.charset = charset;
	}
	
	@Override
	public void setAsText(String text) throws IllegalArgumentException {
		try {
			if (StringUtils.isNotBlank(text)) {
				text = ConverStr.changeCharset(text,ConverStr.ISO_8859_1, charset);
			}
			setValue(text);
			return;
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
	}
	
	public void setCharset(String charset) {
		this.charset = charset;
	}
	
}

//controller应用
@InitBinder
public void initBinder(WebDataBinder binder, HttpServletRequest request) {
    if ("GET".equalsIgnoreCase(request.getMethod())) {
        binder.registerCustomEditor(String.class, new CustomStringEditor(ConverStr.UTF_8));
    }
}


展开阅读全文
加载中

作者的其它热门文章

打赏
0
1 收藏
分享
打赏
0 评论
1 收藏
0
分享
返回顶部
顶部