日期:2014-05-16 浏览次数:20678 次
在做ajax应用时,如果一个会话已经超时,但是此时再通过ajax请求,那么ajax返回的则是一个登陆页面HTML代码,这肯定不是我们想要的。那么,怎样解决该问题呢?
如何解决上面两个问题呢?
我们可以自定义一个存放结果信息的类,让它包含消息及结果
public class ExecuteResult<T> { private T result; private String successMessage; private List<String> errorMessages = new ArrayList<String>(); private Map<String, String> fieldErrors = new HashMap<String, String>(); private List<String> warningMessages = new ArrayList<String>(); public T getResult() { return result; } public void setResult(T result) { this.result = result; } public boolean isSuccess() { return (this.errorMessages.isEmpty()) && (this.fieldErrors.isEmpty()); } public String getSuccessMessage() { return successMessage; } public void setSuccessMessage(String successMessage) { this.successMessage = successMessage; } public List<String> getErrorMessages() { return errorMessages; } public void setErrorMessages(List<String> errorMessages) { this.errorMessages = errorMessages; } public Map<String, String> getFieldErrors() { return fieldErrors; } public void setFieldErrors(Map<String, String> fieldErrors) { this.fieldErrors = fieldErrors; } public List<String> getWarningMessages() { return warningMessages; } public void setWarningMessages(List<String> warningMessages) { this.warningMessages = warningMessages; } public void addErrorMessage(String errorMessage) { this.errorMessages.add(errorMessage); } public void addFieldError(String field, String errorMessage) { this.fieldErrors.put(field, errorMessage); } public void addWarningMessage(String warningMessage) { this.warningMessages.add(warningMessage); } }
?
?
if(userName == null){ String requestType = request.getHeader("X-Requested-With"); //判断是否为AJAX请求 if(requestType!=null && "XMLHttpRequest".equals(requestType)){ ExecuteResult<String> rlt = new ExecuteResult<String>(); rlt.addErrorMessage("AJAX SESSION TIMEOUT."); response.getWriter().write(JSONObject.fromObject(rlt).toString()); return Action.NONE; }else{ return Action.LOGIN; } }
?
?
public class SearchResourceAction extends BaseSecurityAction { private static final long serialVersionUID = -522813327706559035L; @SuppressWarnings("unchecked") private ExecuteResult executeResult; //ExecuteResult setter and Getter方法省略.. @SuppressWarnings("unchecked") public String getNodes() throws Exception { String pid = ""; if (loadNode != null) { pid = loadNode[0]; } List<ResourceObj> objs = resourceService.searchResources(pid); //executeResult.addErrorMessage(""); executeResult.setResult(objs); return SUCCESS; } }
?
$.ajax({ url:url, data:params, type:'post', dataType:'json', success:function(data){ if(data.errorMessages && data.errorMessages.length > 0 ){ alert(data.errorMessages); }else if(data.actionErrors && data.actionErrors.length > 0 ){ alert(data.actionErrors); }else if(data.actionMessages.length > 0 ){ alert(data.actionMessages); }else if(data.fieldErrors && data.fieldErrors.length > 0 ){ alert(data.fieldErrors); }else{ //业务逻辑处理 } }error:function(data){ alert(data); } });
??
一般自定义结果类型中不需要添加那么多添加消息的方法,但考虑到一半请求时,可以将ExecuteResult中的消息转给Action,在前台用Struts标签直接显示,所以就添加了很多消息