如何显示成功消息,然后在超时(例如5秒)后将用户重定向到另一个页面?
我需要成功登录后的登录页面。我尝试了以下操作,我可以看到登录失败的警告消息,但不能看到登录成功的成功消息。它会立即显示目标页面。
public String check(){
if (username.equals("test") && password.equals("test")) {
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,"Sample info message", "PrimeFaces rocks!"));
return "Success";
}else{
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN,"Sample warn message", "Watch out for PrimeFaces!"));
return "Failure";
}
}
我正在使用Seam的PageFlow进行导航。
我有
<p:messages id="messages" showDetail="true" autoUpdate="true" closable="true" />
这是Flash的一个实用程序。而不是
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,"Sample info message", "PrimeFaces rocks!"));:
只需使用下面的代码
FacesContext facesContext = FacesContext.getCurrentInstance();
Flash flash = facesContext.getExternalContext().getFlash();
flash.setKeepMessages(true);
flash.setRedirect(true);
facesContext.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,"Sample info message", "PrimeFaces rocks!"));
首先,对于你发布的代码,你不会在重定向之前看到FacesMessage,你会在重定向之后看到它。但是,为了实现这一点,您还需要添加一个过滤器,因为在重定向时消息会丢失。这是您需要的过滤器的代码(不要忘记在web.xml中声明它):
public class MultiPageMessagesSupport implements PhaseListener {
private static final long serialVersionUID = 1250469273857785274L;
private static final String sessionToken = "MULTI_PAGE_MESSAGES_SUPPORT";
@Override
public PhaseId getPhaseId() {
return PhaseId.ANY_PHASE;
}
/*
* Check to see if we are "naturally" in the RENDER_RESPONSE phase. If we
* have arrived here and the response is already complete, then the page is
* not going to show up: don't display messages yet.
*/
@Override
public void beforePhase(final PhaseEvent event) {
FacesContext facesContext = event.getFacesContext();
int msg = this.saveMessages(facesContext);
if (PhaseId.RENDER_RESPONSE.equals(event.getPhaseId())) {
if (!facesContext.getResponseComplete()) {
this.restoreMessages(facesContext);
}
}
}
/*
* Save messages into the session after every phase.
*/
@Override
public void afterPhase(final PhaseEvent event) {
if (event.getPhaseId() == PhaseId.APPLY_REQUEST_VALUES ||
event.getPhaseId() == PhaseId.PROCESS_VALIDATIONS ||
event.getPhaseId() == PhaseId.INVOKE_APPLICATION) {
FacesContext facesContext = event.getFacesContext();
int msg = this.saveMessages(facesContext);
}
}
@SuppressWarnings("unchecked")
private int saveMessages(final FacesContext facesContext) {
List<FacesMessage> messages = new ArrayList<FacesMessage>();
for (Iterator<FacesMessage> iter = facesContext.getMessages(null); iter.hasNext();) {
messages.add(iter.next());
iter.remove();
}
if (messages.isEmpty()) {
return 0;
}
Map<String, Object> sessionMap = facesContext.getExternalContext().getSessionMap();
List<FacesMessage> existingMessages = (List<FacesMessage>) sessionMap.get(sessionToken);
if (existingMessages != null) {
existingMessages.addAll(messages);
} else {
sessionMap.put(sessionToken, messages);
}
return messages.size();
}
@SuppressWarnings("unchecked")
private int restoreMessages(final FacesContext facesContext) {
Map<String, Object> sessionMap = facesContext.getExternalContext().getSessionMap();
List<FacesMessage> messages = (List<FacesMessage>) sessionMap.remove(sessionToken);
if (messages == null) {
return 0;
}
int restoredCount = messages.size();
for (Object element : messages) {
facesContext.addMessage(null, (FacesMessage) element);
}
return restoredCount;
}
}
如果这对您不起作用,并且您需要在之前显示消息,那么您将不得不像下面这样做:使方法返回无效,通过ajax调用它,并在添加成功消息后调用一些javascript方法,该方法将等待几秒钟,然后进行重定向(可能通过编程单击重定向到下一页的隐藏按钮)。在我看来,这是不值得的麻烦,你只会延迟登录过程。无论如何,用户将知道登录成功,因为他将被重定向到主页(或您发送给他的任何页面)
编辑:当方法完成时,消息将显示在页面中,因此在托管bean方法中等待将不起作用。添加FacesMessage后,使用
RequestContext.getCurrentInstance().execute("waitAndRedirect()");
在你的xhtml中,你需要有一个类似这样的javascript函数:
function waitAndRedirect() {
setTimeout(function() {
hiddenButtonId.click();
}, 2000);
}
其中hiddenButtonId是重定向到主页并被隐藏(display:none)的p:按钮的ID
但是,这又是一个讨厌的方法,在我看来没有必要这样做,你只会延迟登录过程
你不能在web.xml中声明MultiPageMessagesSupport你必须在faces-config.xml中声明MultiPageMessagesSupport。运动的例子:
enter code here
<lifecycle>
<phase-listener>your.package.MultiPageMessagesSupport</phase-listener>
</lifecycle>