Java 登录后如何传递到页面?
我试图通过login.xhtml传递到index.xhtml,它可以工作。问题是登录页面在我请求任何页面时都会出现。我不明白我该纠正什么。 这是login.xhtmlJava 登录后如何传递到页面?,java,jsf,Java,Jsf,我试图通过login.xhtml传递到index.xhtml,它可以工作。问题是登录页面在我请求任何页面时都会出现。我不明白我该纠正什么。 这是login.xhtml <h:panelGrid styleClass="panelGridCenter" columns="2" cellpadding="5"> <h:outputLabel for="email" value="Email" /> <p:inputText id="em
<h:panelGrid styleClass="panelGridCenter" columns="2" cellpadding="5">
<h:outputLabel for="email" value="Email" />
<p:inputText id="email" name="email" label="email"
required="true" value="#{loginBean.email}" requiredMessage="le champ username est obligatoire"/>
<h:outputLabel for="password" value="Mot de passe:" />
<p:password id="password" name="pw" label="Mot de passe"
required="true" value="#{loginBean.password}" requiredMessage="le champ mot de passe est obligatoire"/>
<f:facet name="footer">
<p:commandButton value="Login" action ="#{loginBean.login}">
</p:commandButton>
<h:outputLabel for="inscrit" value="Vous n’avez pas de compte ?" />
<p:commandLink id="ins" immediate="true" action="#{loginBean.register}" ajax="false">
<h:outputText value="Inscription" />
</p:commandLink>
</f:facet>
</h:panelGrid>
Util.java
public class Util {
public static HttpSession getSession() {
return (HttpSession)
FacesContext.
getCurrentInstance().
getExternalContext().
getSession(false);
}
public static HttpServletRequest getRequest() {
return (HttpServletRequest) FacesContext.
getCurrentInstance().
getExternalContext().getRequest();
}
public static String getUserName()
{
HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
return session.getAttribute("email").toString();
}
faces-config.xml
<navigation-rule>
<from-view-id>/login.xhtml</from-view-id>
<navigation-case>
<from-outcome>index</from-outcome>
<to-view-id>/index.xhtml</to-view-id>
</navigation-case>
</navigation-rule>
/login.xhtml
指数
/index.xhtml
您是否用
标记将代码括起来?为什么要在faces config
中使用导航规则
标记?使用命令按钮上的action
属性和托管bean方法上的return
值应该就足够了。如果我通过authentication加密代码也不起作用。即使我删除了导航规则,问题仍然存在。你知道如何让用户登录,直到他注销?
<navigation-rule>
<from-view-id>/login.xhtml</from-view-id>
<navigation-case>
<from-outcome>index</from-outcome>
<to-view-id>/index.xhtml</to-view-id>
</navigation-case>
</navigation-rule>