整合:

导入jar包

sturts2-json-plugin-2.1.8.1.jar

说明:

在该jar包中有struts-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>

从上述配置中可以看到
    *  有一个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)就可以忽略该方法

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的整合的更多相关文章

  1. Struts2与ajax整合之缺点

    之前有篇博客介绍了Struts2与ajax的整合,链接Struts2之-集成Json插件实现Ajax 这里不再累述,看以上博客. 此篇博客想吐槽一下Struts2的缺点--错误处理做的不好,怎么做的不 ...

  2. Struts2之ajax初析

    Web2.0的随波逐流,Ajax那是大放异彩,Struts2框架自己整合了对Ajax的原生支持(struts 2.1.7+,之前的版本可以通过插件实现),框架的整合只是使得JSON的创建变得异常简单, ...

  3. 使用Maven搭建Struts2+Spring3+Hibernate4的整合开发环境

    做了三年多的JavaEE开发了,在平时的JavaEE开发中,为了能够用最快的速度开发项目,一般都会选择使用Struts2,SpringMVC,Spring,Hibernate,MyBatis这些开源框 ...

  4. struts2+hibernate-jpa+Spring+maven 整合(1)

    1.0.0 struts2 与 spring 的整合. 1.1.0 新建maven工程 , 编写pom.xml ,这里只需要简单的添加 一个组件就够了: 在myeclipse 生成的pom.xml 添 ...

  5. Struts2结合Ajax实现登录

    前言:Struts2作为一款优秀的MVC框架,和Ajax结合在一起,用户就会有良好的体验,本篇博文我们来模拟一个简单的登录操作,实现Ajax的异步请求,其中Struts2进行的是链接处理,Action ...

  6. struts2 + spring + mybatis 框架整合详细介绍

    struts2 + spring + mybatis  框架整合详细介绍 参考地址: https://blog.csdn.net/qq_22028771/article/details/5149898 ...

  7. Struts2+Hibernate4+Spring4框架整合搭建Java项目原型

    收藏 http://www.cnblogs.com/mageguoshi/p/5850956.html Struts2+Hibernate4+Spring4框架整合搭建Java项目原型

  8. Struts2 利用AJAX 导出大数据设置遮罩层

    Struts2 利用AJAX 导出大数据设置遮罩层 需求背景: 每次我们导出excel的时候 ,如果数据量很大,导出花费的时间会很长,页面却有没人任何反应,这个时候用户会认为系统有问题,要么关了页面, ...

  9. Struts2对AJAX的支持

    一.简介        struts2确实一个非常棒的MVC框架.这里部分记述一下struts2对AJAX的支持.实现AJAX有两种方式,一种是使用原生的javascript代码实现,一种是使用第三方 ...

随机推荐

  1. vux 获取后台数据

    1. 首先,导入api 的组件 import api from '../vuex/action' 2.定义数据获取方法 created () { this.fetchData() }, 3.设置方法内 ...

  2. Yii 动作过滤的方法

    protected function _init() { } public function beforeAction($action) { //黑名单 $blackList = array('tes ...

  3. 将speedbutton放在toolbar上,flat设为true,并将speedbutton的width和height设得比较大,在speedbutton中间会出现一条竖线,如何消去?

    把toolbar的flat设为false就没有竖线了

  4. Memcache之内存分配机制

    可参见:http://blog.csdn.net/hguisu/article/details/7353482

  5. PHP上传文件示例

    虽然大多数人认为Web只包含网页,但HTTP协议实际上可以传输任何文件,如office文档.PDF.可执行文件.AVI.压缩文件及各种其他文件类型.虽然FTP在历史上一直是向服务器上传文件的标准方式, ...

  6. thinkphp 3.23语言包加载

    模块home: 1.config 里添加 配置 //'配置项'=>'配置值'    'LANG_SWITCH_ON'   => true,      // 开启语言包功能    'LANG ...

  7. LeetCode——Single Number II(找出数组中只出现一次的数2)

    问题: Given an array of integers, every element appears three times except for one. Find that single o ...

  8. Appirater -- app中提示用户为app评价的提示框

    Appirater是一段你可以嵌入自己工程中的代码,在用户使用应用一段时间后会自动弹出提示用户进行评分. 使用Appirater方面,你可以简单把源代码嵌入你的app工程中,并把以下代码添加至它的委托 ...

  9. JavaScript老梗之this对象

    Js中的this关键词貌似是初学者必经的坑 都不例外 我们经常听到其他人说 this对象谁调用指向谁 的确这是最容易理解的 但是我们可以更加深入的去探索下 加深印象以便更加灵活的适用它 这里不得不提下 ...

  10. Java知识结构