2013-05-17 281 views
0

我正在开发使用primefaces和jpa的jsf,并且我的屏幕没有重定向到主页面,但仍然存在于登录页面中而没有显示。jsf登录页面重定向错误

我的web.xml文件

<?xml version="1.0" encoding="UTF-8"?> 
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> 
    <display-name>Cation</display-name> 
    <welcome-file-list> 
    <welcome-file>login.xhtml</welcome-file> 
    </welcome-file-list> 
    <servlet> 
    <servlet-name>Faces Servlet</servlet-name> 
    <servlet-class>javax.faces.webapp.FacesServlet</servlet-class> 
    <load-on-startup>1</load-on-startup> 
    </servlet> 
    <servlet-mapping> 
    <servlet-name>Faces Servlet</servlet-name> 
    <url-pattern>/faces/*</url-pattern> 
    <url-pattern>*.xhtml</url-pattern> 
    </servlet-mapping> 
    <context-param> 
    <param-name>javax.servlet.jsp.jstl.fmt.localizationContext</param-name> 
    <param-value>resources.application</param-value> 
    </context-param> 
    <context-param> 
    <description>State saving method: 'client' or 'server' (=default). See JSF Specification 2.5.2</description> 
    <param-name>javax.faces.STATE_SAVING_METHOD</param-name> 
    <param-value>client</param-value> 
    </context-param> 
    <context-param> 
    <description> 
    This parameter tells MyFaces if javascript code should be allowed in 
    the rendered HTML output. 
    If javascript is allowed, command_link anchors will have javascript code 
    that submits the corresponding form. 
    If javascript is not allowed, the state saving info and nested parameters 
    will be added as url parameters. 
    Default is 'true'</description> 
    <param-name>org.apache.myfaces.ALLOW_JAVASCRIPT</param-name> 
    <param-value>true</param-value> 
    </context-param> 
    <context-param> 
    <description> 
    If true, rendered HTML code will be formatted, so that it is 'human-readable' 
    i.e. additional line separators and whitespace will be written, that do not 
    influence the HTML code. 
    Default is 'true'</description> 
    <param-name>org.apache.myfaces.PRETTY_HTML</param-name> 
    <param-value>true</param-value> 
    </context-param> 
    <context-param> 
    <param-name>org.apache.myfaces.DETECT_JAVASCRIPT</param-name> 
    <param-value>false</param-value> 
    </context-param> 
    <context-param> 
    <description> 
    If true, a javascript function will be rendered that is able to restore the 
    former vertical scroll on every request. Convenient feature if you have pages 
    with long lists and you do not want the browser page to always jump to the top 
    if you trigger a link or button action that stays on the same page. 
    Default is 'false' 
</description> 
    <param-name>org.apache.myfaces.AUTO_SCROLL</param-name> 
    <param-value>true</param-value> 
    </context-param> 
    <listener> 
    <listener-class>org.apache.myfaces.webapp.StartupServletContextListener</listener-class> 
    </listener> 
</web-app> 

我faces-config.xml文件

<?xml version='1.0' encoding='UTF-8'?> 
<faces-config 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_1_2.xsd" 
    version="1.2"> 
    <managed-bean> 
     <managed-bean-name>home</managed-bean-name> 
     <managed-bean-class>com.cation.action.LoginAction</managed-bean-class> 
     <managed-bean-scope>request</managed-bean-scope>  
    </managed-bean> 
    <navigation-rule> 
     <navigation-case> 
      <from-outcome>home_page</from-outcome> 
      <to-view-id>/pages/homePage.xhtml</to-view-id> 
     </navigation-case> 
    </navigation-rule> 
</faces-config> 

我login.xhtml

<html xmlns="http://www.w3c.org/1999/xhtml" 
    xmlns:f="http://java.sun.com/jsf/core" 
    xmlns:h="http://java.sun.com/jsf/html" 
    xmlns:p="http://primefaces.prime.com.tr/ui"> 
<h:head> 
    <link type="text/css" rel="stylesheet" href="themes/bluesky/skin.css" /> 
</h:head> 
<h:body> 
    <center> 
    <p:panel header="Login Form" style="width: 350;"> 
     <h:form> 
      <h:panelGrid columns="2" cellpadding="2"> 
       <h:outputLabel for="#{home.username}" value="UserName"/> 
       <h:inputText value="#{home.username}" label="UserName"></h:inputText> 
       <h:outputLabel for="#{home.password}" value="Password"/> 
       <h:inputSecret value="#{home.password}"></h:inputSecret> 
       <h:commandButton type="submit" value="Login" action="#{home.validateUser}"></h:commandButton> 
      </h:panelGrid> 
     </h:form> 
    </p:panel> 
    <div><h:messages ></h:messages></div> 
    </center> 
</h:body> 
</html> 

index.jsp文件

<jsp:forward page="login.xhtml"></jsp:forward> 

我的登录操作文件

package com.cation.action; 

import java.util.List; 

import javax.persistence.EntityManager; 
import javax.persistence.EntityManagerFactory; 
import javax.persistence.Persistence; 
import javax.persistence.Query; 

import logon.Users; 

public class LoginAction { 

    private String username; 

    private String password; 

    private static final String PERSISTENCE_UNIT_NAME = "Cation"; 

    private static EntityManagerFactory factory; 

    @SuppressWarnings("unchecked") 
    public String validateUser() throws Exception { 
     factory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME); 
     EntityManager em = factory.createEntityManager(); 
     // Read the existing entries and write to console 
     Query q = em.createQuery("SELECT u FROM Users u where u.Login='"+username+"'"); 
     List<Users> userList = q.getResultList(); 
     Users user = (Users) userList.get(0); 

     if(user == null){ 
      return "error"; 
     } 
     /*// Create new user 
     em.getTransaction().begin(); 
     Users user = new Users(); 
     user.setName("Tom Johnson"); 
     user.setLogin("tomj"); 
     user.setPassword("pass"); 
     em.persist(user); 
     em.getTransaction().commit(); 

     em.close();*/ 
     return "home_page"; 
    } 

    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; 
    } 
} 

当我使用的用户名和密码,它显示在行动成功,但它不重定向到主页在同一页面仍然存在没有任何显示和URL显示如登录的问题是本地主机:8080 /阳离子/ login.xhtml

谁能plz帮助我解决它

+0

您的Faces Servlet网址映射是/ faces/*。所以你应该调用页面localhost:8080/faces/login.xhtml在Faces Conntext中运行你的应用程序。此外,您应该有一个home_page.xhtml页面,因为您的操作会返回到该页面。 – erencan

回答

1
  1. 回报"home_page?facesRedirect=true"代替return "home_page";
  2. 我建议这样的登录方法:
public void login() throws IOException { 
    FacesContext  context; 
    HttpServletRequest request; 
    ExternalContext externalContext;   

    context = FacesContext.getCurrentInstance(); 
    request = (HttpServletRequest) context.getExternalContext().getRequest(); 
    externalContext = context.getExternalContext(); 

    try { 
     // try to login 
     request.login(username, password); 
     // login successful so remember logged user 
     loggedUser = userDao.findByName(username); 
     externalContext.redirect(requestedURI); 
    } catch (ServletException e) { 
     // Unknown login 
     context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, ejbUtils.getMsg("msgUnknownLogin"), null)); 
     loggedUser = null; 
    } 
    } 

我认为你在做什么,都不会影响用户有权查看受保护的页面。它看起来像测试数据库中现有的用户和密码。它适用于桌面(java se),但不适用于web(java ee)应用程序,因为ee用户可以通过它的地址访问页面,而不是通过菜单或按钮。

查找基于表单的登录示例。