如何从JSP访问Locale?

时间:2021-07-05 07:29:15

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:

我想根据当前Locale的值包含一个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>
<% } %>

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

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

Does anyone knows how I can solve this?

有谁知道我怎么能解决这个问题?

10 个解决方案

#1


19  

At the moment I am using this :

目前我正在使用这个:

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

This can later be access by using ${localeCode}

稍后可以使用$ {localeCode}进行访问

  1. Scriplet mode, discouraged! See Why not use Scriptlets for reason not to use a scriptlet.
  2. Scriplet模式,气馁!请参阅为什么不使用Scriptlet而不使用scriptlet。

The localeCode variable can be queried inside a scriptlet with:

可以在scriptlet中使用以下内容查询localeCode变量:

<%
  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.
  2. Scripletless模式正确的方法去。请参阅如何避免JSP文件中的Java代码?在这里。

I am using spring 2.5 config at the moment.

我目前正在使用spring 2.5配置。

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>

#2


4  

in Struts2 try

在Struts2中试试

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

#3


3  

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

Struts将语言环境放入会话中。获取Locale的正确方法是:

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

#4


2  

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?

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


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.

编辑:org.apache.struts.action.LOCALE是org.apache.struts.Global.LOCALE_KEY的值 - 因此用作键的值本身不应该是问题。

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.

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

#5


1  

In Struts2, using EL I successfully used:

在Struts2中,使用EL I成功使用:

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

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

例如。输出Locale的值:

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

#6


1  

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

我添加了新的例子来澄清这一点,因为这篇文章对我没什么帮助。

To get locale from jsp:

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

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

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

返回Struts2 Framework的语言环境,可能与前一个示例中的相同或不同。如果你传递param 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>

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

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

#7


1  

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>

#8


0  

Ken G. pointed to the answer.

Ken G.指出了答案。

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

should be used instead

应该用来代替

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

#9


0  

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

#10


0  

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}"/>

获取区域设置的两种最佳方法是使用动作继承的ActionLoale of Action支持到JSP:

When locale has been changed with this method.

使用此方法更改语言环境时。

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

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

#1


19  

At the moment I am using this :

目前我正在使用这个:

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

This can later be access by using ${localeCode}

稍后可以使用$ {localeCode}进行访问

  1. Scriplet mode, discouraged! See Why not use Scriptlets for reason not to use a scriptlet.
  2. Scriplet模式,气馁!请参阅为什么不使用Scriptlet而不使用scriptlet。

The localeCode variable can be queried inside a scriptlet with:

可以在scriptlet中使用以下内容查询localeCode变量:

<%
  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.
  2. Scripletless模式正确的方法去。请参阅如何避免JSP文件中的Java代码?在这里。

I am using spring 2.5 config at the moment.

我目前正在使用spring 2.5配置。

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>

#2


4  

in Struts2 try

在Struts2中试试

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

#3


3  

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

Struts将语言环境放入会话中。获取Locale的正确方法是:

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

#4


2  

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?

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


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.

编辑:org.apache.struts.action.LOCALE是org.apache.struts.Global.LOCALE_KEY的值 - 因此用作键的值本身不应该是问题。

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.

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

#5


1  

In Struts2, using EL I successfully used:

在Struts2中,使用EL I成功使用:

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

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

例如。输出Locale的值:

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

#6


1  

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

我添加了新的例子来澄清这一点,因为这篇文章对我没什么帮助。

To get locale from jsp:

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

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

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

返回Struts2 Framework的语言环境,可能与前一个示例中的相同或不同。如果你传递param 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>

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

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

#7


1  

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>

#8


0  

Ken G. pointed to the answer.

Ken G.指出了答案。

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

should be used instead

应该用来代替

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

#9


0  

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

#10


0  

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}"/>

获取区域设置的两种最佳方法是使用动作继承的ActionLoale of Action支持到JSP:

When locale has been changed with this method.

使用此方法更改语言环境时。

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

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