我有一个类,其中有一个名为 title 的属性,并且我有一个获取和设置该属性的 getter/setter。如果属性是 P,我需要在页面上打印“Peer”一词,如果属性是 T,我需要在页面上打印“Team”。我可以在 JSP 中执行此操作而不使用 scriplet 吗?我尝试使用
<jsp:getProperty name="value" class"classname" />
但是从那里我不知道如何在 JSP 中使用条件。请帮忙。
最佳答案
使用 JSTL,如 @CoolBeans says 。它看起来像这样:
在 servlet 中,
// where myBean is an instance of the class with [get|set]Title
request.setAttribute("myFoo", myBean);
然后,在 JSP 中,
<c:choose>
<c:when test="${myBean.title eq 'P'}">Peer</c:when>
<c:when test="${myBean.title eq 'T'}">Team</c:when>
</c:choose>
如果您不熟悉 JSTL,我建议您阅读 JSP section of the Java EE 5 Tutorial ,或获取 Head First Servlets and JSP 的副本(相当不错)。
关于java - 在 JSP 中显示 Java 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5386508/