【问题标题】:How should I include other jsp pages in a jsp page correctly?我应该如何正确地在 jsp 页面中包含其他 jsp 页面?
【发布时间】:2012-10-19 13:36:55
【问题描述】:

我现在正在尝试用jsp做一个小网站,就像大多数索引页面一样,我网站的索引页面将包括一些部分:顶部(包含徽标和菜单),主要部分,底部部分。为了避免过多的html标签填充到索引页面中,也许包含页面会是一个好主意。

经过搜索,我知道包含jsp页面有两种方法:使用<%@ include file=""%><jsp:include page="">,我知道它们之间存在一些差异,但我仍然遇到包含页面的一些问题。

如果我有index.jsptop.jsp,我想在index.jsp 中包含top.jsp

index.jsp 是这样的:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>title</title>
<style type="text/css">
  body {background-color:black;margin:0px;padding:0px;}
  #left,#right {width:10%;margin:0px;padding:0px;}
  #left,#center,#right {float:left;}
  #center {width:80%;}
  #top {height:150px;}
  #main {height:600px;background-color:white;}
</style>
</head>

<body>
<div id="left">&nbsp;</div>
<div id="center">
  <div id="top"><jsp:include page="top.jsp"/></div>
  <div id="main"></div>
  <div id="bottom"></div>
</div>
<div id="right">&nbsp;</div>
</body>
</html>

top.jsp 像这样:

<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
</head>
<body>  
<div id="logo"><img width="80px" height="65px" src="images/logo.jpg"></div>
<div id="menu">
<p>
  <a href="">hello</a>
  <a href="">work</a>
  <a href="">contact me</a>
</p>
</div>
</body>
</html>

我的问题如下:

1.无论我使用这两种方法中的哪一种,我在浏览器中点击“查看源代码”,我都得到了这个:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>title</title>
<style type="text/css">
  body {background-color:black;margin:0px;padding:0px;}
  #left,#right {width:10%;margin:0px;padding:0px;}
  #left,#center,#right {float:left;}
  #center {width:80%;}
  #top {height:150px;}
  #main {height:600px;background-color:white;}
</style>
</head>

<body>
<div id="left">&nbsp;</div>
<div id="center">
  <div id="top"><html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
</head>
<body>  
<div id="logo"><img width="80px" height="65px" src="images/logo.jpg"></div>
<div id="menu">
<p>
  <a href="">hello</a>
  <a href="">work</a>
  <a href="">contact me</a>
</p>
</div>
</body>
</html>
</div>
  <div id="main"></div>
  <div id="bottom"></div>
</div>
<div id="right">&nbsp;</div>
</body>
</html>

top.jsp 中的代码包含头部的内容出现在index.jsp 中,我想也许我犯了一些错误,应该是这样的。

2.如果我只写一些这样的标签:

<div id="logo"><img width="80px" height="65px" src="images/logo.jpg"></div>
<div id="menu">
<p>
  <a href="">hello</a>
  <a href="">work</a>
  <a href="">contact me</a>
</p>
</div>

那么如果代码中包含一些“ISO-8859-1”不支持的字符,myeclipse会报错信息对话框。 那么我应该如何正确地包含一个jsp页面呢? 谢谢!

刘鹏

【问题讨论】:

标签: jsp include


【解决方案1】:

JSP 在&lt;jsp:include&gt; 期间不会为您删除&lt;html&gt;&lt;head&gt;&lt;body&gt;。它只包括未修改的所有输出。如果您需要自动删除不相关的 HTML 元素,您应该使用具有模板功能的视图技术,例如 JSP 的后续 Facelets,或 Velocity、Freemarker 等一些 3rd 方库。

您的top.jsp 确实需要包含您真正需要最终出现在最终HTML 产品中的内容,该内容恰好位于父页面中声明&lt;jsp:include&gt; 的位置。只需将&lt;html&gt;&lt;head&gt;&lt;body&gt;top.jsp 隔开即可。

至于字符编码问题,这是另外一回事,与是否包含JSP无关。您只需要在每个 JSP 的顶部添加一个&lt;%@page pageEncoding="UTF-8" %&gt; 来告诉容器它应该使用给定的字符编码来处理 JSP。 UTF-8 是事实上的标准,它涵盖了人类所知道的每一个字符。为防止在每个 JSP 上重复同一行,请将其添加到 web.xml

<jsp-config>
    <jsp-property-group>
        <url-pattern>*.jsp</url-pattern>
        <page-encoding>UTF-8</page-encoding>
    </jsp-property-group>
</jsp-config>

【讨论】:

  • 感谢您的回复。当我将 添加到 top.jsp 时,问题解决了,但是当我删除行时 '' 从 top.jsp 和 相反,我仍然得到错误的信息对话框,无法保存 top.jsp,所以如果我有其他错误?跨度>
  • 也许您的 IDE 需要配置为使用正确的编码保存 JSP 文件。另见balusc.blogspot.com/2009/05/…
  • 再次感谢。我已经将jsp文件的默认编码设置为“UFT-8”,现在我可以成功保存jsp页面了。谢谢!
  • 这是否意味着你不能在你的jsp中指定一个css样式表,你将包括在内?因为当你将 jsp 包含到 body 中时,css 的导入也会放在你的 body 中。
【解决方案2】:

试试这个:这就像复制和粘贴一样。

在所有页面中添加:

<%@page contentType="text/html" pageEncoding="UTF-8"%>

顶部和底部是页眉和页脚,只要正文内容就足够了。不用打html标签就行了。

要在主页中导入页眉和页脚,请执行以下代码:

<html>
<head>
</head>
<body>
<jsp:include page="/head.jsp" />
my body content
<jsp:include page="/foot.jsp" />
</body>
</html>

您也可以在 head 中导入 css 和 js。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-04-11
    • 2016-02-19
    • 1970-01-01
    • 2010-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多