2012-08-26 50 views
0

我只是想找回在selectOneMenu用於列表 這裏所選用戶的登錄的代碼:在selectOneMenu用於列表檢索選定項的值

我的XHTML頁面:

<?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:f="http://java.sun.com/jsf/core"  
    xmlns:h="http://java.sun.com/jsf/html" 
    xmlns:ui="http://java.sun.com/jsf/facelets" 
    xmlns:p="http://primefaces.org/ui"> 

<h:head> 
    <title>JSF 2.0 Test</title> 
</h:head> 
<h:body> 
    <h3>JSF 2.0 Test</h3> 
    <h:form> 

    <br></br> 
<p:commandButton id="modalDialogButton" value="Modal" onclick="dlg2.show();" 
    type="button"/> 
<p:dialog id="modalDialog" header="Modal Dialog" widgetVar="dlg2" modal="true" 
    height="100"> 
    <h:outputText value="This is a Modal Dialog." /> 

     <p:selectOneMenu value="#{adminMB.selectedUser}" converter="userConverter" 
     effect="fade"> 
      <f:selectItem itemLabel="Choose Administor" /> 
      <f:selectItems value="#{adminMB.users}" var="user" itemValue="#{user}" 
      itemLabel="#{user.userLogin}" /> 
     </p:selectOneMenu> 

     <p:commandButton id="Add" value=" Add " actionListener="# 
     {adminMB.createProject}"></p:commandButton> 

     </p:dialog> 

    </h:form> 
</h:body> 
</html> 

的adminMB.createProject方法:

public void createProject(ActionEvent actionEvent){ 

    System.err.println("created project admin login :"+this.selectedUser.getUserLogin()); 
               } 

的UserConverter類:

package tn.talan.testFramework.converter; 

import java.util.ArrayList; 
import javax.faces.component.UIComponent; 
import javax.faces.context.FacesContext; 
import javax.faces.convert.Converter; 
import javax.faces.convert.FacesConverter; 

import org.objectweb.salome_tmf.data.User; 
import org.objectweb.salome_tmf.ihm.admin.AdministrationFinal; 

import tn.talan.testFramework.managedBean.AdminMB; 

@FacesConverter (value="userConverter")//(forClass= AdminMB.class) 

public class UserConverter implements Converter { 

@Override public Object getAsObject(FacesContext context, UIComponent component, 
    String value) { 

    User selectedUser = null; 
    ArrayList<User> usersList; 
    String userLogin; 
    AdministrationFinal administration=new AdministrationFinal(); 
    administration.onInit(); 
    usersList = administration.getAllUsers(); 
    if (usersList!=null){ 
    for(int i=0 ;i<usersList.size();i++) 
    { selectedUser=usersList.get(i); 
     userLogin=selectedUser.getLoginFromModel(); 

    System.err.println(userLogin); 
    if (userLogin==value) 
     return userLogin; 
    } 
    } 
    return null; 
} 

@Override 
public String getAsString(FacesContext context, UIComponent component, Object value)  
    { 
    if (value == null) return ""; 
    return ((User) value).getUserLogin(); 

} 

    } 

所以,當我點擊「添加」按鈕,AdminMB的Bean的createProject方法被調用,我得到這個錯誤:

錯誤輸出:

26 août 2012 13:58:52 javax.faces.event.MethodExpressionActionListener processAction 
GRAVE: Réception de 'java.lang.NullPointerException' lors de l'invocation du listener d'action '#{adminMB.createProject}' du composant 'Add' 
26 août 2012 13:58:52 javax.faces.event.MethodExpressionActionListener processAction 
GRAVE: java.lang.NullPointerException 
    at tn.talan.testFramework.managedBean.AdminMB.createProject(AdminMB.java:205) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) 
    at java.lang.reflect.Method.invoke(Unknown Source) 
    at org.apache.el.parser.AstValue.invoke(AstValue.java:264) 
    at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:278) 
at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:102) 
    at javax.faces.event.MethodExpressionActionListener.processAction(MethodExpressionActionListener.java:144) 
    at javax.faces.event.ActionEvent.processListener(ActionEvent.java:84) 
    at javax.faces.component.UIComponentBase.broadcast(UIComponentBase.java:773) 
    at javax.faces.component.UICommand.broadcast(UICommand.java:296) 
    at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:790) 
    at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1255) 
    at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:77) 
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:97) 
    at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:114) 
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:334) 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305) 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) 
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:225) 
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123) 
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472) 
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168) 
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98) 
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:927) 
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118) 
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407) 
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1001) 
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:579) 
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) 
    at java.lang.Thread.run(Unknown Source) 

26 août 2012 13:58:52 com.sun.faces.context.AjaxExceptionHandlerImpl log 
GRAVE: JSF1073 : javax.faces.event.AbortProcessingException intercepté durant le traitement de INVOKE_APPLICATION 5 : UIComponent-ClientId=j_idt8:Add, Message=/primeFacesDialog.xhtml @31,97 actionListener="#{adminMB.createProject}": java.lang.NullPointerException 
26 août 2012 13:58:52 com.sun.faces.context.AjaxExceptionHandlerImpl log 
GRAVE: /primeFacesDialog.xhtml @31,97 actionListener="#{adminMB.createProject}": java.lang.NullPointerException 
javax.faces.event.AbortProcessingException: /primeFacesDialog.xhtml @31,97 actionListener="#{adminMB.createProject}": java.lang.NullPointerException 
    at javax.faces.event.MethodExpressionActionListener.processAction(MethodExpressionActionListener.java:178) 
    at javax.faces.event.ActionEvent.processListener(ActionEvent.java:84) 
    at javax.faces.component.UIComponentBase.broadcast(UIComponentBase.java:773) 
    at javax.faces.component.UICommand.broadcast(UICommand.java:296) 
    at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:790) 
    at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1255) 
    at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:77) 
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:97) 
    at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:114) 
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:334) 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305) 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) 
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:225) 
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123) 
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472) 
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168) 
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98) 
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:927) 
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118) 
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407) 
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1001) 
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:579) 
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) 
    at java.lang.Thread.run(Unknown Source) 

回答

1

如果選擇的值被成功地轉換爲你在你的談話暗示上方,下一個可用的罪魁禍首是你的託管bean。具體來說,您的託管bean的範圍。如果你使用的是一個@RequestScoped bean,那麼你一定會遇到這樣的問題。將你的bean更改爲@ViewScoped,看看會發生什麼

1

public void createProject(){ 
    System.out.println(selectedUser}); 
} 
更換 public void createProject(ActionEvent actionEvent){

,並添加process="modalDialog"到您的按鈕

+0

謝謝你,但它爲選定的用戶渲染爲空值 –

+0

btw替換' Daniel

+0

中選擇一個用戶,它沒有看到選擇的項目,它不會在這裏顯示項目的值。我完全不明白問題在哪裏 –