为什么不能更改Struts2的valueStack? [英] Why can't I change the valueStack of Struts2?

查看:123
本文介绍了为什么不能更改Struts2的valueStack?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在拦截器中对Struts2的值堆栈进行一些操作,代码如下:

I do some operation of Struts2's value stack in an interceptor,code like this:

public String intercept(ActionInvocation actionInvocation) throws Exception {
    String invokeRes = actionInvocation.invoke();
    ValueStack valueStack = actionInvocation.getStack();

    List<Object> shouldCheckFieldValues = Lists.newArrayList();

    List<String> keywords = Lists.newArrayList("哈哈", "头部", "测试");
    RegexKeywordFilter filter = new RegexKeywordFilter();
    filter.add(keywords);
    filter.compile();

    final ReplaceStrategy highLightStrategy = new ReplaceStrategy() {
        @Override
        public String replaceWith(String keyword) {
            return "<span style='background-color:red;'>" + keyword + "</span>";
        }
    };

    Object respObj = valueStack.findValue("resp");

    if (respObj instanceof JoyPageWebDTO) {
        JoyPageWebDTO pageWebDTO = (JoyPageWebDTO) respObj;
        List<?> recordsList = pageWebDTO.getRecords();
        if (CollectionUtils.isNotEmpty(recordsList)) {
            for (Object audit : recordsList) {
                for (Field field : audit.getClass().getDeclaredFields()) {
                    if (field.isAnnotationPresent(AICheck.class)) {
                        boolean fieldIsAccess = field.isAccessible();
                        if (!fieldIsAccess) field.setAccessible(true);

                        Object fieldValue = field.get(audit);
                        if (fieldValue instanceof String && filter.hasKeywords((String) fieldValue)) {
                            String replacedStr = filter.replace((String) fieldValue, highLightStrategy);
                            field.set(audit, replacedStr);
                        }
                        if (!fieldIsAccess) field.setAccessible(false);
                    }
                }
            }
        }
    }
    return invokeRes;
}

拦截之前的值栈是:

[ http://i.stack.imgur.com/SHqqD.png]

拦截后的值堆栈为:

[ http://i.stack.imgur.com/Ths7m.png]

值堆栈已更改.但是,真正的返回结果是:

It's seamed the valuestack has changed.However,the really return result is:

{
"pageIndex": 0,
"pageSize": 30,
"recordCount": 0,
"records": [
 {
  "auditContent": "",
  "auditID": 354,
  "auditStatus": 3,
  "bizStatus": 1,
  "bodyPart": "2",
  "categoryID": 141,
  "city": "上海",
  "desc": "22",
  "duration": 2,
  "forbidden": "2",
  "indications": "2",
  "name": "头部按摩很爽很爽"
 }
]
}

我使用xml config,代码如下:

I use xml config,code like this:

<package name="ajax" namespace="/ajax" extends="json-default">
    <interceptors>
        <interceptor name="aiCheck"
                     class="com.dianping.joy.admin.web.interceptor.AICheckInterceptor">
        </interceptor>
        <interceptor-stack name="defaultInterceptorStack">
            <interceptor-ref name="aiCheck" />
            <interceptor-ref name="defaultStack" />
        </interceptor-stack>
    </interceptors>
    <default-interceptor-ref name="defaultInterceptorStack"/>

    <action name="searchJoySpu" class="com.action.SpuSearchAction">
        <result type="json">
            <param name="root">resp</param>
        </result>
    </action>
 </package>

返回结果不变.为什么以及如何更改才能得到更改的结果?

The return result is not changed. Why and how can I change this get the changed result?

推荐答案

您正在对对象进行序列化后对其进行修改,因此JSON包含旧值.

You are modifying the object after it has been serialized, so JSON contains the old value.

在执行json结果之前,可以使用拦截器中的PreResultListener处理数据.

You can use PreResultListener in the interceptor to handle data before json result is executed.

PreResultListener

A PreResultListener可能会影响 拦截器/动作阶段和结果阶段.典型用途包括 切换到其他Result或以某种方式修改ResultAction对象在之前执行.

PreResultListener

A PreResultListener can affect an action invocation between the interceptor/action phase and the result phase. Typical uses include switching to a different Result or somehow modifying the Result or Action objects before the Result executes.

public class MyInterceptor extends AbstractInterceptor {
   ...
    public String intercept(ActionInvocation invocation) throws Exception {
       invocation.addPreResultListener(new PreResultListener() {
            public void beforeResult(ActionInvocation invocation, 
                                     String resultCode) {
                // perform operation necessary before Result execution
            }
       });
    }
   ...
}

这篇关于为什么不能更改Struts2的valueStack?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