如何从 JSF 中的 managedBean 重新加载同一页面? [英] How can I reload the same page from a managedBean in JSF?
问题描述
我在 JSF 应用程序中实现登录,但重定向有问题.
Im implementing a login in a JSF application and have a problem with the redirection.
我想在应用程序的每个 xhtml 中提供登录表单,但在登录成功或失败后,我想让用户保持在点击登录时所在的页面.
I want to make available the login form in every xhtml in the app, but after the login success or fail I want to keep the user in the same page they were when clicked on login.
我曾尝试在 managedBean 中返回 null,但这不起作用,因为它不会刷新网页,我需要刷新页面才能使视图逻辑正常工作.
I have tried to return null in the managedBean methong but that doesnt work because it not refreshes the webPage, I need the page to be refreshed for the view logic to work.
这是登录表单:
<h:form id="loginForm" rendered="#{!loginBean.estaLogueado()}">
<p:panel header="#{msg.header_login}">
<h:outputLabel for="login" value="#{msg.login}"/>
<p:inputText id="login" value="#{loginBean.usuario}"></p:inputText><br/>
<h:outputLabel for="pwd" value="#{msg.password}"/>
<p:inputText id="pwd" type="password" value="#{loginBean.password}"></p:inputText><br/>
<p:commandButton action="#{loginBean.login()}" value="Login"/>
</p:panel>
</h:form>
<h:form id="logoutForm" rendered="#{loginBean.estaLogueado()}">
Bienvenido #{loginBean.nombreUsuario}!!<br/>
<p:commandButton action="#{loginBean.logout()}" value="Desconectar"/>
</h:form>
这是action属性中的方法:
And this is the method in the action attribute:
public String login(){
currentUser = gu.login(usuario, password);
return null;
}
有没有办法返回到用户登录的xhtml,而不是像login.xhtml"那样固定的xhtml??
There is a way to return to the xhtml where the user loged, not being a fixed xhtml like "login.xhtml"??
推荐答案
public void login() throws IOException {
// ...
ExternalContext ec = FacesContext.getCurrentInstance().getExternalContext();
ec.redirect(((HttpServletRequest) ec.getRequest()).getRequestURI());
}
这篇关于如何从 JSF 中的 managedBean 重新加载同一页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!