【问题标题】:How to out.print() HTML attributes in JSP?如何在 JSP 中输出.print() HTML 属性?
【发布时间】:2020-05-18 05:46:15
【问题描述】:

所以问题是我想知道我应该如何out.print() 属性,我看到人们这样做:

<%
 out.println("<h1> Hello world </h1>");
%>

但是我该怎么做呢:

<%
 out.println("<h1 align="center"> Hello world </h1>");
%>

【问题讨论】:

  • 像这样尝试out.println("&lt;h1 align='center'&gt; Hello world &lt;/h1&gt;"); 双引号嵌套会破坏您的代码。

标签: java html jsp attributes tags


【解决方案1】:

你也可以像这样使用字符串连接,

 <% out.print("<h1 align='center'>"+"Hello World"+"</h1>"); %>

【讨论】:

    【解决方案2】:

    java中的双引号在字符串值内转义。

    out.println("<h1 align=\"center\"> Hello world </h1>");
    

    【讨论】:

      【解决方案3】:

      你应该始终在字符串中使用双引号作为外部字符串,在内部字符串中使用单引号

           out.println("<h1 align='center'> Hello world </h1>");
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-07-28
        • 1970-01-01
        • 1970-01-01
        • 2010-10-26
        • 2012-01-03
        • 1970-01-01
        相关资源
        最近更新 更多