1

我正在我的网站上写一个登录名,不幸的是我遇到了一些问题。

MenagedBean 用于记录:

@ManagedBean
@SessionScoped
public class LoginMB {

    private static final String PERSISTENCE_UNIT_NAME = "ejbPU";
    private static EntityManagerFactory factory;
    private String login;
    private String password;
    private User user;

--konstruktor, gettery i settery

    public String validate() {
        factory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);
        EntityManager em = factory.createEntityManager();
        Query q = em.createQuery("SELECT u FROM User u WHERE u.personalId = :login AND u.password = :password");
        q.setParameter("login", login);
        q.setParameter("password", password);
        try {
            user = (User) q.getSingleResult();
            if (login.equals(user.getPersonalId()) && password.equals(user.getPassword())) {
                return user.getRole();
            }
        } catch (Exception e) {
            System.out.println("Błąd: " + e.getMessage());
            return "failed";
        }
        return "failed";
    }

    public String logout() {
        FacesContext.getCurrentInstance().getExternalContext().invalidateSession();
        return "abcd";
    }
} 

筛选:

public class FilterLogin implements Filter{

    FilterConfig fc;

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        fc = filterConfig;
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.

        HttpServletRequest req = (HttpServletRequest) request;
        HttpServletResponse resp = (HttpServletResponse) response;
        HttpSession session = req.getSession(true);
        String pageRequested = req.getRequestURL().toString();

        if (session.getAttribute("loginMB") == null && !pageRequested.contains("/login.xhtml")) {
            resp.sendRedirect("/login.xhtml");
        } else {
            chain.doFilter(request, response);
        }  
    }

    @Override
    public void destroy() {
        //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

}

web.xml

<filter>
        <filter-name>filter</filter-name>
        <filter-class>pl.ePrzychodnia.filter.LoginFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>filter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping> 
    <session-config>
        <session-timeout>
            1
        </session-timeout>
    </session-config>

记录页面:

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core">
    <h:head>
        <title>Logowanie</title>
    </h:head>
    <h:body>
        <h1>Login</h1>
        <h:form>
            <h:panelGrid columns="3">
                <h:outputLabel value="Login"></h:outputLabel>
                <h:inputText id="login" value="#{loginMB.login}" label="Login"/>        
                <h:message for="login" style="color:red" />
                <h:outputLabel value="Haslo"></h:outputLabel>
                <h:inputSecret id="pass" value="#{loginMB.password}" label="Password"/>        
                <h:message for="pass" style="color:red" />
                <h:commandButton value="Zaloguj" action="#{loginMB.validate()}"></h:commandButton>
                <h:commandButton value="Załóż konto" action="#{userMB.createAccount()}"></h:commandButton>
            </h:panelGrid>
        </h:form>
    </h:body>
</html> 

当我尝试转到另一个页面时会话到期时,我收到一个错误:

发生错误:viewId:/adminPanel.xhtml - 无法恢复视图 /adminPanel.xhtml。

而是转到页面 login.xhtml。为什么?帮助别人?

4

1 回答 1

1

您需要处理 ViewExpiredException,如果您在会话超时后尝试单击任何内容,则会发生这种情况。JSF 将尝试恢复视图,因此您会收到异常

查看:如何在 JSF 2 中处理会话过期和 ViewExpiredException?

于 2013-06-06T17:48:23.380 回答