簡體   English   中英

如何將Java String傳遞給JSP頁面中的JavaScript函數

[英]How to pass a Java String to a JavaScript function in a JSP page

我希望將我的Java中的字符串傳遞給javascript showContent函數。 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(我必須相信彼得在另一個問題上向我提供此內容)

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

我試過用"strLine";替換上面的“印刷內容"strLine"; (strLine); ("strLine");

我還嘗試使用session.setAttribute("strLine", strLine);strLine設置為會話屬性session.setAttribute("strLine", strLine); 並使用"<%=strLine%>"; 但結果是空打印到屏幕上。

對此的任何幫助都會很棒。

HTML

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

而不是使用out.println打印它,你應該放入一個變量(也許是一個StringBuilder)。 為了做到這一點,你必須:

在適當的范圍內聲明變量(可能在JSP的開頭)

StringBuilder contentInnerHtml = new StringBuilder();

然后將文件的文本附加到此新變量:

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

最后,在代碼的javascript部分返回其值(使用toString() ):

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

如果您的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>

但是,在第二種情況下,您將只顯示文件的最后一行,因此我不完全確定您要完成的任務。

如果您希望顯示全文,也許您可​​以使用:

        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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM