FacesContext
和之间有什么区别ExternalContext
?我什么时候可以使用其中一个?什么有一个什么有另一个?
以下示例来自JavaServer Faces第3版:
<h:commandButton ... actionListener="#{rushmore.handleMouseClick}" />
Run Code Online (Sandbox Code Playgroud)
支持豆:
public void handleMouseClick(ActionEvent e) {
FacesContext context = FacesContext.getCurrentInstance();
String clientId = e.getComponent().getClientId(context);
Map<String, String> requestParams = context.getExternalContext().getRequestParameterMap();
// ...
}
Run Code Online (Sandbox Code Playgroud)
为什么请求参数在ExternalContext
?什么是clientId
?它是在应用程序启动时由JSF生成的吗?
我试图将一些单元测试添加到JSF应用程序.此应用程序并非严重依赖任何最佳实践,因此许多服务方法使用FacesContext
从托管会话bean中提取数据,如下所示:
(这是在util类中)
public static Object getPageBean(String beanReference) {
FacesContext fc = FacesContext.getCurrentInstance();
VariableResolver vr = fc.getApplication().getVariableResolver();
return vr.resolveVariable(fc, beanReference);
}
Run Code Online (Sandbox Code Playgroud)
嘲笑这个最好的方法是什么?我正在使用groovy所以我还有一些选项来创建我通常无法创建的类.
如何在过滤器中检索FacesContext?
我跟着就如何检索下面的文章FacesContext
中Filter
:
http://ocpsoft.org/java/jsf-java/jsf-20-extension-development-accessing-facescontext-in-a-filter/
但问题是它无法使用Flash作用域.关注NPE:
java.lang.NullPointerException
at com.sun.faces.context.flash.ELFlash.loggingGetPhaseMapForWriting(ELFlash.java:751)
at com.sun.faces.context.flash.ELFlash.getPhaseMapForWriting(ELFlash.java:785)
at com.sun.faces.context.flash.ELFlash.put(ELFlash.java:392)
at com.sun.faces.context.flash.ELFlash.put(ELFlash.java:112)
Run Code Online (Sandbox Code Playgroud)
我想在我的过滤器中添加重定向并使用闪存范围来保存一些数据以及消息,这是无效的.
在过去的几天里,我一直在努力使用我的网络应用程序的登录部分.我已经到了能够在tomcat上使用JDBCRealm(通过从sql server数据库中读取用户)成功验证用户的程度.现在我想在用户的帐户被阻止时发送某种反馈,或者当凭据不正确时,这就是我现在被困住的地方.
我想用这个:
try {
request.login(request.getParameter("user"), request.getParameter("pass"));
} catch (ServletException se) {
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Wrong Username/Password combination"));
log(se.getMessage());
}
Run Code Online (Sandbox Code Playgroud)
但FacesContext.getCurrentInstance()始终返回null ..
在做了一些研究后,我发现请求必须来自位于/ faces中的页面,以便调用FacesServlet并初始化FacesContext(至少这是我所理解的).
所以我将登录页面移动到Web Pages文件夹中名为faces的新文件夹.但现在每次我尝试调用login.xhtml页面时,都会收到此错误:
/login.xhtml Not Found in ExternalContext as a Resource
Run Code Online (Sandbox Code Playgroud)
这是堆栈跟踪:
com.sun.faces.context.FacesFileNotFoundException: /login.xhtml Not Found in ExternalContext as a Resource
at com.sun.faces.facelets.impl.DefaultFaceletFactory.resolveURL(DefaultFaceletFactory.java:232)
at com.sun.faces.facelets.impl.DefaultFaceletFactory.resolveURL(DefaultFaceletFactory.java:273)
at com.sun.faces.facelets.impl.DefaultFaceletFactory.getFacelet(DefaultFaceletFactory.java:201)
at com.sun.faces.application.view.FaceletViewHandlingStrategy.buildView(FaceletViewHandlingStrategy.java:764)
at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:100)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:410)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:224)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:169)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:929)
at …
Run Code Online (Sandbox Code Playgroud) 在经历javadoc时FacesContext
,我偶然发现了这句话
在调用release()方法之前,实例保持活动状态,之后不允许进一步引用此实例.当FacesContext实例处于活动状态时,除了执行此Web应用程序的servlet容器用于处理此请求的线程之外,不得从任何线程引用它.
这是否意味着FacesContext
永远不会进行垃圾收集,只有当current-webapplication停止(服务器停止)时才会销毁实例?
是FacesContext
以下单例模式?在这种情况下,当多个请求同时呈现响应时它将如何表现,因为它每次只提供一个请求?
我试图FacesContext
通过调用类FacesContext.getCurrentInstance()
的run()
方法来获取Runnable
,但它返回null
.
public class Task implements Runnable {
@Override
public void run() {
FacesContext context = FacesContext.getCurrentInstance(); // null!
// ...
}
}
Run Code Online (Sandbox Code Playgroud)
这是怎么造成的,我该如何解决?
我使用PrimeFaces 3.5.x和Mojarra JSF 2.1.x我想以编程方式访问和显示这两个库的版本.
我使用版本作为maven2属性,但我希望有一个更简单的方法来获得版本.我希望找到类似的东西:
Primeface.getVersion();
FacesContext.getCurrentInstance();
Run Code Online (Sandbox Code Playgroud)
基于JavaScript的解决方案也可以,因为我只想在状态页面上显示版本.
我不知道如何继续,但我总是得到新的JSF 1.2 Web应用程序的"java.lang.RuntimeException:找不到FacesContext".我确定这只是我找不到的一些配置.
第一个f:
或h:
标记出现异常.已经与重要<f:view>
的开始.
我的 index.jsp
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h"%>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<f:view>
<html>
<head>
<title>MyWebsite</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
</head>
<body>
<div>MyContent</div>
</body>
</html>
</f:view>
Run Code Online (Sandbox Code Playgroud)
我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">
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<context-param>
<param-name>javax.faces.DEFAULT_SUFFIX</param-name>
<param-value>.jsp</param-value>
</context-param>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.jsf</url-pattern>
</servlet-mapping> …
Run Code Online (Sandbox Code Playgroud) 当bean使用FacesContext或创建使用FacesContext的类的对象时,所有junit测试都会因NullPointerException而失败.请建议使用junit测试代码的方法.
@ManagedBean(name = "displayIssuesBean")
@SessionScoped
public class DisplayIssuesBean implements Serializable {
private static final long serialVersionUID = -3017739535520843880L;
private static final Logger LOGGER = Logger.getLogger(AddIssueBean.class);
private transient AddIssueDTO addNewIssueDTO = new AddIssueDTO();
private transient IDisplayIssues displayIssuesService = new DisplayIssuesService();
private transient List<String> projectNames = displayIssuesService
.getProjectNames(getEmailId());
private transient List<IssueDisplayDTO> issues = new ArrayList<IssueDisplayDTO>();
private transient List<IssueDisplayDTO> modifiedList = new ArrayList<IssueDisplayDTO>();
private String projectName;
private int projectId;
int firstId;
int secondId;
private transient IssueDisplayDTO backup1;
private transient IssueDisplayDTO backup2;
public …
Run Code Online (Sandbox Code Playgroud) facescontext ×10
jsf ×6
jsf-2 ×4
flash-scope ×1
groovy ×1
instance ×1
java ×1
jmockit ×1
jsp ×1
junit ×1
mocking ×1
null ×1
primefaces ×1
runnable ×1
tomcat ×1
unit-testing ×1