我目前正在使用旧代码,试图让它在较新的浏览器中正常工作。该代码是用 Struts 1.3 编写的,并通过以下方式广泛使用了 html 标签库:
<html:text property="myTextInput" maxlength="10"/>
渲染时会生成以下 html:
<input name="myTextInput" type="text" maxlength="10" value="">
在旧版本的 IE 中,可以使用 document.getElementById('myTextInput')
即使元素只有 name
也能获得引用属性并且没有 id
属性。当使用 jsp html 标签时,name
属性生成 name
html 代码中的属性,但不会生成 id
属性。
我发现添加 styleId
到 jsp 中的 html 标记中添加 id
属性到生成的 xml,但这意味着我必须触摸所有 jsp 中的每个 html 标记元素并将其更改为类似于:
<html:text property="myTextInput" styleId="myTextInput" maxlength="10"/>
我还发现了document.getElementByName()
,但这会导致接触大量的 javascript,而且(由于代码错误),我不知道它是否真的引用 id
的元素。或name
所以这可能会导致一些问题。
有没有一种简单的方法可以添加 styleId 属性而不触及每个元素?
最佳答案
我最终编写了一个小的 java main 方法来处理这个问题。我使用正则表达式查找尚不具有 styleId
属性的 html
元素(select
、option
.text
、hidden
、textarea
),然后添加与 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();
}
}
}
}
关于javascript - 有没有一种简单的方法可以添加 struts 1.3 html styleId 属性而不触及每个元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34185304/