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

Is there an easy way to add the struts 1.3 html styleId attribute without touching every element?

我目前正在处理遗留代码,试图让它在较新的浏览器中正常工作。代码是用 Struts 1.3 编写的,并通过以下方式广泛使用 html 标签库:

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

渲染时会产生以下 html:

<input name="myTextInput" type="text" maxlength="10" value="">

在旧版本的 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 属性而无需触及每个元素?

我最后写了一个小的 java 主要方法来处理这个问题。我使用正则表达式查找 html 元素(selectoptiontexthiddentextareastyleId 属性,然后添加与 property 属性具有相同值的 styleId 属性。这可以扩展为一次处理一堆文件,但现在我只想处理单个文件,这样我就可以轻松地根据源代码控制检查它们并确保它正常工作。这是解决问题的快速而肮脏的解决方案,因此我不必手动梳理大量 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();
            }
        }
    }
}