整合:ajax
導入jar包apache
sturts2-json-plugin-2.1.8.1.jarjson
說明:服務器
在該jar包中有struts-plugin.xml文件app
<struts>
<package name="json-default" extends="struts-default">
<result-types>
<result-type name="json" class="org.apache.struts2.json.JSONResult"/>
</result-types>
<interceptors>
<interceptor name="json" class="org.apache.struts2.json.JSONInterceptor"/>
</interceptors>
</package>
</struts>post
從上述配置中能夠看到
* 有一個package"json-default",有一個自定義的結果集,該結果集處理哪些數據應該返回客戶端
* 在struts的配置文件中:
全部的package應該繼承json-default,result的類型應該是"json",result沒有文本值
* 在action中,全部的get方法,例如
getXxx 將會已這樣的形式返回{xxx:'aaa'}
* 針對struts2與ajax結合,不管採用$.post仍是$.ajax都捕獲不到服務器產生的錯誤,這點struts2內部設計的不是很好
可是$.ajax捕獲錯誤在servlet能作到。由於
因此全部的struts2的錯誤都會走該模板頁面
* 若是在action中,一個get方法返回的不是數據,並且是一個對象,好比返回一個service
這個時候只須要在該方法上加@JSON(serialize=false)就能夠忽略該方法spa
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);
}
}
設計