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

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

本文关键字:添加 情况下 元素 struts styleId 属性 html 接触 一种 简单 方法      更新时间:2023-09-26

我目前正在使用遗留代码,试图让它在较新的浏览器中正常工作。该代码使用Struts1.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主方法来处理这个问题。我使用regex查找尚未具有styleId属性的html元素(selectoptiontexthiddentextarea),然后添加与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();
            }
        }
    }
}
相关文章: