我想根据当前语言环境的值包含一个js文件.我试图从JSP访问它,如下所示: %@ page import="java.util.Locale" % % if( ((Locale) pageContext.getAttribute("org.apache.struts.action.LOCALE",PageContext.REQUEST_SCOPE)).get
          <%@ page import="java.util.Locale" %>  
<% if( ((Locale) pageContext.getAttribute("org.apache.struts.action.LOCALE",PageContext.REQUEST_SCOPE)).getLanguage().equals("de")) { %>
    <script src="../themes/administration/js/languages/i18nDE.js" type="text/javascript"> </script>
<% } else { %>
    <script src="../themes/administration/js/languages/i18nEN.js" type="text/javascript"> </script>
<% } %> 
 但是,由于pageContext.getAttribute(“org.apache.struts.action.LOCALE”,PageContext.REQUEST_SCOPE)为NULL,因此我获得了java.lang.NullPointerException.
有谁知道如何解决这个问题?
目前我使用的是:<c:set var="localeCode" value="${pageContext.response.locale}" /> 
 以后可以通过使用${localeCode}
> Scriplet模式,不鼓励!请参阅Why not use Scriptlets为何不使用scriptlet.
localeCode变量可以在scriptlet中查询:
<%
  Object ob_localeCode = pageContext.getAttribute("localeCode");
  if (ob_localeCode != null) {
    String currentLanguageCode = (String) ob_localeCode;
  }
  //more code
%> 
 > Scripletless模式正确的方式去.参见How to avoid Java Code in JSP-Files?这里.
我现在使用spring 2.5配置.
所以接下来,回到你原来的问题,你可以实现一些如下:
<c:set var="localeCode" value="${pageContext.response.locale}" />
<c:choose>
  <c:when test="$localecode == 'de' }"> 
    <script src="../themes/administration/js/languages/i18nDE.js" type="text/javascript"> </script>
  </c:when>
  <c:otherwise>
    <script src="../themes/administration/js/languages/i18nEN.js" type="text/javascript"> </script>
  </c:otherwise>
</c:choose> 
 或者如果您真的想使用一些简短的代码来打动同事,您可以:
<c:set var="localeCode" value="${fn:toUpperCase(pageContext.response.locale)}" />
<c:set var="availLanguages" value="EN,DE" />
<c:if test="${!fn:contains(availLanguages,localeCode)}">
  <c:set var="localeCode" value="EN" />
</c:if>
<script src="../themes/administration/js/languages/i18n{$localeCode}.js" type="text/javascript"> </script>
        
             