JSP-将Java与HTML分开 [英] JSP - separating Java from HTML
本文介绍了JSP-将Java与HTML分开的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果我有这样的事情:
<%
String name = (String)session.getAttribute("username");
if(name!=null)
{%>
<div id="navBar">
<h2><a href="blah.jsp">Home</a> |
<a href="blah1.jsp">SomewhereElse</a> |
</div>
<%}%>
基本上,如果变量名称为null,则不显示导航栏,但是由于应避免将Java和HTML混合使用-我看不到如何将其重写以分离两种语言?
Basically, if variable name is null, don't display navigation bar, but since mixing Java and HTML should be avoided - I can't see how you can rewrite this to separate the 2 languages???
推荐答案
Use taglibs/EL. Your particular example can be solved as follows with help of JSTL <c:if>
:
<c:if test="${not empty username}">
<div id="navBar">
<h2><a href="blah.jsp">Home</a> |
<a href="blah1.jsp">SomewhereElse</a> |
</div>
</c:if>
仅当页面,请求,会话或应用程序范围中的任何一个中没有名称为username
的属性时,才会打印给定的HTML.
The given HTML will only be printed when there's no attribute with the name username
in any of the page, request, session or application scopes.
- How to avoid Java code in JSP files?
- JSTL tag info page
- EL tag info page
这篇关于JSP-将Java与HTML分开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文