简单的JSP网站
我正在寻找使用相当于 PHP include+switch 功能创建简单 JSP 网站的帮助/建议。
目标是我希望能够在一个主页中的多个 JSP 包含页面之间进行切换。
上述“函数”最简单的形式是什么?
I'm looking for help/advice with creating simple JSP website using equivalent of PHP include+switch function.
The goal is that I want to be able to switch between multiple JSP include pages in one main page.
What would be the simplest possible form of above 'function'?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在那里你有< /code>
为。您可以使用 EL 来指定
页面
属性。创建一个
/WEB-INF/main.jsp
文件,如下所示:您可以借助页面控制器 servlet 来控制
${page}
值。类似于:在
web.xml
中映射此 servlet,如下所示:这样可以通过
http://example.com/context/page/foo.jsp
访问该 servlet,并且在此 URL 示例中,它将从路径信息中获取/foo.jsp
,从而将page
属性设置为值/WEB-INF/foo.jsp< /code> 以便它在 EL 中可用
${page}
以便jsp:include
知道它应该包含什么。不需要讨厌的 scriptlet 或 switch 语句。在
/WEB-INF/foo.jsp
中,您只需写下 HTML,就像将其放置在 HTML标记内一样。
请注意,JSP 文件放置在
/WEB-INF
中,这样做是为了防止通过 URL 直接访问,这样用户就无法在不通过页面控制器的情况下请求它们,例如http://example.com/context/foo.jsp
只会返回部分内容(要包含的页面)。希望这有帮助。
There you have the
<jsp:include>
for. You can use EL to specify thepage
attribute.Create a
/WEB-INF/main.jsp
file which look like:You can control the
${page}
value with help of a page controller servlet. Something like:Map this servlet in
web.xml
as follows:This way the servlet is accessible through
http://example.com/context/page/foo.jsp
and in this URL example it will then get/foo.jsp
from the pathinfo and thus set thepage
attribute with the value/WEB-INF/foo.jsp
so that it is available in EL as${page}
so that thejsp:include
knows what it should include. No need for nasty scriptlets or switch statements.In the
/WEB-INF/foo.jsp
you can just write down HTML as if it is placed inside the HTML<body>
tag.Note that the JSP files are placed in
/WEB-INF
, this is done so to prevent direct access by URL so that the users cannot request them without going through the page controller, such as for examplehttp://example.com/context/foo.jsp
which would only return the partial content (the to-be-included page).Hope this helps.
尝试
或者,如果可以的话,请查看 JSF2 和/或 Facelets。它具有更强大的模板功能。
Try
Or, if you have the option, check out JSF2 and/or Facelets. It has much more powerful templating capabilities.