由于发送一个请求路径返回到浏览器是一串json字符串,虽然写了过滤器但是实际上过滤器没有拦截到。因此它是一段纯文本格式,所有要设置它的编码类型和字符编码,
也就是response.setContentType("text/plain;charset=UTF-8"),但是它的设定一定要在pw.write(json)之前。否则还是会出现乱码。
public class BaseAction extends ActionSupport implements ServletRequestAware, ServletResponseAware{
/**
* @Fields serialVersionUID : 序列化
*/
private static final long serialVersionUID = 2823334602737926536L;
public static final String JSON_ERROR = "{\"success\":\"false\"}";
public static final String JSON_SUCCESS = "{\"success\":\"true\"}";
protected HttpServletRequest _request;
protected HttpServletResponse _response;
@Override
public void setServletResponse(HttpServletResponse response) {
this._response=response;
this._response.setContentType("text/plain;charset=UTF-8");
}
@Override
public void setServletRequest(HttpServletRequest request) {
this._request=request;
}
@JSON(serialize=false)
public void returnJson(String json){
PrintWriter pw=null;
try {
pw=this._response.getWriter();
pw.write(json);
pw.flush();
} catch (IOException e) {
e.printStackTrace();
}finally{
if (pw!=null) {
pw.close();
}
}
}
}