1

JavaからjavascriptshowContent関数に文字列を渡そうとしています。Javaとjavascriptの両方がJSPページに含まれています。文字列strLineには、showContent関数を使用して表示するXMLコンテンツが含まれています。

私のJava

        try{    
        //Open the file that is the first command line parameter
            FileInputStream fstream = new FileInputStream(table.get(xmlMatch));                 

            // Get the object of DataInputStream
            DataInputStream in = new DataInputStream(fstream);
            BufferedReader br = new BufferedReader(new InputStreamReader(in));
            String strLine;

        //Read File Line By Line
            while ((strLine = br.readLine()) != null)   
            {
                out.println (strLine);
            }  

Javascript(別の質問でこれを提供してくれたPeterの功績を認めなければなりません)

<script type="text/javascript" language="JavaScript">
function showContent()
{
document.getElementById('showContent').innerHTML = "printed content";
}
</script>

上記の「印刷物」をとに置き換えてみまし"strLine"; (strLine);("strLine");

また、を使用しstrLineてセッション属性として 設定しようとしましたが、結果が画面にnullで出力されました。session.setAttribute("strLine", strLine);"<%=strLine%>";

これに関するどんな助けも素晴らしいでしょう。

HTML

<a href="#" onclick="showContent()">Next! <%=keywords%> concept </a>
<div id="showContent"></div>
4

2 に答える 2

3

で出力する代わりにout.println、変数 (おそらく StringBuilder) を入れる必要があります。そのためには、次のことを行う必要があります。

適切なスコープで変数を宣言します (おそらく JSP の先頭で)

StringBuilder contentInnerHtml = new StringBuilder();

次に、ファイルのテキストをこの新しい変数に追加します。

while ((strLine = br.readLine()) != null)   
{
    contentInnerHtml.append(strLine);
}

最後に、コードの JavaScript 部分でその値を返します (with toString()):

<script type="text/javascript" language="JavaScript">
function showContent()
{
    document.getElementById('showContent').innerHTML = "<%=contentInnerHtml.toString()%>";
}
</script>
于 2012-10-09T15:36:28.427 に答える
0

HTML が try/catch ブロック内にある場合は、正常に<%=strLine%>動作するはずです。そうでない場合は、セッション属性として割り当てることもできますが、セッションからもアクセスする必要があります。

元:

    try{    
    //Open the file that is the first command line parameter
        FileInputStream fstream = new FileInputStream(table.get(xmlMatch));                 

        // Get the object of DataInputStream
        DataInputStream in = new DataInputStream(fstream);
        BufferedReader br = new BufferedReader(new InputStreamReader(in));
        String strLine;

    //Read File Line By Line
        while ((strLine = br.readLine()) != null)   
        {
%>
<div id="xxx"><%= strLine %></div>
<%
            out.println (strLine);
        }  

しかし、それは信じられないほど醜いコードであり、読み取り/デバッグが困難です。

    try{    
    //Open the file that is the first command line parameter
        FileInputStream fstream = new FileInputStream(table.get(xmlMatch));                 

        // Get the object of DataInputStream
        DataInputStream in = new DataInputStream(fstream);
        BufferedReader br = new BufferedReader(new InputStreamReader(in));
        String strLine;

    //Read File Line By Line
        while ((strLine = br.readLine()) != null)   
        {
           session.setAttribute("strLine", strLine);  
           out.println (strLine);

        }  
  } // end of try
  catch( ... ){}
%>

<div id="xxx"> <%= session.getAttribute( "strLine" ) %></div>

ただし、2 番目のケースでは、ファイルの最後の行しか表示されないため、何を達成しようとしているのか完全にはわかりません。

全文を表示したい場合は、おそらく次を使用できます。

        StringBuffer strLineBuf;

    //Read File Line By Line
        while ((strLine = br.readLine()) != null)   
        {
            strLineBuf.append(strLine).append("<br/>");
        }  
        session.setAttribute( "strLine", strLineBuf.toString() );

そして、try/catch が終了したら、html コードで次のようにします。

  <div id="xxx"><%= session.getAttribute( strLine ) %> </div>
于 2012-10-09T15:39:44.073 に答える