如何从 JSP 访问区域设置?

发布于 2024-07-09 02:15:53 字数 734 浏览 6 评论 0 原文

我想根据当前区域设置的值包含一个 js 文件。 我尝试从 JSP 访问它,如下所示:

<%@ 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>
<% } %>

但是,我收到 java.lang.NullPointerException 因为 pageContext.getAttribute("org.apache.struts.action.LOCALE",PageContext .REQUEST_SCOPE)NULL

有谁知道我该如何解决这个问题?

I want to include a js file depending on the value of the current Locale. I have tried to access it from JSP as follows:

<%@ 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>
<% } %>

However, I am getting a java.lang.NullPointerException because pageContext.getAttribute("org.apache.struts.action.LOCALE",PageContext.REQUEST_SCOPE) is NULL.

Does anyone knows how I can solve this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

美羊羊 2024-07-16 02:15:53

目前我正在使用这个:

<c:set var="localeCode" value="${pageContext.response.locale}" />

稍后可以使用 ${localeCode}

  1. Scriplet 模式访问它,气馁! 请参阅为什么不使用 Scriptlet 了解不使用 Scriptlet 的原因。

可以通过以下方式在 scriptlet 内查询 localeCode 变量:

<%
  Object ob_localeCode = pageContext.getAttribute("localeCode");
  if (ob_localeCode != null) {
    String currentLanguageCode = (String) ob_localeCode;
  }
  //more code
%>
  1. Scripletless 模式 正确的方法。 请参阅如何避免 JSP 文件中的 Java 代码? 此处。

我现在使用的是 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>

At the moment I am using this :

<c:set var="localeCode" value="${pageContext.response.locale}" />

This can later be access by using ${localeCode}

  1. Scriplet mode, discouraged! See Why not use Scriptlets for reason not to use a scriptlet.

The localeCode variable can be queried inside a scriptlet with:

<%
  Object ob_localeCode = pageContext.getAttribute("localeCode");
  if (ob_localeCode != null) {
    String currentLanguageCode = (String) ob_localeCode;
  }
  //more code
%>
  1. Scripletless mode correct way to go. See How to avoid Java Code in JSP-Files? Here on SO.

I am using spring 2.5 config at the moment.

So following this, coming back to your original question you can implement something like:

<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>

or if you really want to use some short code to impress your colleagues, you can do:

<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>
小ぇ时光︴ 2024-07-16 02:15:53

在Struts2中尝试

<s:if test="#request.locale.language=='us'">
     <s:select name="gender" list="#{'M':'Male','F':'female'}" ></s:select>
 </s:if>

in Struts2 try

<s:if test="#request.locale.language=='us'">
     <s:select name="gender" list="#{'M':'Male','F':'female'}" ></s:select>
 </s:if>
囚我心虐我身 2024-07-16 02:15:53

Struts 将区域设置放入会话中。 获取 Locale 的正确方法是:

Locale locale = (locale)request.getSession().getAttribute(Globals.LOCALE_KEY);

Struts puts locale in the session. The correct way to get the Locale is:

Locale locale = (locale)request.getSession().getAttribute(Globals.LOCALE_KEY);
绅士风度i 2024-07-16 02:15:53

我在 Struts 1.x 文档中找不到常量 org.apache.struts.action.LOCALE - 应该是 org.apache.struts.Globals.LOCALE_KEY? 或者其他 LOCALE_KEY 常量之一?


编辑:org.apache.struts.action.LOCALEorg.apache.struts.Global.LOCALE_KEY - 所以该值本身用作密钥,应该不是问题。

验证 Request 中是否设置了 LOCALE。 我的理解是,如果设置了 LOCALE_KEY ,则在 PageContext.SESSION_SCOPE 中设置。

I can't find a constant org.apache.struts.action.LOCALE in the Struts 1.x documentation - should it be org.apache.struts.Globals.LOCALE_KEY? Or one of the other LOCALE_KEY constants?


Edit: org.apache.struts.action.LOCALE is the value of the org.apache.struts.Global.LOCALE_KEY - so the value itself, used as a key, shouldn't be the problem.

Verify that a LOCALE is being set in the Request. My understanding is that the LOCALE_KEY is set in PageContext.SESSION_SCOPE if it is set.

_畞蕅 2024-07-16 02:15:53

在Struts2中,使用EL我成功地使用:

${sessionScope["org.apache.struts2.action.LOCALE"]}

例如输出Locale的值:

<c:out value='${sessionScope["org.apache.struts2.action.LOCALE"]}'/>

In Struts2, using EL I successfully used:

${sessionScope["org.apache.struts2.action.LOCALE"]}

E.g. to output the value of the Locale:

<c:out value='${sessionScope["org.apache.struts2.action.LOCALE"]}'/>
也只是曾经 2024-07-16 02:15:53

