有没有一种简单的方法可以在不接触每个元素的情况下添加struts 1.3 html styleId属性
Is there an easy way to add the struts 1.3 html styleId attribute without touching every element?
我目前正在使用遗留代码,试图让它在较新的浏览器中正常工作。该代码使用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,而且(由于代码不好),我不知道它是否真的引用了id
或name
的元素,所以这可能会导致一些问题。
有没有一种简单的方法可以在不接触每个元素的情况下添加styleId属性?
我最终编写了一个小的java主方法来处理这个问题。我使用regex查找尚未具有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();
}
}
}
}
- 如何在不使用ajax的情况下将pair值添加到数组并发送到django中的视图
- 在不刷新整个网格的情况下,向Asp.net网格视图添加新行
- 在不添加树节点的情况下从网格拖放到树
- 在不使用循环的情况下,从一个数据库字符串值向javascript数组添加多个对象
- 在不向上滚动的情况下向列表框中添加项目
- 如何在结果中添加下划线但不添加虚假值的情况下进行映射
- 为什么在这种情况下要向JQuery添加两个链接
- AngularJs,在没有隔离的情况下将指令属性添加到作用域
- 在这种情况下,如何将字符串添加到函数中 php
- 在 Javascript 不起作用的情况下为数字添加逗号
- 是否可以在不将其添加到DOM的情况下预加载和缓存视频文件
- 在不使用jQuery的情况下解析json,并在HTML代码中添加值
- 如何在不完成添加序列的情况下从rxjs创建数组
- 如何在不覆盖的情况下添加同一JPanel类的不同实例
- 如何添加$scope$打开以在没有控制器的情况下查看
- 谷歌应用程序脚本:如何在不使用getComment()方法的情况下将第二条注释添加到第一条注释中
- 如何在不向现有JavaScript函数添加代码的情况下向该函数添加回调函数
- 有没有一种方法可以在没有jQuery的情况下获得JavaScript中当前添加到后台的事件列表
- 在不使用200行var的情况下,在结果字段中添加顺序命名的变量?[PDF表格]
- 如何在表空或在jquery数据表中找不到匹配项的情况下添加自定义警告消息