等于字符串的 JSTL if 标记

我从 JSP 页面上的一个对象获得了一个变量:

<%= ansokanInfo.getPSystem() %>

这个变量的值是 NAT,它是正确的,我想为这个值应用一些页面元素。如何使用标签来了解案件?我试过

<c:if test = "${ansokanInfo.getPSystem() == 'NAT'}">
process
</c:if>

但是上面并没有显示任何东西。我应该怎么做呢? 或者我也可以使用 scriptlet,即。

<% if (ansokanInfo.getPSystem().equals("NAT"){ %>
process
<% } %>

谢谢你的回答和评论。

349388 次浏览

Try:

<c:if test = "${ansokanInfo.PSystem == 'NAT'}">

JSP/Servlet 2.4 (I think that's the version number) doesn't support method calls in EL and only support properties. The latest servlet containers do support method calls (ie Tomcat 7).

<c:if test="${ansokanInfo.pSystem eq 'NAT'}">

You can use scriptlets, however, this is not the way to go. Nowdays inline scriplets or JAVA code in your JSP files is considered a bad habit.

You should read up on JSTL a bit more. If the ansokanInfo object is in your request or session scope, printing the object (toString() method) like this: ${ansokanInfo} can give you some base information. ${ansokanInfo.pSystem} should call the object getter method. If this all works, you can use this:

<c:if test="${ ansokanInfo.pSystem  == 'NAT'}"> tataa </c:if>

I think the other answers miss one important detail regarding the property name to use in the EL expression. The rules for converting from the method names to property names are specified in 'Introspector.decpitalize` which is part of the java bean standard:

This normally means converting the first character from upper case to lower case, but in the (unusual) special case when there is more than one character and both the first and second characters are upper case, we leave it alone.

Thus "FooBah" becomes "fooBah" and "X" becomes "x", but "URL" stays as "URL".

So in your case the JSTL code should look like the following, note the capital 'P':

<c:if test = "${ansokanInfo.PSystem == 'NAT'}">