0

我有以下代码:

<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 是存储在会话中的属性,类型为 ResourceBundle。我想做以下事情:如果会话中存在语言RB,则使用括号中字符串的值定义属性,否则将属性设置为默认值。

我收到以下错误:

    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")%>"/>
4

2 回答 2

1

首先,您不应该混合使用scriptlet和 taglibs/EL。使用其中之一。由于Scriptlet十年来一直被官方禁止,您应该忘记它们并坚持使用 taglibs/EL。您的具体问题是因为无论 JSTL 标签库的结果如何,总是调用scriptltets 。它们不会与基于编码的标记库同步运行。您可以将其可视化如下:scriptlet首先从上到下运行,然后轮到 taglibs/EL 再次从​​上到下运行。您应该使用 EL 来访问资源包属性。另一个优点是 EL 是空安全的,它不会抛出 NPE,而只是绕过属性访问。

其次,当你用EL替换scriptlet<c:otherwise>时,你有一个新问题,根本不支持test属性。摆脱它。<c:when>只有当没有任何条件匹配时,它才会被击中。

所以,总而言之,这应该做:

<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>
于 2013-02-22T15:16:16.637 回答
0

在表达式中,您需要直接从会话中获取捆绑包:

<%=((ResourceBundle)session.getAttribute("languageRB")).getString("style.language")%>
于 2013-02-22T15:07:27.523 回答