Java 如何在 JSP 中包含 HTML?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16376227/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to include HTML in JSP?
提问by Giorgi Margiani
i've searched for this question, there are some answers, but not exactly as my question. so here is my jsp code:
我已经搜索过这个问题,有一些答案,但并不完全是我的问题。所以这是我的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" %>
but instead of this, i want to include "loginForm.html" only in "else" block. how can i do it? of course this doesn't work, but you'll guess what i want :
但不是这个,我想只在“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" ;
}
%>
采纳答案by Vinay
I would like to show you a way, try like below. You can try with else instead with if
.
我想向您展示一种方法,尝试如下。您可以尝试使用 else 代替if
.
<body>
<%
int x = 10;
if(x>10) {
%>
<%@include file="some.html" %>
<%
}
%>
</body>
回答by Howard J
You could also include reusable html in a jsp file function and call it for printing where needed. e.g. config.jsp contains
您还可以在 jsp 文件函数中包含可重用的 html,并在需要时调用它进行打印。例如 config.jsp 包含
<%!
public void printMenu()
{
%>
HTML HERE
<%!
}
%>
home.jsp contains
home.jsp 包含
<%@include file="config.jsp"%>
<!DOCTYPE html>
<html>
<body>
<% printMenu(); %>
<div id="PeopleTableContainer" style="width: 800px;"></div>