- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 SEAM 2 的 LocaleSelector 在我的应用程序中设置可以正常工作的区域设置
我还使用 JAWR 的 i18n 消息生成器使消息可用作 Javascript 变量,按照说明 here我创建了一个类来解析当前区域设置,以便 JAWR 加载正确的消息包,这是我的代码
@Name("localeResolver")
public class LocaleResolver implements net.jawr.web.resource.bundle.locale.LocaleResolver {
@In private LocaleSelector localeSelector;
public LocaleResolver() {}
@Override
public String resolveLocaleCode(HttpServletRequest request) {
if(localeSelector!=null){
System.out.println("locale selector is not null setting jawr locale to "+localeSelector.getLocaleString());
return localeSelector.getLocaleString();
}else{
System.out.println("locale selector is null setting jawr locale to "+request.getLocale().toString());
return request.getLocale().toString();
}
}
}
但是 localeSelector 始终为 null,因此它使用 request.getLocale 进行设置,这似乎是浏览器的默认区域设置(如果用户希望手动覆盖则不好)
为什么注入(inject)的 localeSelector 会为 null,我在其他组件中已经成功注入(inject)了?
谢谢
更新:web.xml 内容
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<context-param>
<param-name>facelets.DEVELOPMENT</param-name>
<param-value>true</param-value>
</context-param>
<context-param>
<param-name>javax.faces.DEFAULT_SUFFIX</param-name>
<param-value>.xhtml</param-value>
</context-param>
<context-param>
<param-name>javax.faces.STATE_SAVING_METHOD</param-name>
<param-value>server</param-value>
</context-param>
<context-param>
<param-name>org.ajax4jsf.VIEW_HANDLERS</param-name>
<param-value>com.sun.facelets.FaceletViewHandler</param-value>
</context-param>
<context-param>
<param-name>org.jboss.jbossfaces.JSF_CONFIG_NAME</param-name>
<param-value>Mojarra-1.2</param-value>
</context-param>
<context-param>
<param-name>org.richfaces.SKIN</param-name>
<param-value>plain</param-value>
</context-param>
<!-- Richfaces Global Ajax Que,
added as getting concurrent conversation access exceptions
from unknown locations -->
<context-param>
<param-name>org.richfaces.queue.global.enabled</param-name>
<param-value>true</param-value>
</context-param>
<filter>
<filter-name>Seam Filter</filter-name>
<filter-class>org.jboss.seam.servlet.SeamFilter</filter-class>
</filter>
<!-- Seam -->
<listener>
<listener-class>org.jboss.seam.servlet.SeamListener</listener-class>
</listener>
<servlet>
<servlet-name>Seam Resource Servlet</servlet-name>
<servlet-class>org.jboss.seam.servlet.SeamResourceServlet</servlet-class>
</servlet>
<!-- JSF -->
<servlet>
<servlet-name>Document Store Servlet</servlet-name>
<servlet-class>org.jboss.seam.document.DocumentStoreServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- jawr Servlet -->
<servlet>
<servlet-name>JavascriptServlet</servlet-name>
<servlet-class>net.jawr.web.servlet.JawrServlet</servlet-class>
<!-- Location in classpath of the config file -->
<init-param>
<param-name>configLocation</param-name>
<param-value>/jawr.properties</param-value>
</init-param>
<init-param>
<param-name>mapping</param-name>
<param-value>/jsJawrPath/</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>JavascriptServlet</servlet-name>
<url-pattern>/jsJawrPath/*</url-pattern>
</servlet-mapping>
<!-- Store item Servlet -->
<servlet-mapping>
<servlet-name>Document Store Servlet</servlet-name>
<url-pattern>*.csv</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Document Store Servlet</servlet-name>
<url-pattern>*.xls</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Document Store Servlet</servlet-name>
<url-pattern>*.pdf</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
<!-- 120 min session timeout -->
<session-config>
<session-timeout>120</session-timeout>
</session-config>
<!-- Mime types -->
<mime-mapping>
<extension>htc</extension>
<mime-type>text/x-component</mime-type>
</mime-mapping>
<!-- Index file def -->
<welcome-file-list>
<welcome-file>index.xhtml</welcome-file>
</welcome-file-list>
<!-- Server error page -->
<error-page>
<error-code>500</error-code>
<location>/error.xhtml</location>
</error-page>
<!-- File not found page -->
<error-page>
<error-code>404</error-code>
<location>/resourceNotFound.xhtml</location>
</error-page>
<!-- Unathorised pages -->
<error-page>
<error-code>401</error-code>
<location>/notAuthorised.xhtml</location>
</error-page>
<error-page>
<error-code>403</error-code>
<location>/index.xhtml</location>
</error-page>
</web-app>
最佳答案
您的LocaleResolver
作为单独 Servlet 请求的一部分进行调用,该请求不通过标准 JSF Servlet 进行路由。发生这种情况时,Seam 拦截器不会被触发,双射也不会发生。
要解决这个问题,您需要通知 Seam 有关传入请求的信息,以便它正确设置其上下文(请注意,您需要关闭生命周期,因此无法返回获得的值):
ServletLifecycle.beginRequest(request);
String locale = LocaleSelector.instance().getLocaleString();
ServletLifecycle.endRequest();
return locale;
或者,您可以使用提供的 ContextualHttpServletRequest
类,它以更简洁的方式执行相同的操作(在 finally
block 中关闭生命周期等):
final String[] locale = new String[1];
new ContextualHttpServletRequest(request) {
@Override
public void process() throws ServletException, IOException {
// Do the things that need to access Seam contexts within this function:
locale[0] = LocaleSelector.instance().getLocaleString();
}
}.run();
return locale[0];
关于java - SEAM localeSelector 未注入(inject),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15974460/
我正在使用 SEAM 2 的 LocaleSelector 在我的应用程序中设置可以正常工作的区域设置 我还使用 JAWR 的 i18n 消息生成器使消息可用作 Javascript 变量,按照说明
我是一名优秀的程序员,十分优秀!