简体   繁体   English

从.jsp到html页面的变量

[英]variable from .jsp to the html page

I have a .jsp page that passes the variable from the servlet. 我有一个.jsp页面,它从servlet传递变量。 Lets assume that I have a .jsp page like 让我们假设我有一个.jsp页面

...
<body>
${Variable}
</body>
...

and lets also assume that variable = "oh my god" . 并且我们也假设variable = "oh my god" This is OK. 还行吧。 But how I can put this variable to the .html page that the browser will show the value of the variable? 但是我怎么能把这个变量放到.html页面上,浏览器会显示变量的值?

You need to do this: 你需要这样做:

<%= Variable %>

The resulting HTML will be: 生成的HTML将是:

<body>
oh my god
</body>

Actually currently best voted answer and solution posted there ( <%= Variable %> ) acts exactly the same as code that you provided in the question ( ${Variable} ). 实际上,当前最佳投票答案和解决方案( <%= Variable %> )与您在问题中提供的代码( ${Variable} )完全相同。 The only difference is that yours is the one that should be used , because it's more readable and it's not any damn scriptlet! 唯一的区别是你的是应该使用的那个 ,因为它更具可读性,而且它不是任何该死的小脚本!

For my point of view the, if you want to your JSP variable in play html page, you will need javascript to retrieve that variable out of html rendered by jsp, and you it in the actual newPage.html . 对于我的观点来说,如果你想在播放html页面中使用你的JSP变量,你将需要javascript从jsp渲染的html中检索该变量,并将它放在实际的newPage.html You could put hidden iframe into that newPage.html , embed there jsp page as source of that iframe, and just parse its html with eg getElementById() 您可以将隐藏的iframe放入newPage.html ,将jsp页面嵌入到该iframe的源代码中,并使用例如getElementById()解析其html

There are two options, either use scriptlets or expression language , i would suggest go with expression language . 有两个选项,使用scriptletsexpression language ,我建议使用expression language

Good Read on why Scriptlets are Bad 好好了解为什么Scriptlet是坏的

before accessing variable inside html you need to initialize the variable and then do whatever the calculations and other modifications inside another JSP code block. 在访问html内部的变量之前,您需要初始化变量,然后在另一个JSP代码块中执行任何计算和其他修改。 Now you can access the variable inside the html. 现在您可以访问html中的变量。 This is my first answer for the Stackoverflow.com please experts notify the mistakes i've done. 这是我对Stackoverflow.com的第一个答案,请专家通知我已经完成的错误。

<body>
<% java.lang.Integer var=0; %>
<%
  int a;
  int b;
  var=a+b;
%>
<% out.print(var);%>
</body>

Although this question is old, I think, it's still actual, so I'll try to contribute from my side. 虽然这个问题很老,但我认为,这仍然是实际问题,所以我会尽力为自己做出贡献。
Question is quite simple and I think most of the answers are just answering different question - hence - providing a bit of confusion. 问题很简单,我认为大多数答案只是回答不同的问题 - 因此 - 提供一些混乱。
As far as I understand, that question is: 据我了解,这个问题是:

Can I have a dynamic variable of JSP (which is btw, an element of Expression Lnaguage ) in html, in the same way as I have it in jsp? 我可以在html中使用JSP的动态变量(这是btw, Expression Lnaguage的一个元素),就像我在jsp中一样吗?

And the answer is No . 答案是否定的

JSP translates for Java Server Pages , and that's the point here, that the dynamic value is generated and being provided to jsp on server side. JSP转换为Java Server Pages ,这就是生成动态值并将其提供给服务器端的jsp的重点。 You can't make your html dynamic. 你不能使你的HTML动态。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM