Jsf 注销消息不存在';不显示
当我从应用程序注销时,它不会显示我的注销消息 我有一个输出文本试图在我的视图中显示消息(Jsf 注销消息不存在';不显示,jsf,session,redirect,message,Jsf,Session,Redirect,Message,当我从应用程序注销时,它不会显示我的注销消息 我有一个输出文本试图在我的视图中显示消息(): <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <ui:composition xml
):
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:o="http://omnifaces.org/ui"
xmlns:p="http://primefaces.org/ui"
xmlns:ui="http://java.sun.com/jsf/facelets"
template="/WEB-INF/templates/template.xhtml">
<ui:define name="content">
<o:form id="login" includeViewParams="true">
<h:outputText id="message" value="#{loginMB.message}" />
... Another components ...
</o:form>
</ui:define>
</ui:composition>
@ManagedBean
@SessionScoped
public class UserBB implements Serializable {
private String message;
public void logout() {
try {
message = "You have been logout successfully";
SecurityUtils.getSubject().logout();
Faces.invalidateSession();
Faces.redirect("login.xhtml");
} catch (Exception exception) {
... Another code ...
}
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
public void logout() {
SecurityUtils.getSubject().logout();
Messages.addFlashGlobalInfo("You have successfully log out");
Faces.invalidateSession();
Faces.redirect("login.xhtml?faces-redirect=true&lo=t");
}
<p:commandButton action="#{userBB.logout}" value="log out" />
Faces.redirect("login.xhtml?msg=You have been logged out sucessfully");
为什么它不显示我的注销消息
编辑:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:o="http://omnifaces.org/ui"
xmlns:p="http://primefaces.org/ui"
xmlns:ui="http://java.sun.com/jsf/facelets"
template="/WEB-INF/templates/template.xhtml">
<ui:define name="content">
<o:form id="login" includeViewParams="true">
<h:outputText id="message" value="#{loginMB.message}" />
... Another components ...
</o:form>
</ui:define>
</ui:composition>
@ManagedBean
@SessionScoped
public class UserBB implements Serializable {
private String message;
public void logout() {
try {
message = "You have been logout successfully";
SecurityUtils.getSubject().logout();
Faces.invalidateSession();
Faces.redirect("login.xhtml");
} catch (Exception exception) {
... Another code ...
}
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
public void logout() {
SecurityUtils.getSubject().logout();
Messages.addFlashGlobalInfo("You have successfully log out");
Faces.invalidateSession();
Faces.redirect("login.xhtml?faces-redirect=true&lo=t");
}
<p:commandButton action="#{userBB.logout}" value="log out" />
Faces.redirect("login.xhtml?msg=You have been logged out sucessfully");
注销方法:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:o="http://omnifaces.org/ui"
xmlns:p="http://primefaces.org/ui"
xmlns:ui="http://java.sun.com/jsf/facelets"
template="/WEB-INF/templates/template.xhtml">
<ui:define name="content">
<o:form id="login" includeViewParams="true">
<h:outputText id="message" value="#{loginMB.message}" />
... Another components ...
</o:form>
</ui:define>
</ui:composition>
@ManagedBean
@SessionScoped
public class UserBB implements Serializable {
private String message;
public void logout() {
try {
message = "You have been logout successfully";
SecurityUtils.getSubject().logout();
Faces.invalidateSession();
Faces.redirect("login.xhtml");
} catch (Exception exception) {
... Another code ...
}
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
public void logout() {
SecurityUtils.getSubject().logout();
Messages.addFlashGlobalInfo("You have successfully log out");
Faces.invalidateSession();
Faces.redirect("login.xhtml?faces-redirect=true&lo=t");
}
<p:commandButton action="#{userBB.logout}" value="log out" />
Faces.redirect("login.xhtml?msg=You have been logged out sucessfully");
注销按钮:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:o="http://omnifaces.org/ui"
xmlns:p="http://primefaces.org/ui"
xmlns:ui="http://java.sun.com/jsf/facelets"
template="/WEB-INF/templates/template.xhtml">
<ui:define name="content">
<o:form id="login" includeViewParams="true">
<h:outputText id="message" value="#{loginMB.message}" />
... Another components ...
</o:form>
</ui:define>
</ui:composition>
@ManagedBean
@SessionScoped
public class UserBB implements Serializable {
private String message;
public void logout() {
try {
message = "You have been logout successfully";
SecurityUtils.getSubject().logout();
Faces.invalidateSession();
Faces.redirect("login.xhtml");
} catch (Exception exception) {
... Another code ...
}
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
public void logout() {
SecurityUtils.getSubject().logout();
Messages.addFlashGlobalInfo("You have successfully log out");
Faces.invalidateSession();
Faces.redirect("login.xhtml?faces-redirect=true&lo=t");
}
<p:commandButton action="#{userBB.logout}" value="log out" />
Faces.redirect("login.xhtml?msg=You have been logged out sucessfully");
这将销毁会话
,由于bean是会话作用域,所有数据都将丢失
解决方案:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:o="http://omnifaces.org/ui"
xmlns:p="http://primefaces.org/ui"
xmlns:ui="http://java.sun.com/jsf/facelets"
template="/WEB-INF/templates/template.xhtml">
<ui:define name="content">
<o:form id="login" includeViewParams="true">
<h:outputText id="message" value="#{loginMB.message}" />
... Another components ...
</o:form>
</ui:define>
</ui:composition>
@ManagedBean
@SessionScoped
public class UserBB implements Serializable {
private String message;
public void logout() {
try {
message = "You have been logout successfully";
SecurityUtils.getSubject().logout();
Faces.invalidateSession();
Faces.redirect("login.xhtml");
} catch (Exception exception) {
... Another code ...
}
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
public void logout() {
SecurityUtils.getSubject().logout();
Messages.addFlashGlobalInfo("You have successfully log out");
Faces.invalidateSession();
Faces.redirect("login.xhtml?faces-redirect=true&lo=t");
}
<p:commandButton action="#{userBB.logout}" value="log out" />
Faces.redirect("login.xhtml?msg=You have been logged out sucessfully");
在您的xhtml中
<h:outputText id="message" value="#{param['msg']}"/>
如果不想在url中显示
创建另一个文件logout.xhtml
(实际上logout.xhtml将具有与login.xhtml相同的代码,只需添加所需的额外行即可)您还可以使用url参数隐藏mesage
Faces.redirect(“login.xhtml?Faces redirect=true&logoutMsg=true”);
和xhtml
为什么它不显示我的注销消息
因为您已经将其指定为会话范围bean的属性,然后使会话无效并发送重定向。会话的失效将破坏会话范围和其中存储的所有对象,包括会话范围的托管bean。此后的重定向将创建一个全新的请求和会话,所有会话范围的托管bean都将重新创建,所有属性都设置为默认值
这种方法有两个问题:
使用
而不是
显示消息
将消息存储在会话作用域中,而不是存储在会话作用域中
要解决您的问题,请使用
显示消息,并使用FacesContext#addMessage()
,或者使用OmniFaces的实用程序类将消息添加到faces上下文。您可以使用
仅显示全局消息(即带有null
客户端ID的消息)。您可以使用来告诉JSF将当前请求的所有消息存储在flash作用域中
因此,总的来说,这应该做到:
<h:messages id="messages" globalOnly="true" />
对于感兴趣的人来说,下面是使用“普通JSF”的情况:
重要的一点是。您至少需要2.1.27才能消除所有这些问题。您管理的bean的范围???会话范围。我刚刚编辑了我的问题OK。如何修复此问题?如何在url位置栏中隐藏邮件?我今天将进行这些更改。谢谢你,巴卢斯克。使用此解决方案,我遇到了以下问题:当会话自动关闭后需要登录时,下次登录时会出现注销消息。您是如何执行注销的?通过命令按钮/链接,对吗?您使用的是哪个JSF impl/版本?