我添加了新的示例来进一步澄清这一点,因为这篇文章对我没有多大帮助。

要从 jsp 获取语言环境:

<%=request.getLocale()%>

它是一个 ServletRequest 方法 a 根据 Accept-Language 标头返回客户端将接受内容的首选语言环境,

Struts2 Locale: <s:property value="#request.locale"/>

返回 Struts2 框架的语言环境,该语言环境可能与前面的例子。 例如,如果您传递参数 request_locale=de ...

<s:url id="localeDE" namespace="/">
   <s:param name="request_locale" >de</s:param>
</s:url>
<s:a href="%{localeDE}" >German</s:a>

struts2 #request.locale 将更改为德语,覆盖原始 Accept-Language 标头的值

I added new examples to clarify this a bit more because this post didn't help me much.

To get locale from jsp:

<%=request.getLocale()%>

it's a ServletRequest Method a Returns the preferred Locale that the client will accept content in, based on the Accept-Language header,

Struts2 Locale: <s:property value="#request.locale"/>

Returns the locale for the Struts2 Framework, that may or may not be the same as in the previous example. if you pass the param request_locale=de for instance...

<s:url id="localeDE" namespace="/">
   <s:param name="request_locale" >de</s:param>
</s:url>
<s:a href="%{localeDE}" >German</s:a>

the struts2 #request.locale will changed to german overriding the value of the original Accept-Language header

沉鱼一梦 2024-07-16 02:15:53

尝试用这个

<s:if test='locale.toString() == "si"'>
    <script src="../themes/administration/js/languages/i18nDE.js" type="text/javascript"> </script>
</s:if>
<s:elseif test='locale.toString() == "ta"'>
    <script src="../themes/administration/js/languages/i18nEN.js" type="text/javascript"> </script>
</s:elseif>
<s:else>
    ANOTHER SCRIPT
</s:else>

Try with this

<s:if test='locale.toString() == "si"'>
    <script src="../themes/administration/js/languages/i18nDE.js" type="text/javascript"> </script>
</s:if>
<s:elseif test='locale.toString() == "ta"'>
    <script src="../themes/administration/js/languages/i18nEN.js" type="text/javascript"> </script>
</s:elseif>
<s:else>
    ANOTHER SCRIPT
</s:else>
烙印 2024-07-16 02:15:53

肯·G.指出了答案。

pageContext.getAttribute("org.apache.struts.action.LOCALE",PageContext.SESSION_SCOPE) 

应该使用

pageContext.getAttribute("org.apache.struts.action.LOCALE",PageContext.REQUEST_SCOPE)

Ken G. pointed to the answer.

pageContext.getAttribute("org.apache.struts.action.LOCALE",PageContext.SESSION_SCOPE) 

should be used instead

pageContext.getAttribute("org.apache.struts.action.LOCALE",PageContext.REQUEST_SCOPE)
柠檬 2024-07-16 02:15:53
<%@page import="java.util.Locale"%>
<%@page import="org.apache.struts.Globals"%>


<%Locale locale = (Locale)session.getAttribute(Globals.LOCALE_KEY);
if (locale.getLanguage().equals("fr")) {%>
    <script language="JavaScript" src="lib/js/dateofday.js" type="text/javascript"></script>
    <script type="text/javascript" src="<%=request.getContextPath() %>/lib/js/jscalendar-1.0/lang/calendar-fr.js"></script>
<%} else {%>
    <script language="JavaScript" src="lib/js/dateofday-en.js" type="text/javascript"></script>
    <script type="text/javascript" src="<%=request.getContextPath() %>/lib/js/jscalendar-1.0/lang/calendar-en.js"></script>
<%}%>
<%@page import="java.util.Locale"%>
<%@page import="org.apache.struts.Globals"%>


<%Locale locale = (Locale)session.getAttribute(Globals.LOCALE_KEY);
if (locale.getLanguage().equals("fr")) {%>
    <script language="JavaScript" src="lib/js/dateofday.js" type="text/javascript"></script>
    <script type="text/javascript" src="<%=request.getContextPath() %>/lib/js/jscalendar-1.0/lang/calendar-fr.js"></script>
<%} else {%>
    <script language="JavaScript" src="lib/js/dateofday-en.js" type="text/javascript"></script>
    <script type="text/javascript" src="<%=request.getContextPath() %>/lib/js/jscalendar-1.0/lang/calendar-en.js"></script>
<%}%>
愿得七秒忆 2024-07-16 02:15:53

获取 locale 的两种最佳方法是使用由操作继承的 Action 支持的 getLocale 到 JSP 上:

当使用 此方法

它不同于:
${pageContext.response.locale}

The two best ways to get locale is by using the getLocale of Action support inherited by an action, onto a JSP:
<s:hidden name="locale"/> or
<s:property value"%{locale}"/>

When locale has been changed with this method.

It is not the same as:
${pageContext.response.locale}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文