0

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)
4

2 に答える 2

1

上記の会話で示唆したように、選択した値が正常に変換されている場合、次の原因はマネージド Bean です。具体的には、マネージド Bean のスコープです。@RequestScopedBeanを使用している場合、このような問題に遭遇することは確実です。Bean を a に変更して、@ViewScoped何が起こるかを確認します

于 2012-09-04T03:53:17.203 に答える
1

public void createProject(ActionEvent actionEvent){と置き換えます

public void createProject(){
    System.out.println(selectedUser});
}

process="modalDialog"ボタンに追加します

于 2012-08-26T13:47:17.350 に答える