zoukankan      html  css  js  c++  java
  • 解决Struts2.2.20版本的标签不支持style属性的问题

    我先把Exception错误信息贴出来:org.apache.jasper.JasperException: /WEB-INF/jsp/topicAction/addUI.jsp (line: 40, column: 0) Unable to find setter method for attribute: style

    通过Exception定位的源代码是:

    通过提示我们大概能知道是因为找不到style属性的setter方法,可这个标签是struts2封装的HTML原生标签,但style属性在HTML里使用是完全没问题的,由此我想到可能是struts2封装的问题,于是乎找到struts关于UI标签的源码。如下:

    public class AbstractUITagBeanInfo extends SimpleBeanInfo {
        private static final Logger LOG = 
        LoggerFactory.getLogger(AbstractUITagBeanInfo.class);  
        public PropertyDescriptor[] getPropertyDescriptors() {
            try {
                List<PropertyDescriptor> descriptors = new ArrayList<PropertyDescriptor>();
                // Add the tricky one first
                Method setter = AbstractUITag.class.getMethod("setCssClass", String.class);
                descriptors.add(new PropertyDescriptor("class", null, setter));
                descriptors.add(new PropertyDescriptor("cssClass", null, setter));
    
                for (Field field : AbstractUITag.class.getDeclaredFields()) {
                    String fieldName = field.getName();
                    if (!"dynamicAttributes".equals(fieldName)) {
                        String setterName = "set" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);
                        setter = AbstractUITag.class.getMethod(setterName, String.class);
                        descriptors.add(new PropertyDescriptor(fieldName, null, setter));
                    }
                }
    
                PropertyDescriptor[] array = new PropertyDescriptor[descriptors.size()];
                return descriptors.toArray(array);
            } catch (Exception e) {
                // This is crazy talk, we're only doing things that should always succeed
                LOG.fatal("Could not construct bean info for AbstractUITag. This is very bad.", e);
                return null;
            }
        }
        }
     

    发现 AbstractUITagBeanInfo中确实没有对style属性的解析处理代码,于是我去官网下载了更新版本的struts(struts2.3.21),查看源代码 惊喜的发现它解决了这个问题。我把它的这部分代码贴出来:

    descriptors.add(new PropertyDescriptor("class", null, classSetter));      
    
    descriptors.add(newPropertyDescriptor("cssClass", null, classSetter));   
    
    descriptors.add(new PropertyDescriptor("style", null, styleSetter));  //这里是相对于struts2.3.20增加的对style属性的解析
    
    descriptors.add(new PropertyDescriptor("cssStyle", null, styleSetter));   //这里是相对于struts2.3.20增加的对cssStyle属性的解析
     

    那么问题来了,我们要怎么解决这个问题呢,我的做法是在工程中新建一个和AbstractUITagBeanInfo这个类同名同包的文件(包名和类名都要相同),再把struts2.3.21版本中这个类的源代码拷贝过来,这样新的类就会覆盖原来的那个标签处理类。当然你也可以找到这个类所在的jar包删掉再把新版本中的这个jar包加进来。
    贴下我的工程里的图

    注:这个不仅是针对<s:form/>标签,所有有style属性的标签如<s:textfiled/>等都会出现这个问题。

    版权声明:本文为博主原创文章,未经博主允许不得转载。

  • 相关阅读:
    大学生程序猿IT情书“2014爱的告白挑战赛”获奖名单及优秀情书展示系列之
    HDU 4952 Poor Mitsui(贪心)
    linux-CentOS6.4安装Memcached+memcached扩展+安装memcache扩展+Memcache+mecached同步SESSION的几种方法
    POJ 1006 Biorhythms(中国剩余定理 )
    java多线程实验 滚动字
    学习鸟哥的Linux私房菜笔记(17)——Linux引导流程
    PCI的imagework已由freeview软件代替
    小强的HTML5移动开发之路(26)—— JavaScript回顾1
    小强的HTML5移动开发之路(25)—— AppCan开发环境
    小强的HTML5移动开发之路(24)—— PhoneGap Android开发环境搭建
  • 原文地址:https://www.cnblogs.com/ziysong/p/4652938.html
Copyright © 2011-2022 走看看