English 中文(简体)
• 如何在 Java15号案文栏目中增加变量? [复制]
原标题:How can i add variables inside Java 15 text block feature? [duplicate]

刚刚在 Java15年,即“TEXT BLOCKS”出现新的特征。 我可以假设,可以通过与“+”操作者协作,在案文栏内添加一个变量:

String html = """
          <html>
              <body>
                  <p>Hello, """+strA+"""</p>
              </body>
          </html>
          """;

但是,它们是否提供了任何途径,以便我们能够增加各种变量,使其他许多语文能够普遍使用,具体如下:

String html = """
          <html>
              <body>
                  <p>Hello, ${strA}</p>
              </body>
          </html>
          """;

This question might sound silly but it may be useful in certain scenario.

最佳回答

https://openjdk.java.net/jeps/355“rel=“nofollow noreferer” 段 次 页 次

Text blocks do not directly support string interpolation. Interpolation may be considered in a future JEP.

“结构污染”的含义

evaluating a string literal containing one or more placeholders, yielding a result in which the placeholders are replaced with their corresponding values

from Wikipedia


如上文所述,也许我们今后会失败。 虽然很难说他们可以如何执行,而不打破落后的兼容性——例如,如果我扼制了<条码>{><>>/代码”的话,情况如何? Java语设计师很少添加可能打破落后兼容性的任何东西。

在我看来,他们会更好,要么立即支持,要么永远不支持。

也许有可能出现一种新的案文。 不能使用<条码>,而是使用<条码>/代码>表示某一参数的正文体。

问题回答

Java 15并不支持直接在案文栏目内进行污染,也不支持直面字面的明晰度。

Java15的解决办法是使用。 方法:

String html = """
      <html>
          <body>
              <p>Hello, %s</p>
          </body>
      </html>
      """.formatted(strA);

正如用户@Michael提到的那样:他们(正在执行JEP 368)的蒸汽项目Amer没有为干涉案文栏中的扼杀提供任何途径。

Note that I somewhat doubt it ll ever happen. For starters, there is the backwards compatibility issue; any such attempt to introduce interpolation requires some marker so that any existing text blocks aren t all of a sudden going to change in what it means depending on which version of javac to invoke.

But more to the point, you yourself, asking the question, can t even come up with a valid example, which is perhaps indicative that this feature is less useful than it sounds. It looks like you came up with a valid use case, but that s not actually true: If what you wrote would compile and work, then you just wrote a webapp with a rather serious XSS security leak in it!

问题在于,你真的想要的是节奏,在引导声音真正简单的同时(对这一表述进行公正的评估,然后将结果推到我打上表达的扼杀性权利,请!) ——这只是一纸空文。 资本是造成这种情况的主要原因。 但是,在案文组中,你可以笼统适用<代码>${strA}的规则: Evaluate expression strA,then RUS RUS, ,后将其置于以下两个原因: 谁说,你把事情推向外是超文本,而不是说是JSON或ToML或CSV,或者说什么不是,谁说,我想要的干涉首先需要放弃吗? 如果我想主动注入<代码><em>,或者说不了,那么我不想将这一条变成<代码>&lt;em&gt;?

要么我们更新地毯,以适应所有这些情况,现在我们又重新发明一个完整的排球系统,把这个系统推入似乎更适合专用图书馆的工作的地毯,要么我们不喜欢,而这个特征似乎非常有用,但事实上是独特的: 你们要么很少使用,要么在你的法典基础上都有安全和其他ug——任何引起虐待的兰经特征,我希望大家会同意我的意见——这不是一个大事。

确实,许多语言都这样,但是,现在决定 j语特征的民间人士似乎正在认识到这种特征,并从中汲取教训,但仅仅因为所有其他语言都有这些语言,就赢得了 j语的附加特征——一些思想和使用案例总是首先得到考虑,而对 literal字面上的干涉进行的任何分析都可能导致:“Eh,可能不是对语言的有益补充”。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签