如何显示成功消息,然后在超时后将用户重定向到另一个页面. 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" />
在登录页面上.
首先,使用您发布的代码,您将无法在重定向之前看到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; } }
如果这对您不起作用,并且您需要在之前显示消息,那么您将需要执行以下操作:使方法返回void,通过ajax调用它,并在添加成功消息后调用一些javascript方法这将等待几秒钟,然后进行重定向(可能通过编程方式单击重定向到下一页的隐藏按钮).
在我看来这不值得麻烦,你只会延迟登录过程.无论如何,用户将知道tlogin成功,因为他将重定向到主页(或您发送给他的任何页面)
编辑:
当方法完成时,消息将显示在页面中,因此在托管bean方法中等待将不起作用.添加FacesMessage后,使用
RequestContext.getCurrentInstance().execute("waitAndRedirect()");
在你的xhtml中,你需要有一个类似于这个的javascript函数:
function waitAndRedirect() { setTimeout(function() { hiddenButtonId.click(); }, 2000); }
其中hiddenButtonId是p:按钮的ID,它重定向到主页并被隐藏(显示:无)
但同样,这是一个讨厌的方法,在我看来没有必要这样做,你只会延迟登录过程.
翻译自:https://stackoverflow.com/questions/15809772/show-success-message-and-then-redirect-to-another-page-after-a-timeout-using-pag