繁体   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