You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Nadia Kunkov <na...@parkerglobal.com> on 2004/12/02 16:24:55 UTC

JSTL when...test won't work.

Hello,

This is my first try with JSTL and somewhere I'm getting the syntax wrong.  Could you please help me?
I have a bean called user in the session.  One of this bean's properties is a hash map called frmRoles.  I need to go over the hashmap's keys and test if a particular key exists.  I'm not getting an error when I run the jsp,  it just tells me that this key is not found.  But I can print all the keys so its my c:choose   test syntax.  The code that gives me a problem is in bold face.  I also would like to ask you to point me to the documentation on the syntax used within c:when test.  Is it regular expressions or JSTL syntax?

I'd appreciate any help.

Thanks in advance.

NK

<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<%@ taglib prefix="c_rt" uri="http://java.sun.com/jstl/core_rt" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>

<html>
<head>
<title>Welcome World!</title>
<html:base/>
</head>
<body bgcolor="white">
<logic:present scope="session" name="user">
  <h3>Welcome <bean:write name="user"  property="userId"/>!</h3>
  <br>
  <bean:write name="user" property="userFName"/>
  <bean:write name="user" property="userLName"/>
</logic:present>
<logic:notPresent scope="session" name="user">
  <h3>Welcome World!</h3>
</logic:notPresent>
<html:errors/>  
<ul>
<li><html:link forward="logon">Sign in</html:link></li>
<logic:present scope="session" name="user">
  <li><html:link forward="logoff">Sign out</html:link></li>
  <br>
  <br>
  <bean:define id="fRoles"  name="user" property="frmRoles"/>
  <logic:iterate id="element" name="fRoles">
       Key is <bean:write name="element" property="key"/>  <br>
     Value is <bean:write name="element" property="value"/>  <br>
  </logic:iterate>
  <c:choose>
     <c:when test="${fRoles['manager'] != null}">
        Found formRoles manager
     </c:when>
     <c:otherwise>
        NOT Found
     </c:otherwise>
  </c:choose> 

</logic:present>
</ul>
<img src='struts-power.gif' alt='Powered by Struts'>

</body>
</html>


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: JSTL when...test won't work.

Posted by Bill Siggelkow <bi...@bellsouth.net>.
   <c:choose>
      <c:when test="${not empty fRoles['manager']}">
         Found formRoles manager
      </c:when>
      <c:otherwise>
         NOT Found
      </c:otherwise>
   </c:choose>

Nadia Kunkov wrote:

> Hello,
> 
> This is my first try with JSTL and somewhere I'm getting the syntax wrong.  Could you please help me?
> I have a bean called user in the session.  One of this bean's properties is a hash map called frmRoles.  I need to go over the hashmap's keys and test if a particular key exists.  I'm not getting an error when I run the jsp,  it just tells me that this key is not found.  But I can print all the keys so its my c:choose   test syntax.  The code that gives me a problem is in bold face.  I also would like to ask you to point me to the documentation on the syntax used within c:when test.  Is it regular expressions or JSTL syntax?
> 
> I'd appreciate any help.
> 
> Thanks in advance.
> 
> NK
> 
> <%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
> <%@ taglib prefix="c_rt" uri="http://java.sun.com/jstl/core_rt" %>
> <%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
> <%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
> <%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
> 
> <html>
> <head>
> <title>Welcome World!</title>
> <html:base/>
> </head>
> <body bgcolor="white">
> <logic:present scope="session" name="user">
>   <h3>Welcome <bean:write name="user"  property="userId"/>!</h3>
>   <br>
>   <bean:write name="user" property="userFName"/>
>   <bean:write name="user" property="userLName"/>
> </logic:present>
> <logic:notPresent scope="session" name="user">
>   <h3>Welcome World!</h3>
> </logic:notPresent>
> <html:errors/>  
> <ul>
> <li><html:link forward="logon">Sign in</html:link></li>
> <logic:present scope="session" name="user">
>   <li><html:link forward="logoff">Sign out</html:link></li>
>   <br>
>   <br>
>   <bean:define id="fRoles"  name="user" property="frmRoles"/>
>   <logic:iterate id="element" name="fRoles">
>        Key is <bean:write name="element" property="key"/>  <br>
>      Value is <bean:write name="element" property="value"/>  <br>
>   </logic:iterate>
>   <c:choose>
>      <c:when test="${fRoles['manager'] != null}">
>         Found formRoles manager
>      </c:when>
>      <c:otherwise>
>         NOT Found
>      </c:otherwise>
>   </c:choose> 
> 
> </logic:present>
> </ul>
> <img src='struts-power.gif' alt='Powered by Struts'>
> 
> </body>
> </html>


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: JSTL when...test won't work.

Posted by Rick Reumann <st...@reumann.net>.
Nadia Kunkov wrote the following on 12/2/2004 10:24 AM:

>      <c:when test="${fRoles['manager'] != null}">

You don't want to do this. Comparisons to null won't work. You want to 
use "empty." Change it to

<c:when test="${!empty fRoles['manager']}">

!empty should evaluate both null and empty. (At least I 'think' that's 
right:)

-- 
Rick

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org