struts2与ajax的整合总结

struts2与ajax的整合
   1.导入jar包
      struts2-json-plugin-2.1.8.1.jsr
	  说明:在该jar包中有sttuts-plugin。xml文件
		<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>

	   在上述配置中可以看到:
	     1.有一个package json-default 有一个自定义的结果集 该结果集处理哪些数据应该返回客户端
    在struts的配置文件中
	  所有的package应该继承 json-default
	  result的类型应该是 type=“json” result没有文本值
	  <!-- struts2 支持json -->
	  <package name="userjson" namespace="/" extends="json-default">
		<action name="userJsonAction_*" method="{1}" class="userAction">
			<result type="json"></result>
		</action>
	  </package>
	
	在action中 所有的get方法 例如 getXxx 将会以这样的形式放回 {xxx:‘aaa’}

	针对struts2和ajax结合 无论 采用$.post() 还是$.ajax()都不能捕获到服务器产生的错误
	但是$.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);
        }
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值