简体   繁体   English

如何将Java String传递给JSP页面中的JavaScript函数

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

I am looking to pass a string from my Java to a javascript showContent function. 我希望将我的Java中的字符串传递给javascript showContent函数。 Both the Java and javascript are contained in a JSP page. Java和javascript都包含在JSP页面中。 The string strLine contains XML content I want to display using the showContent function. 字符串strLine包含我想使用showContent函数显示的XML内容。

My Java 我的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);
            }  

The Javascript (I have to credit Peter for supplying me with this in another question ) Javascript(我必须相信彼得在另一个问题上向我提供此内容)

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

I have tried replacing the above "printed content" with "strLine"; 我试过用"strLine";替换上面的“印刷内容"strLine"; (strLine); and ("strLine"); ("strLine");

I also tried set strLine as a session attribute using session.setAttribute("strLine", strLine); 我还尝试使用session.setAttribute("strLine", strLine);strLine设置为会话属性session.setAttribute("strLine", strLine); and using "<%=strLine%>"; 并使用"<%=strLine%>"; but the result was null printed to the screen. 但结果是空打印到屏幕上。

Any help with this would be great. 对此的任何帮助都会很棒。

The HTML HTML

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

Instead of printing it with out.println , you should put in a variable (a StringBuilder maybe). 而不是使用out.println打印它,你应该放入一个变量(也许是一个StringBuilder)。 In order to do that you have to: 为了做到这一点,你必须:

Declare the variable at the right scope (maybe at the beginning of the JSP) 在适当的范围内声明变量(可能在JSP的开头)

StringBuilder contentInnerHtml = new StringBuilder();

Then append the text of the file to this new variable: 然后将文件的文本附加到此新变量:

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

Finally, in the javascript part of the code return its value (with toString() ): 最后,在代码的javascript部分返回其值(使用toString() ):

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

If your html is within your try/catch block, <%=strLine%> should work fine. 如果您的html在try / catch块中, <%=strLine%>应该可以正常工作。 If not, then assigning it as a session attribute would work as well, however, you need to access it from the session as well: 如果没有,那么将其指定为会话属性也会起作用,但是,您还需要从会话中访问它:

ex: 例如:

    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);
        }  

But that is incredibly ugly code and difficult to read/debug. 但这是令人难以置信的丑陋代码,难以阅读/调试。

    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>

However, the in the second case, you will only be displaying the last line of your file, so I am not entirely sure what you are trying to accomplish. 但是,在第二种情况下,您将只显示文件的最后一行,因此我不完全确定您要完成的任务。

If you are hoping to display the full text, perhaps you can use: 如果您希望显示全文,也许您可​​以使用:

        StringBuffer strLineBuf;

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

And then after your try/catch finishes, in your html code: 然后在你的try / catch完成之后,在你的html代码中:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何通过jsp表达式标签将java字符串变量传递给javascript函数? - How to pass java string variable to the javascript function by jsp expression tag? 如何将jsp页面中的javascript变量传递给另一个java文件? - How to pass javascript variable in jsp page to another java file? 如何将Java变量传递到包含JavaScript的其他JSP页面? - How to pass a java variable to a different jsp page containing javascript? 如何获取要传递给jsp中java字符串的javascript变量值 - how to get the variable value of javascript to be pass to the string of java in jsp 如何在JSP中将Java方法返回值传递给JavaScript函数? - How to pass java method return value to JavaScript function in JSP? 想在jsp中将java值传递给javascript函数 - Want to pass the java value into javascript function in jsp 如何将java对象传递给jsp页面 - How to pass a java object to jsp page 如何在JSP中将JavaScript变量传递给Java Scriplet - How to pass javascript variable to java scriplet in jsp 如何将jsp页面中显示的列表值传递到Java中的javascript方法中 - How do I pass list values displayed in jsp page into javascript method in java Java,JSP-如何通过会话集属性将描述HTML标记的字符串传递给页面 - Java, JSP - How to pass a String describing an HTML Tag via session set attribute into page
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM