简体   繁体   English

jsp / jstl页面中的Java错误

[英]java error in jsp/jstl page

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. languageRB是存储在会话中的属性,类型为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. 我要执行以下操作:如果session中存在languageRB,则使用括号中的字符串值定义属性,否则将属性设置为默认值。

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

Firstly, you shouldn't mix scriptlets and taglibs/EL. 首先,您不应混用scriptlet和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. 十多年来, 正式禁止使用 脚本 ,因此您应该忘记它们,并坚持使用taglibs / EL。 Your concrete problem is caused because scriptltets are always invoked regardless of outcome of JSTL taglibs. 造成您的具体问题的原因是,无论JSTL标签库的结果如何,总是会调用脚本 They do not run in sync with taglibs based on the coding. 它们不会根据编码与taglib同步运行。 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. 您可以将其可视化如下: scriptlet首先从上到下运行,然后轮到taglibs / EL从上到下运行。 You should use EL to access the resource bundle property. 您应该使用EL来访问资源束属性。 Additional advantage is that EL is null-safe, it won't throw a NPE, but just bypass the property access. EL的另一个优点是它是null安全的,它不会抛出NPE,而只是绕过属性访问。

Secondly, you've a new problem when you replace the scriptlet by EL, the <c:otherwise> doesn't support a test attribute at all. 其次,当您用EL替换scriptlet时,您遇到了一个新问题, <c:otherwise>根本不支持test属性。 Get rid of it. 摆脱它。 It's already only hit when none of the <c:when> conditions have matched. 仅当<c:when>条件都不匹配<c:when>它才被命中。

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>

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

<%=((ResourceBundle)session.getAttribute("languageRB")).getString("style.language")%>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM