2014-10-28 90 views
1

我在login.xhtml下面的代碼:爲什麼commandButton actionListener不起作用?

<html xmlns="http://www.w3.org/1999/xhtml" 
xmlns:h="http://java.sun.com/jsf/html" 
     xmlns:f="http://java.sun.com/jsf/core" 
    xmlns:p="http://primefaces.org/ui"> 

<h:head><title>Login</title></h:head> 
<h:body> 
    <h:form> 
     <p:commandButton id="loginBtn" value="Login" type="submit" actionListener="#{userMB.login}"/> 
    </h:form> 
</h:body> 
</html> 

而且我有,我有以下豆:

@ManagedBean(name="userMB") 
@RequestScoped 
public class UsersManagedBean implements Serializable { 
    public void login(ActionEvent event) { 
     System.out.println("print here..."); 
    } 
} 

我沒有得到消息,因爲印刷的login()方法尚未調用。有什麼不對的嗎?

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_2_5.xsd" 
    id="WebApp_ID" 
    version="2.5">  
<context-param> 
    <param-name>javax.faces.STATE_SAVING_METHOD</param-name> 
    <param-value>client</param-value> 
</context-param> 
<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> *.xhtml</url-pattern> 
    </servlet-mapping> 
</web-app> 
+0

你把p:commandButton放在h:form裏面嗎?請顯示您的所有login.xhtml代碼。 – wittakarn 2014-10-28 02:54:54

回答

1

請確保您使用javax.faces.event.ActionEvent。

XHTML

<html xmlns="http://www.w3.org/1999/xhtml" 
     xmlns:h="http://java.sun.com/jsf/html" 
     xmlns:p="http://primefaces.org/ui"> 

    <h:head><title>Login</title></h:head> 
    <h:body> 
     <h:form> 
      <p:commandButton id="loginBtn" 
          value="Login" 
          type="submit" 
          actionListener="#{userMB.login}"/> 
     </h:form> 
    </h:body> 
</html> 

managedbean

import java.io.Serializable; 
import javax.faces.bean.ManagedBean; 
import javax.faces.bean.RequestScoped; 
import javax.faces.event.ActionEvent; 

@ManagedBean(name = "userMB") 
@RequestScoped 
public class UsersManagedBean implements Serializable { 

    public void login(ActionEvent event) { 
     System.out.println("print here..."); 
    } 
} 
+0

我仍然有問題沒有解決。 – user3377708 2014-10-28 03:28:16

+0

@ user3377708顯示你的web.xml。 – wittakarn 2014-10-28 03:32:18

+0

爲什麼用'ActionEvent event'的參數聲明登錄方法?它也應該在沒有參數的情況下工作。 – 2014-10-29 01:28:10

1

試試這個刪除type="submit"

<html xmlns="http://www.w3.org/1999/xhtml" 
xmlns:h="http://java.sun.com/jsf/html" 
     xmlns:f="http://java.sun.com/jsf/core" 
    xmlns:p="http://primefaces.org/ui"> 

<h:head><title>Login</title></h:head> 
<h:body> 
    <h:form> 
     <p:commandButton id="loginBtn" value="Login" action="#{userMB.login}"/> 
    </h:form> 
</h:body> 
</html> 

後您的ManagedBean應該是這樣的。

import java.io.Serializable; 
import javax.faces.bean.ManagedBean; 
import javax.faces.bean.RequestScoped; 
import javax.faces.event.ActionEvent; 

@ManagedBean(name = "userMB") 
@RequestScoped 
public class UsersManagedBean implements Serializable { 

    public void login() { 
     System.out.println("print here..."); 
    } 
} 
+0

已刪除,但仍然沒有辦法。 – user3377708 2014-10-28 04:11:08

+0

謝謝,但不起作用。 – user3377708 2014-10-28 04:35:39

1

我已將您的完整示例login.xhtml頁面複製到我的測試項目,它工作正常。我自己創建的託管bean。

您是否嘗試避免primefaces?使用標準的JSF實現,然後再試一次:

<h:commandButton id="loginBtn" value="Login" type="submit" actionListener="#{userMB.login}"/> 

通知的<h:而不是<p:commandButton

順便說一下:type="sumbit"是PrimeFaces中的默認設置,忽略或使用此屬性的值爲submit(PrimeFaces用戶指南5.1,第108頁)應該沒有任何區別。