我是 jsf 的新手,使用用户自定义身份验证和浏览页面。当我提交表单时,我将转到下一页,但是当我在第二页上刷新用户浏览器时,它会将我带回到表单提交页面而不是同一页面。
我的第一页
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:rich="http://richfaces.org/rich"
xmlns:a4j="http://richfaces.org/a4j">
<h:head>
<title>Community</title>
</h:head>
<h:body>
<h:form id="login">
<h:panelGrid columns="2">
<rich:panel>
<f:facet name="header">
<h:outputText value="Login form" />
</f:facet>
<h:panelGrid columns="2" width="210">
<h:outputText value="Username:" />
<h:inputText label="username" id="username" value="#{loginMB.username}" required="true" requiredMessage="user name is requried">
<f:validateLength minimum="5" />
</h:inputText>
<h:outputText value="Password" />
<h:inputSecret label="password" id="password" value="#{loginMB.password}" requiredMessage="password is requried">
<f:validateLength minimum="8" maximum="16" />
</h:inputSecret>
<rich:notifyMessages stayTime="2000" nonblocking="true" />
<f:facet name="footer">
<a4j:commandButton value="Login" action="#{loginMB.outcome.toString()}" actionListener="#{loginMB.authUser()}"/>
</f:facet>
</h:panelGrid>
</rich:panel>
</h:panelGrid>
</h:form>
</h:body>
</html>
我的后盾
package com.jsf;
import com.ejb.UsersFacadeLocal;
import com.entity.Users;
import java.io.IOException;
import java.io.Serializable;
import java.util.List;
import java.util.Map;
import javax.annotation.ManagedBean;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.enterprise.context.SessionScoped;
import javax.faces.application.FacesMessage;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.faces.event.ComponentSystemEvent;
import javax.inject.Named;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
/**
*
* @author krishna teja
*/
@Named(value = "loginMB")
@ManagedBean
@SessionScoped
public class LoginMB implements Serializable {
private static final long serialVersionUID = 1L;
private Map<String, Object> sessionMap;
private ExternalContext externalContext;
@EJB
private UsersFacadeLocal usersFacade;
protected Users user;
protected List<Users> lusers;
protected String username;
protected String password;
protected String role;
protected boolean islogedin;
protected String outcome = "";
public LoginMB() {
}
@PostConstruct
public void init() {
if (this.usersFacade.findAll() != null) {
this.lusers = this.usersFacade.findAll();
}
}
public void authUser() {
FacesMessage message = null;
this.user = usersFacade.findByUserName(this.username);
if (null != user) {
if (this.username.equals(this.user.getUsername()) && this.password.equals(this.user.getPassword())) {
this.islogedin = true;
externalContext = FacesContext.getCurrentInstance().getExternalContext();
sessionMap = externalContext.getSessionMap();
sessionMap.put(username, user);
this.outcome = "user";
System.out.println(this.sessionMap.isEmpty()+" trying to print user");
if ("Admin".equals(this.username)) {
this.outcome = "admin";
}
}
} else {
message = new FacesMessage("The user name provided is registered please register if u are new user");
FacesContext.getCurrentInstance().addMessage(username, message);
}
}
public void verifyUseLogin(ComponentSystemEvent event) {
if (!this.islogedin) {
doRedirect();
}
}
private void doRedirect() {
try {
FacesContext context = FacesContext.getCurrentInstance();
context.getExternalContext().redirect("index.xhtml");
} catch (IOException e) {
e.printStackTrace();
}
}
public void logout() {
this.password = null;
this.username = null;
this.sessionMap.clear();
try{
HttpSession session = (HttpSession)FacesContext.getCurrentInstance().getExternalContext().getSession(false);
session.invalidate();
}catch(IllegalStateException e){
System.out.println("exception closing on already closed session");
e.printStackTrace();
}
doRedirect();
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public List<Users> getLusers() {
return lusers;
}
public void setLusers(List<Users> lusers) {
this.lusers = lusers;
}
public String getOutcome() {
return outcome;
}
public void setOutcome(String outcome) {
this.outcome = outcome;
}
}
提交页面后我的导航页面
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:a4j="http://richfaces.org/a4j"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:rich="http://richfaces.org/rich">
<h:head>
<title>this is admin page</title>
<f:metadata>
<f:event listener="#{loginMB.verifyUseLogin(event)}" type="preRenderView">
</f:event>
</f:metadata>
</h:head>
<h:body>
this is admin side page for test
</h:body>
</html>
面对上下文
<faces-config version="2.1"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_2_1.xsd">
<navigation-rule>
<from-view-id>/index.xhtml</from-view-id>
<navigation-case>
<from-outcome>user</from-outcome>
<to-view-id>/user.xhtml</to-view-id>
</navigation-case>
<navigation-case>
<from-outcome>admin</from-outcome>
<to-view-id>/admin.xhtml</to-view-id>
</navigation-case>
<navigation-case>
<from-outcome>register</from-outcome>
<to-view-id>/register.xhtml</to-view-id>
</navigation-case>
</navigation-rule>
</faces-config>
我想在浏览器刷新后转到同一页面