Im implementing a login in a JSF application and have a problem with the redirection.
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.
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.
This is the login form:
<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>
And this is the method in the action attribute:
public String login(){
currentUser = gu.login(usuario, password);
return null;
}
There is a way to return to the xhtml where the user loged, not being a fixed xhtml like "login.xhtml"??
Managed Bean is a regular Java Bean class registered with JSF. In other words, Managed Beans is a Java bean managed by JSF framework Managed bean contains the getter and setter methods, business logic, or even a backing bean Managed beans works as Model for UI component. Managed Bean can be accessed from JSF page.
Just put the desired logic in the constructor of the request scoped bean associated with the JSF page. public Bean () { // Do your stuff here. } Use @PostConstruct annotated method on a request or view scoped bean. It will be executed after construction and initialization/setting of all managed properties and injected dependencies.
@ManagedBean marks a bean to be a managed bean with the name specified in name attribute. If the name attribute is not specified, then the managed bean name will default to class name portion of the fully qualified class name. In our case, it would be helloWorld.
JSF 2.2 has a new feature which performs this task using viewAction. Show activity on this post. @PostConstruct is run ONCE in first when Bean Created. the solution is create a Unused property and Do your Action in Getter method of this property and add this property to your .xhtml file like this :
Just redirect to the request URI.
public void login() throws IOException {
// ...
ExternalContext ec = FacesContext.getCurrentInstance().getExternalContext();
ec.redirect(((HttpServletRequest) ec.getRequest()).getRequestURI());
}
As far as i am concern there is 2 ways for this purpose.
?faces-redirect=true
to return value of action.First solution.
<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" update=":loginForm :logoutForm"/>
</p:panel>
</h:form>
<h:form id="logoutForm" rendered="#{loginBean.estaLogueado()}">
Bienvenido #{loginBean.nombreUsuario}!!<br/>
<p:commandButton action="#{loginBean.logout()}" update=":loginForm :logoutForm" value="Desconectar"/>
</h:form>
the update attribute will update the components.
Second solution
Add ?faces-redirect=true
to your return value of action method for a real refresh
public String login(){
currentUser = gu.login(usuario, password);
return "login?faces-redirect=true";
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With