外行人说,当有人说某个对象是Java中的线程单一单例时,它意味着什么?我正在参加有关Java Server Faces的讲座,每次谈到FacesContext时 - 讲师总是提醒我们这是一个线程单一的单身人士.
在验证我的用户之后,我想在会话中将引用添加到当前登录用户.
这里我是如何在setCurrentUser方法中做到的:
FacesContext facesContext = FacesContext.getCurrentInstance();
HttpSession session = (HttpSession) facesContext.getExternalContext().getSession(true);
session.setAttribute("CURRENT_USER", currentUser);
Run Code Online (Sandbox Code Playgroud)
不幸的是,session引用始终为null!
或者,我尝试使用sessionMap
FacesContext facesContext = FacesContext.getCurrentInstance();
Map<String, Object> sessionMap = facesContext.getExternalContext().getSessionMap();
sessionMap.put("CURRENT_USER", currentUser);
Run Code Online (Sandbox Code Playgroud)
这个例外情况很糟糕:
java.lang.UnsupportedOperationException
at java.util.AbstractMap.put(AbstractMap.java:186)
(...)
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么 ?
我的控制器的完整代码
UserController.java
public class UserController implements Filter {
private FilterConfig fc;
private static final String CURRENT_USER = "CURRENT_USER";
public void init(FilterConfig filterConfig) throws ServletException {
fc = filterConfig;
log(">> Filter initialized");
}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException …Run Code Online (Sandbox Code Playgroud) 我的jsp页面是:Home.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="f" uri="http://java.sun.com/jsf/core"%>
<%@ taglib prefix="h" uri="http://java.sun.com/jsf/html"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<f:view>
<h:form>
<center>
<br><br><br><br>
<h:outputLink value="EmployeeHome.jsp">Employee Management</h:outputLink><br><br>
<h:outputLink value="ProjectHome.jsp">Project Management</h:outputLink><br><br>
</center>
</h:form>
</f:view>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
我的web.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>EmployeeMgmt</display-name>
<welcome-file-list>
<welcome-file>Home.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
</servlet-mapping>
</web-app>
Run Code Online (Sandbox Code Playgroud)
我得到的例外:
org.apache.jasper.JasperException: java.lang.RuntimeException: …Run Code Online (Sandbox Code Playgroud) 为了从资源包中检索字符串,我试图比较这两种方法的结果,下面是代码示例:
第一个例子:
baseName:资源包的完全限定名称(<base-name>in <resource-bundle>).
FacesContext context = FacesContext.getCurrentInstance();
Application app = context.getApplication();
ResourceBundle bundle = app.getResourceBundle(context, baseName);
Run Code Online (Sandbox Code Playgroud)
第二个例子:
varName:是表示<var></var>in 的String<resource-bundle>
FacesContext context = FacesContext.getCurrentInstance();
Locale locale = context .getViewRoot().getLocale();
ClassLoader loader = Thread.currentThread().getContextClassLoader();
ResourceBundle bundle = ResourceBundle.getBundle(varName, locale, loader);
Run Code Online (Sandbox Code Playgroud)
这两个例子有什么区别?如果没有区别,获得ResourceBundle(使用Application#getMessageBundle()或ResourceBundle#getBundle())的最佳做法是什么?
我有一个豆:
import javax.faces.context.FacesContext;
import javax.faces.view.ViewScoped;
...
@Named
@ViewScoped
public class SimpleBean implements Serializable
{
private static final long serialVersionUID = 1L;
@Inject
protected FacesContext facesContext;
...
}
Run Code Online (Sandbox Code Playgroud)
根据
https://arjan-tijms.omnifaces.org/p/jsf-23.html#1316
这应该与2.3一起工作...
部署到Wildfly 14时,结果是:
13:02:33,516 INFO [org.hibernate.dialect.Dialect] (ServerService Thread Pool -- 72) HHH000400: Using dialect: org.hibernate.dialect.MySQL8Dialect
13:02:33,563 INFO [org.hibernate.envers.boot.internal.EnversServiceImpl] (ServerService Thread Pool -- 72) Envers integration enabled? : true
13:02:34,344 INFO [org.hibernate.hql.internal.QueryTranslatorFactoryInitiator] (ServerService Thread Pool -- 72) HHH000397: Using ASTQueryTranslatorFactory
13:02:34,531 WARN [org.jboss.weld.Bootstrap] (MSC service thread 1-1) WELD-000146: BeforeBeanDiscovery.addAnnotatedType(AnnotatedType<?>) used for class …Run Code Online (Sandbox Code Playgroud) 有人能告诉我为什么语言环境芬兰语不起作用而其余的?
private static Map<String,Object> countries;
private static Locale finnishLocale = new Locale("fi", "FI");
static{
countries = new LinkedHashMap<String,Object>();
countries.put("English", Locale.ENGLISH); //label, value
countries.put("French", Locale.FRENCH);
countries.put("German", Locale.GERMAN);
countries.put("Finnish", finnishLocale); <---------- Not working!
}
public void setLocaleCode(String localeCode) {
this.localeCode = localeCode;
updateLocale(localeCode);
}
public void updateLocale(String newLocale){
String newLocaleValue = newLocale;
//loop country map to compare the locale code
for (Map.Entry<String, Object> entry : countries.entrySet()) {
if(entry.getValue().toString().equals(newLocaleValue)){
FacesContext.getCurrentInstance()
.getViewRoot().setLocale((Locale)entry.getValue());
}
}
}
Run Code Online (Sandbox Code Playgroud)
我的意思是我用 New-clause 创建的语言环境不起作用。我无法更好地解释它,因为我认为这样实现的语言环境与例如 Locale.GERMAN 的语言环境对象类似?我的软件除了更新语言环境 ja Faces 上下文之外什么都不做。没有例外。对不起,如果 q …
facescontext ×6
jsf ×4
java ×2
jsf-2 ×2
cdi ×1
faces-config ×1
jsf-2.3 ×1
locale ×1
localization ×1
session ×1
thread-local ×1
wildfly ×1