1

I am very new to JSTL.

Now I have created an HashMap and rendered it in the jsp page .

This is how I Have crated the HashMap

In Application Level :

public HashMap<String, String> uploadSmelter(){

HashMap<String, String> progress = new HashMap<String, String>();

try {
        CompanyEntity instanceNew = (CompanyEntity) sessionFactory.getCurrentSession()
                .createCriteria(CompanyEntity.class)
                .add(Restrictions.eq("name", smObj.getName()))
                .add(Restrictions.eq("metal",  MetalEnum.valueOf(metal))).uniqueResult();
        if (instanceNew != null){
            //logger.info();
            progress.put("error", Integer.toString(1));
            progress.put("errorMsg", "Company : " +smObj.getName() + " for Metal: " + metal + " exists." );

        }
     }
 return progress
}

In Controller Level :

HashMap<String, String> result = new HashMap<String, String>();
result.put("error", "0");        
result.put("update", "0");
result.put("create", "0");
HashMap<String, String> progress = new HashMap<String, String>();
int number = 0;
while (rowIterator.hasNext()) 
  {
    progress= companyFacade.uploadSmelter(); // implemented in application level uploadSmelter method

if(progress.get("errorMsg")!=null){   
 result.put(Integer.toString(number),progress.get("errorMsg"));
}
 number=number+1 ;
}

Now finally if I print it in the jsp page , i am getting

{3=Company: ABC COMPANY  for Metal: A Exists, update=0, 2=Company: ABC COMPANY TIN for Metal TIN Exists, 1=Company: ABC COMPANY G for Metal METAL_TANTALUM Exists, 0=Company: ABC COMPANY L for Metal METAL_TANTALUM Exists, error=4, fileName=data.1407219942830.xls, number=4, create=0}

Now I want to run a foreach loop in jsp page that should print filtered value from the Hashmap.

So I have added those condition in JSTL if statement . But seems to be it is not supporting the condition statement and I am not getting any output .

<c:forEach items="${result}" var="item">
  <c:if test="${item.key} !='update' && ${item.key} !='error' && ${item.key} !='create' && ${item.key} !='number' && ${item.key} !='fileName'}"  >                
     <div class="attributeField">Line Number <c:out value="${item.key}" /> &nbsp;<spring:message message="${item.value}"/></div> 
  </c:if>
</c:forEach>

So what the mistake I am doing ? Thanks for help in Advance.

curiousguy
  • 3,212
  • 8
  • 39
  • 71

1 Answers1

6

Just use single ${...} surrounding all the conditions as value of test attribute.

sample code:

<c:forEach items="${result}" var="item">
    <c:if
        test="${item.key !='update' && item.key !='error' && item.key !='create' && item.key !='number' && item.key !='fileName'}">
        ...
    </c:if>
</c:forEach>

&& can only be executed inside ${...}.

Learn more...

Community
  • 1
  • 1
Braj
  • 46,415
  • 5
  • 60
  • 76
  • I tried this already , but getting the error `javax.servlet.ServletException: javax.servlet.jsp.JspException: Attribute value "Company: ABC COMPANY for Metal: A Exists" is neither a JSP EL expression nor assignable to result class [org.springframework.context.MessageSourceResolvable]` – curiousguy Aug 05 '14 at 07:05
  • 1
    that's not related to condition part. for fast response please share minimal testable code. how you are populating the map? – Braj Aug 05 '14 at 07:07
  • I have explained now. Does it make sense ? – curiousguy Aug 05 '14 at 07:29
  • The problem is at ``. there is no message for `Company: ABC COMPANY for Metal: A Exists` key. Better to share `messages.properties` as well. – Braj Aug 05 '14 at 07:43
  • 1
    Exactly @user3218114 , I have changed this now to `
    Line Number  
    ` and its working fine.
    – curiousguy Aug 05 '14 at 07:54
  • Good news. So you don't want to fetch message from properties file. Right? If not then there is no use of using ` – Braj Aug 05 '14 at 08:00