0

I have the following code:

<c:choose>
    <c:when test="${empty sessionScope.languageRB}">
        <html:hidden property="language" value="en"/>
        <html:hidden property="country" value="GB"/>
    </c:when>
    <c:otherwise test="${not empty sessionScope.languageRB}">
        <html:hidden property="language" value="<%=languageRB.getString("style.language")%>"/>
        <html:hidden property="country" value="<%=languageRB.getString("style.country")%>"/>
    </c:otherwise>
</c:choose>

languageRB is an attribute stored in session, of type ResourceBundle. I want to do the following: if languageRB exists in session then the property is defined using the value of the string in the paranthesis, otherwise the property is set to a default value.

I'm getting the following error:

    org.apache.jasper.JasperException: Unable to compile class for JSP: 

An error occurred at line: 89 in the jsp file: /pages/common002-preparelogin.jsp
languageRB cannot be resolved

88:         <c:otherwise test="${not empty sessionScope.languageRB}">
89:             <html:hidden property="language" value="<%=languageRB.getString("style.language")%>"/>
90:             <html:hidden property="country" value="<%=languageRB.getString("style.country")%>"/>
user1802439
  • 2,651
  • 3
  • 18
  • 21

2 Answers2

1

Firstly, you shouldn't mix scriptlets and taglibs/EL. Use the one or the other. As scriptlets are officially discouraged since a decade, you should forget about them and stick to taglibs/EL. Your concrete problem is caused because scriptltets are always invoked regardless of outcome of JSTL taglibs. They do not run in sync with taglibs based on the coding. You can visualize it as follows: scriptlets run form top to bottom first and then it's taglibs/EL's turn to run from top to bottom again. You should use EL to access the resource bundle property. Additional advantage is that EL is null-safe, it won't throw a NPE, but just bypass the property access.

Secondly, you've a new problem when you replace the scriptlet by EL, the <c:otherwise> doesn't support a test attribute at all. Get rid of it. It's already only hit when none of the <c:when> conditions have matched.

So, all with all, this should do:

<c:choose>
    <c:when test="${empty sessionScope.languageRB}">
        <html:hidden property="language" value="en"/>
        <html:hidden property="country" value="GB"/>
    </c:when>
    <c:otherwise>
        <html:hidden property="language" value="${languageRB['style.language']}"/>
        <html:hidden property="country" value="${languageRB['style.country']}"/>
    </c:otherwise>
</c:choose>
Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
0

In expression you need to get your bundle from session directly:

<%=((ResourceBundle)session.getAttribute("languageRB")).getString("style.language")%>
Alexey A.
  • 1,389
  • 1
  • 11
  • 20