struts2里面的自动类型转换是那个类来处理的啊?

yelang2009 2009-08-24

上面是我在我的一个小的项目里面截的图,在源码里找不到如 struts2日期转换之类的类 或者接口,

因为我在用的时候,从页面传到后台的是这样的样式 2009-01-12 但从后台input转到前台自动填充时 格式改了2009-1-12  我不想自己重新设计转换器,用它的代码改一下就可以了,谁知道这个默认的转换类在那里啊!!!

 

还有:谁有ognl的文档 或者 别的教程,发给我一份,非常感谢!

 

paipaitjz 2009-08-26
org.apache.struts2.interceptor.StrutsConversionErrorInterceptor
通过这个拦截器来对类型进行转换的。
yelang2009 2009-08-26

[quote="paipaitjz"]org.apache.struts2.interceptor.StrutsConversionErrorInterceptor 通过这个拦截器来对类型进行转换的。[/quote]

 

 

这个类只是添加了转换后的错误信息,具体怎么转换的不是这个,比如说 它怎么把字符 “2009-08-26” 转成Date的,怎样把“12” 转成 int 的 12 我想得到具体实现,我还找找看,这二天只等答案了,自己没有找了,呵呵!

下面是你提供的类,,参考

 

 protected Object getOverrideExpr(ActionInvocation invocation, Object value) {
        ValueStack stack = invocation.getStack();

        try {
            stack.push(value);

            return "'" + stack.findValue("top", String.class) + "'";
        } finally {
            stack.pop();
        }
    }

 public String intercept(ActionInvocation invocation) throws Exception {

        ActionContext invocationContext = invocation.getInvocationContext();
        Map conversionErrors = invocationContext.getConversionErrors();
        ValueStack stack = invocationContext.getValueStack();

        HashMap fakie = null;

        for (Iterator iterator = conversionErrors.entrySet().iterator();
             iterator.hasNext();) {
            Map.Entry entry = (Map.Entry) iterator.next();
            String propertyName = (String) entry.getKey();
            Object value = entry.getValue();

            if (shouldAddError(propertyName, value)) {
                String message = XWorkConverter.getConversionErrorMessage(propertyName, stack);

                Object action = invocation.getAction();
                if (action instanceof ValidationAware) {
                    ValidationAware va = (ValidationAware) action;
                    va.addFieldError(propertyName, message);
                }

                if (fakie == null) {
                    fakie = new HashMap();
                }

                fakie.put(propertyName, getOverrideExpr(invocation, value));
            }
        }

        if (fakie != null) {
            // if there were some errors, put the original (fake) values in place right before the result
            stack.getContext().put(ORIGINAL_PROPERTY_OVERRIDE, fakie);
            invocation.addPreResultListener(new PreResultListener() {
                public void beforeResult(ActionInvocation invocation, String resultCode) {
                    Map fakie = (Map) invocation.getInvocationContext().get(ORIGINAL_PROPERTY_OVERRIDE);

                    if (fakie != null) {
                        invocation.getStack().setExprOverrides(fakie);
                    }
                }
            });
        }
        return invocation.invoke();
    }


protected boolean shouldAddError(String propertyName, Object value) {
        if (value == null) {
            return false;
        }

        if ("".equals(value)) {
            return false;
        }

        if (value instanceof String[]) {
            String[] array = (String[]) value;

            if (array.length == 0) {
                return false;
            }

            if (array.length > 1) {
                return true;
            }

            String str = array[0];

            if ("".equals(str)) {
                return false;
            }
        }

        return true;
    }


 

Global site tag (gtag.js) - Google Analytics