2015-12-09 150 views
1

我目前正在使用遺留代碼來試圖讓它在新的瀏覽器中正常工作。該代碼是寫在Struts 1.3和利用了HTML標籤庫的廣泛以下方式:有沒有簡單的方法來添加struts 1.3 html styleId屬性而不觸及每個元素?

<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,但這意味着我將不得不觸及每一個HTML標記元素中的所有JSP的,改變它類似於:

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

我還發現document.getElementByName(),但這會導致接觸了大量的JavaScript和也(由於惡劣的代碼),我不知道它是否真的是指一個元素由idname所以這可能會導致一些問題。

有沒有簡單的方法來添加styleId屬性而不觸及每個元素?

回答

0

我寫了一個小的java主要方法來處理這個問題。我用正則表達式來找到html元素(selectoptiontexthiddentextarea)不已經有一個styleId屬性,然後用相同的值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(); 
      } 
     } 
    } 
} 
相關問題