【问题标题】:How to include HTML in JSP?如何在 JSP 中包含 HTML?
【发布时间】:2020-09-14 23:57:49
【问题描述】:

我已经搜索过这个问题,有一些答案,但不完全是我的问题。所以,这是我的jsp代码:

<body>
    <%
        if (manager.isValid(request.getParameter("username"),request.getParameter("password"))){
            out.print("<h1> Welcome ");
            out.print(request.getParameter("username") + "</h1>");
        } else {
            out.print("<h1> Please Try Again </h1> <br />");
            out.print("Either your username or password is incorrect. Please Try again <br /> <br />");
                        }
    %>
    <%@ include file="LoginForm.html" %>
    

但不是这个,我只想在“else”块中包含“loginForm.html”。 我该怎么做? 当然这不起作用,但你会猜到我想要什么:

<%
        if (manager.isValid(request.getParameter("username"),request.getParameter("password"))){
            out.print("<h1> Welcome ");
            out.print(request.getParameter("username") + "</h1>");
        } else {
            out.print("<h1> Please Try Again </h1> <br />");
            out.print("Either your username or password is incorrect. Please Try again <br /> <br />");
            include file="LoginForm.html" ;
        }
    %>

【问题讨论】:

    标签: java html jsp


    【解决方案1】:

    我想告诉你一个方法,试试下面的方法。您可以尝试使用 else 而不是 if

    <body>
            <%
                int x = 10;
                if(x>10) {
            %>
            <%@include  file="some.html" %>
            <%
                }
            %>
    
    </body>
    

    【讨论】:

      【解决方案2】:

      您还可以在 jsp 文件函数中包含可重用的 html,并在需要时调用它进行打印。例如 config.jsp 包含

      <%!
        public void printMenu()
        {
         %>
         HTML HERE
         <%!
        }
      %>
      

      home.jsp 包含

      <%@include file="config.jsp"%>
      <!DOCTYPE html>
      <html>
        <body>
            <% printMenu(); %>
          <div id="PeopleTableContainer" style="width: 800px;"></div>
      

      【讨论】:

      • 仅供未来的程序员参考,我尝试使用这种方法,它的作用是第一行 将插入任何被替换的内容“这里的 HTML”。那么当 出现,它会像平常一样调用函数。因此,至少在我的经验中,这里需要额外的行为。
      猜你喜欢
      • 2011-12-30
      • 2017-10-25
      • 2016-10-17
      • 1970-01-01
      • 2017-09-30
      • 1970-01-01
      • 2013-11-30
      • 2010-09-15
      • 1970-01-01
      相关资源
      最近更新 更多