[英]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.