structs与ajax结合的问题
2014-04-02 21:30
239 查看
structs2与ajax结合,无论采用$.post还是$.ajax都捕获不到服务器产生的错误,这是structs2内部设计的不是很好,
但是$.ajax捕获错误在servlet能做到。因为
public void serviceAction(HttpServletRequest request, HttpServletResponse response, ServletContext context,
ActionMapping mapping) throws ServletException {
Map<String, Object> extraContext = createContextMap(request, response, mapping, context);
// If there was a previous value stack, then create a new copy and pass it in to be used by the new Action
ValueStack stack = (ValueStack) request.getAttribute(ServletActionContext.STRUTS_VALUESTACK_KEY);
boolean nullStack = stack == null;
if (nullStack) {
ActionContext ctx = ActionContext.getContext();
if (ctx != null) {
stack = ctx.getValueStack();
}
}
if (stack != null) {
extraContext.put(ActionContext.VALUE_STACK, valueStackFactory.createValueStack(stack));
}
String timerKey = "Handling request from Dispatcher";
try {
UtilTimerStack.push(timerKey);
String namespace = mapping.getNamespace();
String name = mapping.getName();
String method = mapping.getMethod();
Configuration config = configurationManager.getConfiguration();
ActionProxy proxy = config.getContainer().getInstance(ActionProxyFactory.class).createActionProxy(
namespace, name, method, extraContext, true, false);
request.setAttribute(ServletActionContext.STRUTS_VALUESTACK_KEY, proxy.getInvocation().getStack());
// if the ActionMapping says to go straight to a result, do it!
if (mapping.getResult() != null) {
Result result = mapping.getResult();
result.execute(proxy.getInvocation());
} else {
proxy.execute();
}
// If there was a previous value stack then set it back onto the request
if (!nullStack) {
request.setAttribute(ServletActionContext.STRUTS_VALUESTACK_KEY, stack);
}
} catch (ConfigurationException e) {
// WW-2874 Only log error if in devMode
if(devMode) {
String reqStr = request.getRequestURI();
if (request.getQueryString() != null) {
reqStr = reqStr + "?" + request.getQueryString();
}
LOG.error("Could not find action or result\n" + reqStr, e);
}
else {
if (LOG.isWarnEnabled()) {
LOG.warn("Could not find action or result", e);
}
}
sendError(request, response, context, HttpServletResponse.SC_NOT_FOUND, e);//产生错误的模板页面,并返回到客户端
} catch (Exception e) {
sendError(request, response, context, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e);//产生错误的模板页面,并返回到客户端
} finally {
UtilTimerStack.pop(timerKey);
}
}
所以所有的struts2的错误都会走该模板页面.
但是$.ajax捕获错误在servlet能做到。因为
public void serviceAction(HttpServletRequest request, HttpServletResponse response, ServletContext context,
ActionMapping mapping) throws ServletException {
Map<String, Object> extraContext = createContextMap(request, response, mapping, context);
// If there was a previous value stack, then create a new copy and pass it in to be used by the new Action
ValueStack stack = (ValueStack) request.getAttribute(ServletActionContext.STRUTS_VALUESTACK_KEY);
boolean nullStack = stack == null;
if (nullStack) {
ActionContext ctx = ActionContext.getContext();
if (ctx != null) {
stack = ctx.getValueStack();
}
}
if (stack != null) {
extraContext.put(ActionContext.VALUE_STACK, valueStackFactory.createValueStack(stack));
}
String timerKey = "Handling request from Dispatcher";
try {
UtilTimerStack.push(timerKey);
String namespace = mapping.getNamespace();
String name = mapping.getName();
String method = mapping.getMethod();
Configuration config = configurationManager.getConfiguration();
ActionProxy proxy = config.getContainer().getInstance(ActionProxyFactory.class).createActionProxy(
namespace, name, method, extraContext, true, false);
request.setAttribute(ServletActionContext.STRUTS_VALUESTACK_KEY, proxy.getInvocation().getStack());
// if the ActionMapping says to go straight to a result, do it!
if (mapping.getResult() != null) {
Result result = mapping.getResult();
result.execute(proxy.getInvocation());
} else {
proxy.execute();
}
// If there was a previous value stack then set it back onto the request
if (!nullStack) {
request.setAttribute(ServletActionContext.STRUTS_VALUESTACK_KEY, stack);
}
} catch (ConfigurationException e) {
// WW-2874 Only log error if in devMode
if(devMode) {
String reqStr = request.getRequestURI();
if (request.getQueryString() != null) {
reqStr = reqStr + "?" + request.getQueryString();
}
LOG.error("Could not find action or result\n" + reqStr, e);
}
else {
if (LOG.isWarnEnabled()) {
LOG.warn("Could not find action or result", e);
}
}
sendError(request, response, context, HttpServletResponse.SC_NOT_FOUND, e);//产生错误的模板页面,并返回到客户端
} catch (Exception e) {
sendError(request, response, context, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e);//产生错误的模板页面,并返回到客户端
} finally {
UtilTimerStack.pop(timerKey);
}
}
所以所有的struts2的错误都会走该模板页面.
相关文章推荐
- structs与ajax结合的问题
- ajax 结合structs 做的二级联动.在ie7.0和8.0上访问没问题: ----但是在ie6.0上访问出现问题!
- structs2与ajax结合的经验
- asp.net 结合ajax实现数据异步请求的同步执行问题
- asp.net结合ajaxfileupload控件上传文件一直提示undefined问题
- $.post与$.ajax 结合struts问题
- AspNetPager与ObjectDataSource与Gridview和AJAX.NET结合的有关问题
- file文件添加change事件,结合ajaxFileUpload使用文件上传只生效一次问题解决
- asp结合ajax中文乱码问题
- spring mvc的SimpleFormController与AJAX结合遇到的一些问题
- spring mvc的SimpleFormController与AJAX结合遇到的一些问题
- spring mvc的SimpleFormController与AJAX结合遇到的一些问题
- jQuery 联动 与 DOCTYPE W3C<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 结合没ajax效果问题
- [转]结合PHP实例谈谈如何解决jQuery.ajax提交GB2312或GBK编码中文乱码问题
- JS 获取链接(url)参数以及锚链接(anchor)结合富ajax的应用(ajax前进/后退的问题)
- 5.29ajax传值为乱码的问题
- Ajax本地跨域问题 Cross origin requests are only supported for HTTP
- 条件运算符与宏定义的结合问题
- Ajax发送请求导出Excel出现的问题
- 通过ajax访问Tomcat服务器web service接口时出现No 'Access-Control-Allow-Origin' header问题的解决办法