【问题标题】:Is there an easy way to add the struts 1.3 html styleId attribute without touching every element?有没有一种简单的方法来添加 struts 1.3 html styleId 属性而不接触每个元素?
【发布时间】:2015-12-09 17:34:31
【问题描述】:

我目前正在使用旧代码以尝试使其在较新的浏览器中正常工作。该代码是用 Struts 1.3 编写的,并以下列方式广泛使用 html 标签库:

<html:text property="myTextInput" maxlength="10"/>

渲染时会生成以下 html:

&lt;input name="myTextInput" type="text" maxlength="10" value=""&gt;

在旧版本的 IE 中,即使元素只有name 属性并且没有id 属性,也可以使用document.getElementById('myTextInput') 来获取引用。使用jsp html标签时,name属性在html代码中生成name属性,但不生成id属性。

我发现将styleId 添加到jsp 中的html 标记确实会将id 属性添加到生成的xml,但这意味着我必须触摸所有jsp 中的每个html 标记元素并将其更改为类似于:

<html:text property="myTextInput" styleId="myTextInput" maxlength="10"/>

我还找到了document.getElementByName(),但这会导致涉及到大量的javascript,而且(由于代码错误),我不知道它是否真的指的是idname 的元素所以这可能会导致一些问题。

有没有一种简单的方法可以在不触及每个元素的情况下添加 styleId 属性?

【问题讨论】:

    标签: javascript java jsp struts struts-1


    【解决方案1】:

    我最终编写了一个小的 java main 方法来处理这个问题。我使用正则表达式查找尚不具有styleId 属性的html 元素(selectoptiontexthiddentextarea),然后添加styleId 属性具有与property 属性相同的值。这可以扩展为一次处理一堆文件,但现在我只想对单个文件做一些事情,这样我就可以轻松地对照源代码控制检查它们并确保它正常工作。这是解决问题的快速而肮脏的解决方案,因此我不必手动梳理大量的 jsp 文件,因此我确信它无法处理一些边缘情况。话虽如此:

    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.nio.channels.FileChannel;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    
    
    public class JspModifierStyleId {
    
        public static void main(String[] args) throws IOException {
            String lineEnding = "\r\n";
            String baseDir= "C:/path/to/your/directory/";   //Change this to suit your directory
    
            String origFileName= "OriginalFile.jsp";  //Change this to suit your original file that needs the attribute added
            File origFile = new File(baseDir + origFileName);
    
            String tempFileName = "TemporaryFile.jsp";
            File tempFile = new File(baseDir + tempFileName);
    
            Pattern p = Pattern.compile("^(?!.*styleId)\\s*<html:(?:select|option|text|hidden|textarea)\\s.*property=\"([a-zA-Z1-9.]*)\".+");
    
            FileReader in = new FileReader(origFile);
            FileWriter out = new FileWriter(tempFile);
    
            BufferedReader br = new BufferedReader(in);
            BufferedWriter bw = new BufferedWriter(out);
    
    
            String line;
            while ((line = br.readLine()) != null) {
                Matcher m = p.matcher(line);
                if(m.matches()){
                    String strWithStyleId = line.substring(0, m.start(1)) + m.group(1) + "\" styleId=\"" + line.substring(m.start(1));
                    bw.write(strWithStyleId + lineEnding);
                    System.out.println(strWithStyleId);
                }else {
                    bw.write(line + lineEnding);
                }
            }
    
            br.close();
            bw.close();
    
            //copies back to original file, BE CAREFUL!!! 
            copyFile(tempFile, origFile);
        }
    
        public static void copyFile(File sourceFile, File destFile) throws IOException {
            if(!destFile.exists()) {
                destFile.createNewFile();
            }
    
            FileChannel source = null;
            FileChannel destination = null;
    
            try {
                source = new FileInputStream(sourceFile).getChannel();
                destination = new FileOutputStream(destFile).getChannel();
                destination.transferFrom(source, 0, source.size());
            }
            finally {
                if(source != null) {
                    source.close();
                }
                if(destination != null) {
                    destination.close();
                }
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2015-08-04
      • 1970-01-01
      • 2011-04-10
      • 2011-06-19
      • 1970-01-01
      • 1970-01-01
      • 2017-11-13
      • 2013-06-01
      • 1970-01-01
      相关资源
      最近更新 更多