struts2 ajax exception

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.StrutsStatics;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

public class AjaxExceptionInteceptor extends AbstractInterceptor {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	public String intercept(ActionInvocation invocation) throws Exception {
		// String result;
		// Object action = invocation.getAction();
		// try {
		// result = invocation.invoke();
		// } catch (Exception e) {
		// ActionContext actionContext = invocation.getInvocationContext();
		// HttpServletResponse response = (HttpServletResponse) actionContext
		// .get(StrutsStatics.HTTP_RESPONSE);
		// response.setStatus(500);
		// if (action instanceof EditPurInvoice) {
		// ((EditPurInvoice) action).setResult(e.getMessage());
		// }
		// result = PARAM.AJAX_RESPONSE_CODE;
		// }
		String result = "";
		// Object action = invocation.getAction();
		ActionContext actionContext = invocation.getInvocationContext();
		HttpServletResponse response = (HttpServletResponse) actionContext
				.get(StrutsStatics.HTTP_RESPONSE);

		try {
			result = invocation.invoke();
			if (result.equals(Action.ERROR)) {
				response.setStatus(403);
			}
		} catch (Exception e) {
			response.setStatus(403);
			result = Action.ERROR;

		}
		return result;

	}
}


<global-results>
			<result name="input" type="json">
				<param name="ignoreHierarchy">false</param>
				<param name="includeProperties">actionErrors\[\d+\], fieldErrors\..+$,
					actionMessages\[\d+\]</param>
				<param name="contentType">text/html</param>
			</result>
			<result name="error" type="json">
				<param name="ignoreHierarchy">false</param>
				<param name="includeProperties">actionErrors\[\d+\], fieldErrors\..+$,
					actionMessages\[\d+\]</param>
				<param name="contentType">text/html</param>
			</result>
			<result name="Exception" type="json">
				<param name="ignoreHierarchy">false</param>
				<param name="includeProperties">actionErrors\[\d+\], fieldErrors\..+$,
					actionMessages\[\d+\]</param>
				<param name="contentType">text/html</param>
			</result>
		</global-results>
		<global-exception-mappings>
			<exception-mapping exception="java.lang.Throwable"
				result="Exception" />
		</global-exception-mappings>


<action name="searchUserJson" class="searchUserAction">
			<interceptor-ref name="defaultStack" />
			<interceptor-ref name="ajaxException" />
			<result type="json" />
		</action>

你可能感兴趣的:(exception)