本文整理了Java中javax.faces.context.Flash
类的一些代码示例,展示了Flash
类的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Flash
类的具体详情如下:
包路径:javax.faces.context.Flash
类名称:Flash
[英]The Flash concept is taken from Ruby on Rails and provides a way to pass temporary objects between the user views generated by the faces lifecycle. As in Rails, anything one places in the flash will be exposed to the next view encountered by the same user session and then cleared out. It is important to note that “next view” may have the same view id as the previous view.
Implementation Requirements
The flash is a session scoped object that must be thread safe.
The implementation requirements will be described in terms of the runtime traversing the JSF lifecycle. The flash exposes a Map
interface over two logical maps. The choice of which logical map is accessed depends on the current faces lifecycle phase. One logical map is for the current traversal and the other is for the next traversal. During the execute portion of the lifecycle, all flash accesses are sent to the current traversal map. During the render portion of the lifecycle, all flash accesses are sent to the next traversal map. On the next traversal through the lifecycle, the implementation must ensure that the current traversal map is the next traversal map of the previous traversal. Here is an example for illustration purposes only.
Consider an initial request to the faces lifecycle
Traversal N, execute phase: skipped on initial request.
Traversal N, render phase: flash access goes to flash[N].
Traversal N+1, execute phase: flash access goes to flash[N].
Traversal N+1, render phase: flash access goes to flash[N+1].
The implementation must ensure the proper behaviour of the flash is preserved even in the case of a <navigation-case>
that contains a <redirect />
. The implementation must ensure the proper behavior of the flash is preserved even in the case of adjacent GET requests on the same session. This allows Faces applications to fully utilize the “Post/Redirect/Get” design pattern.
The implementation must allow the user to access the flash via the EL implicit object flash
and also via javax.faces.context.ExternalContext#getFlash. The implementation must ensure that the flash is usable from both JSP and from Facelets for JSF 2. In addition to exposing the Map
interface, there are several features exposed as methods on the Flash
itself. Each of these features may be accessed via the EL as well, as described in the javadocs.
EL Usage Example
First page
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:c="http://xmlns.jcp.org/jsp/jstl/core">
<!-- extra code removed -->
<c:set target="#{flash}" property="foo" value="fooValue" />
Next page
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html">
<!-- extra code removed -->
<h:outputText value="#{flash.foo}" /> will be "fooValue"
without the quotes.
The same usage syntax must be available in JSP.
Note that extra action must be taken when using the flash in concert with output components that cause the browser to issue a GET request when clicked, such as h:button
and h:link
. The following example illustrates one way to use the flash in such circumstances.
First page
<h:button id="nextButton" value="Next (button)" outcome="next.xhtml">
<f:param name="foo" value="bar"/>
</h:button>
Next page
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://xmlns.jcp.org/jsf/core"
xmlns:h="http://xmlns.jcp.org/jsf/html">
<f:metadata>
<f:viewParam name="foo" id="foo" value="#{flash.now.foo}" />
</f:metadata>
<head /><body>
foo = #{flash.foo}
</body>
</html>
Note that this example uses #{flash.now}
on the second page. This is because the value doesn't actuall enter the flash until the server is processing the GET request sent by the browser due to the button being clicked.
[中]Flash的概念取自RubyonRails,它提供了一种在faces生命周期生成的用户视图之间传递临时对象的方法。与Rails一样,flash中的任何内容都将暴露给同一用户会话遇到的下一个视图,然后清除。请务必注意,“下一个视图”可能与上一个视图具有相同的视图id。
实施要求
闪存是会话范围的对象,必须是线程安全的。
实现需求将根据穿越JSF生命周期的运行时来描述。闪存在两个逻辑映射上公开Map
接口。访问哪个逻辑映射的选择取决于当前的faces生命周期阶段。一个逻辑映射用于当前遍历,另一个用于下一次遍历。在生命周期的执行部分,所有闪存访问都被发送到当前遍历映射。在生命周期的渲染部分,所有闪存访问都会发送到下一个遍历贴图。在生命周期的下一次遍历中,实现必须确保当前遍历映射是上一次遍历的下一个遍历映射。这里的示例仅用于说明目的。
考虑对面部生命周期的初始请求
遍历N,执行阶段:在初始请求时跳过。
遍历N,渲染阶段:闪存访问转到闪存[N]。
遍历N+1,执行阶段:闪存访问转到闪存[N]。
遍历N+1,渲染阶段:闪存访问转到闪存[N+1]。
实现必须确保即使在<navigation-case>
包含<redirect />
的情况下,闪存也能保持正确的行为。该实现必须确保即使在同一会话上存在相邻GET请求的情况下,也能保持闪存的正确行为。这使得Faces应用程序能够充分利用“Post/Redirect/Get”设计模式。
实现必须允许用户通过EL implicit对象flash
和javax访问flash。面孔。上下文外部上下文#获取Flash。实现必须确保flash可以从JSP和faceletsforjsf2中使用。除了公开Map
接口之外,在Flash
本身上还有几个作为方法公开的功能。这些特性中的每一个都可以通过EL访问,如javadocs中所述。
EL使用示例
首页
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:c="http://xmlns.jcp.org/jsp/jstl/core">
<!-- extra code removed -->
<c:set target="#{flash}" property="foo" value="fooValue" />
下一页
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html">
<!-- extra code removed -->
<h:outputText value="#{flash.foo}" /> will be "fooValue"
without the quotes.
JSP中必须有相同的用法语法。
请注意,如果将flash与导致浏览器在单击时发出GET请求的输出组件(例如h:button
和h:link
)配合使用,则必须采取额外的操作。以下示例说明了在这种情况下使用闪存的一种方法。
首页
<h:button id="nextButton" value="Next (button)" outcome="next.xhtml">
<f:param name="foo" value="bar"/>
</h:button>
下一页
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://xmlns.jcp.org/jsf/core"
xmlns:h="http://xmlns.jcp.org/jsf/html">
<f:metadata>
<f:viewParam name="foo" id="foo" value="#{flash.now.foo}" />
</f:metadata>
<head /><body>
foo = #{flash.foo}
</body>
</html>
请注意,此示例在第二页上使用#{flash.now}
。这是因为在服务器处理由浏览器发送的GET请求之前,该值实际上不会进入闪存,因为该按钮被单击。
代码示例来源:origin: IQSS/dataverse
public static void addWarningMessage(String message) {
FacesContext.getCurrentInstance().getExternalContext().getFlash().put("warningMsg", message);
}
public void addMessage( FacesMessage.Severity s, String summary, String details ) {
代码示例来源:origin: com.sun.faces/jsf-api
/**
* <p class="changed_added_2_2">The default behavior of this method
* is to call {@link Flash#get(Object)} on the wrapped {@link Flash}
* object.</p>
*
* @since 2.2
*/
@Override
public Object get(Object key) {
return getWrapped().get(key);
}
代码示例来源:origin: org.codehaus.openxma/dsl-platform-jsf
public static void redirectToPage(String viewId) {
FacesContext.getCurrentInstance().getExternalContext().getFlash().setKeepMessages(true);
String requestContextPath = FacesContext.getCurrentInstance().getExternalContext().getRequestContextPath();
String fullRedirectUrl = requestContextPath + "/" + viewId;
try {
System.out.println("Redirecting to " + fullRedirectUrl);
FacesContext.getCurrentInstance().getExternalContext().redirect(fullRedirectUrl);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
代码示例来源:origin: org.apache.myfaces.extensions.cdi.bundles/myfaces-extcdi-bundle-jsf20
Flash flash = facesContext.getExternalContext().getFlash();
if(flash.containsKey(ContextNotActiveException.class.getName()))
if(facesContext.getViewRoot() != null)
viewId = facesContext.getViewRoot().getViewId();
exceptionQueuedEventIterator.remove();
Flash flash = facesContext.getExternalContext().getFlash();
flash.put(throwable.getClass().getName(), throwable);
flash.keep(throwable.getClass().getName());
代码示例来源:origin: org.apache.deltaspike.modules/deltaspike-jsf-module-impl
public static void saveFacesMessages(ExternalContext externalContext)
{
JsfModuleConfig jsfModuleConfig = BeanProvider.getContextualReference(JsfModuleConfig.class);
if (!jsfModuleConfig.isAlwaysKeepMessages())
{
return;
}
try
{
WindowMetaData windowMetaData = BeanProvider.getContextualReference(WindowMetaData.class);
Map<String, Object> requestMap = externalContext.getRequestMap();
@SuppressWarnings({ "unchecked" })
List<FacesMessageEntry> facesMessageEntryList =
(List<FacesMessageEntry>)requestMap.get(FacesMessageEntry.class.getName());
if (facesMessageEntryList == null)
{
facesMessageEntryList = new CopyOnWriteArrayList<FacesMessageEntry>();
}
windowMetaData.setFacesMessageEntryList(facesMessageEntryList);
}
catch (ContextNotActiveException e)
{
//TODO log it in case of project-stage development
//we can't handle it correctly -> delegate to the jsf-api (which has some restrictions esp. before v2.2)
FacesContext.getCurrentInstance().getExternalContext().getFlash().setKeepMessages(true);
}
}
代码示例来源:origin: org.apache.myfaces.extensions.cdi.bundles/myfaces-extcdi-bundle-jsf20
throws IOException
if(FacesContext.getCurrentInstance().getResponseComplete())
if(this.useWindowAwareUrlEncoding || this.useFallback ||
FacesContext.getCurrentInstance().getPartialViewContext().isPartialRequest())
externalContext.getRequestMap().put(WINDOW_CONTEXT_ID_PARAMETER_KEY, windowId);
externalContext.getFlash().keep(WINDOW_CONTEXT_ID_PARAMETER_KEY);
externalContext.redirect(url);
代码示例来源:origin: org.omnifaces/omnifaces
/**
* @see Faces#redirectPermanent(String, String...)
*/
public static void redirectPermanent(FacesContext context, String url, String... paramValues) {
ExternalContext externalContext = context.getExternalContext();
externalContext.getFlash().setRedirect(true); // MyFaces also requires this for a redirect in current request (which is incorrect).
externalContext.setResponseStatus(SC_MOVED_PERMANENTLY);
externalContext.setResponseHeader("Location", prepareRedirectURL(getRequest(context), url, paramValues));
externalContext.setResponseHeader("Connection", "close");
context.responseComplete();
}
代码示例来源:origin: omnifaces/omnifaces
/**
* @see Faces#redirect(String, String...)
*/
public static void redirect(FacesContext context, String url, String... paramValues) {
ExternalContext externalContext = context.getExternalContext();
externalContext.getFlash().setRedirect(true); // MyFaces also requires this for a redirect in current request (which is incorrect).
try {
externalContext.redirect(prepareRedirectURL(getRequest(context), url, paramValues));
}
catch (IOException e) {
throw new UncheckedIOException(e);
}
}
代码示例来源:origin: org.glassfish/jakarta.faces
Flash flash = context.getExternalContext().getFlash();
flash.doPrePhaseActions(context);
} catch (UnsupportedOperationException uoe) {
if (LOGGER.isLoggable(Level.FINE)) {
代码示例来源:origin: stackoverflow.com
FacesContext facesContext = FacesContext.getCurrentInstance();
Flash flash = getCurrentInstance().getExternalContext().getFlash();
flash.setKeepMessages(true);
facesContext.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "yourMessage", null));
return "namePage?faces-redirect=true";
代码示例来源:origin: com.sun.faces/jsf-impl
private void doLastPhaseActions(FacesContext context,
boolean outgoingResponseIsRedirect) {
Map<Object, Object> attrs = context.getAttributes();
try {
attrs.put(ELFlash.ACT_AS_DO_LAST_PHASE_ACTIONS, outgoingResponseIsRedirect);
getFlash().doPostPhaseActions(context);
} finally {
attrs.remove(ELFlash.ACT_AS_DO_LAST_PHASE_ACTIONS);
}
}
代码示例来源:origin: org.apache.myfaces.extensions.cdi.bundles/myfaces-extcdi-bundle-jsf20
/**
* {@inheritDoc}
*/
@Override
public String restoreWindowId(ExternalContext externalContext)
{
if(this.useWindowAwareUrlEncoding || this.useFallback)
{
return super.restoreWindowId(externalContext);
}
return (String)externalContext.getFlash().remove(WINDOW_CONTEXT_ID_PARAMETER_KEY);
}
}
代码示例来源:origin: javax/javaee-web-api
/**
* <p class="changed_added_2_2">The default behavior of this method
* is to call {@link Flash#put} on the wrapped
* {@link Flash} object.</p>
*
* @since 2.2
*/
@Override
public Object put(String key, Object value) {
return getWrapped().put(key, value);
}
代码示例来源:origin: javax/javaee-web-api
/**
* <p class="changed_added_2_2">The default behavior of this method
* is to call {@link Flash#setKeepMessages(boolean)} on the wrapped
* {@link Flash} object.</p>
*
* @since 2.2
*/
@Override
public void setKeepMessages(boolean newValue) {
getWrapped().setKeepMessages(newValue);
}
代码示例来源:origin: com.sun.faces/jsf-api
/**
* <p class="changed_added_2_2">The default behavior of this method
* is to call {@link Flash#keep(String)} on the wrapped {@link Flash}
* object.</p>
*
* @since 2.2
*/
@Override
public void keep(String key) {
getWrapped().keep(key);
}
代码示例来源:origin: com.sun.faces/jsf-api
/**
* <p class="changed_added_2_2">The default behavior of this method
* is to call {@link Flash#doPrePhaseActions(FacesContext)} on the
* wrapped {@link Flash} object.</p>
*
* @since 2.2
*/
@Override
public void doPrePhaseActions(FacesContext ctx) {
getWrapped().doPrePhaseActions(ctx);
}
代码示例来源:origin: javax/javaee-web-api
/**
* <p class="changed_added_2_2">The default behavior of this method
* is to call {@link Flash#containsKey(Object)} on the wrapped
* {@link Flash} object.</p>
*
* @since 2.2
*/
@Override
public boolean containsKey(Object key) {
return getWrapped().containsKey(key);
}
代码示例来源:origin: com.sun.faces/jsf-api
/**
* <p class="changed_added_2_2">The default behavior of this method
* is to call {@link Flash#keySet()} on the wrapped {@link Flash}
* object.</p>
*
* @since 2.2
*/
@Override
public Set<String> keySet() {
return getWrapped().keySet();
}
代码示例来源:origin: javax/javaee-web-api
/**
* <p class="changed_added_2_2">The default behavior of this method
* is to call {@link Flash#setRedirect(boolean)} on the wrapped
* {@link Flash} object.</p>
*
* @since 2.2
*/
@Override
public void setRedirect(boolean newValue) {
getWrapped().setRedirect(newValue);
}
代码示例来源:origin: org.apache.myfaces.core/myfaces-api
@Override
public void doPostPhaseActions(FacesContext context)
{
getWrapped().doPostPhaseActions(context);
}
内容来源于网络,如有侵权,请联系作者删除!