//字符串转参数转码
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));
}
}