2012-06-10 138 views
1

我想要得到的實體從selectOneMenu用於實例,所以我可以指定實體變量其他一些方法。但它指向空。實體對象無法正常工作

XHTML代碼

<h:selectOneMenu value="#{statusReport.projectDetID}" converter="ObjectStringConv" onchange="#{statusReport.retrieveReport()}" > 
       <f:selectItems value="#{statusReport.listOfProjectDetail}" 
        var="projectDetail" itemLabel="#{projectDetail.project}    #{projectDetail.startDate} - #{projectDetail.endDate}" 
        itemValue="#{projectDetail}" noSelectionValue="Select the Saved Project"/> 
      </h:selectOneMenu> 

statusReport豆

public class StatusReport implements Serializable { 


    private ProjectDetail projectDetID; 

    private List<ProjectDetail> listOfProjectDetail; 

    public List<ProjectDetail> getListOfProjectDetail() { 
     listOfProjectDetail = projectDetailFacade.findAll(); 
     return listOfProjectDetail; 
    } 

    public void setListOfProjectDetail(List<ProjectDetail> listOfProjectDetail) { 
     this.listOfProjectDetail = listOfProjectDetail; 
    } 
    public ProjectDetail getProjectDetID() { 
     return projectDetID; 
    } 

    public void setProjectDetID(ProjectDetail projectDetID) { 
     this.projectDetID = projectDetID; 
    } 


    public void retrieveReport(){ 

     System.out.println(" Processing ....."); 
     if (projectDetID == null) 
     { 
      System.out.println("The object from Select null"); 
     } 
     else 
     { 
     System.out.println("The object from Select menu" + projectDetID.toString()); 
     } 

     System.out.println("Generated Data:Completed"); 

}} 

ProjectDetail實體Bean

package com.jira.entity; 

import java.io.Serializable; 
import javax.persistence.*; 

import java.util.Date; 
import java.util.List; 


/** 
* The persistent class for the PROJECT_DETAIL database table. 
* 
*/ 
@Entity 
@Table(name="PROJECT_DETAIL",schema="weeklyrep") 

public class ProjectDetail implements Serializable { 
    private static final long serialVersionUID = 1L; 

    @Id 
    @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="projectdetail_seq") 
    @SequenceGenerator(name="projectdetail_seq",schema="weeklyrep",sequenceName="projectdetail_seq", allocationSize=1) 
    @Column(name="PDETAIL_ID") 
    private long pdetailId; 

    private Boolean completed; 

    @Temporal(TemporalType.DATE) 
    @Column(name="END_DATE") 
    private Date endDate; 

    private Long project; 

    @Temporal(TemporalType.DATE) 
    @Column(name="START_DATE") 
    private Date startDate; 

    //bi-directional many-to-one association to MajorEvent 
    @OneToMany(mappedBy="projectDetail",cascade=CascadeType.ALL) 
    private List<MajorEvent> majorEvents; 
    //bi-directional one-to-one association to ExecSummary 
    @OneToOne(mappedBy="projectDetailExec",cascade=CascadeType.ALL) 
    private ExecSummary execSummary; 
    public ProjectDetail() { 
    } 

    public long getPdetailId() { 
     return this.pdetailId; 
    } 

    public void setPdetailId(Long pdetailId) { 
     this.pdetailId = pdetailId; 
    } 

    public Boolean getCompleted() { 
     return this.completed; 
    } 

    public void setCompleted(Boolean completed) { 
     this.completed = completed; 
    } 

    public Date getEndDate() { 
     return this.endDate; 
    } 

    public void setEndDate(Date endDate) { 
     this.endDate = endDate; 
    } 

    public long getProject() { 
     return this.project; 
    } 

    public void setProject(long project) { 
     this.project = project; 
    } 

    public Date getStartDate() { 
     return this.startDate; 
    } 

    public void setStartDate(Date startDate) { 
     this.startDate = startDate; 
    } 

    public List<MajorEvent> getMajorEvents() { 
     return this.majorEvents; 
    } 

    public void setMajorEvents(List<MajorEvent> majorEvents) { 
     this.majorEvents = majorEvents; 
    } 

    public ExecSummary getExecSummary() { 
     return execSummary; 
    } 

    public void setExecSummary(ExecSummary execSummary) { 
     this.execSummary = execSummary; 
    } 


} 

轉換

我不知道它是否需要轉換器,但是我不't k現在如何編碼。

package com.weeklyreport.converters; 

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

import com.jira.entity.ProjectDetail; 


@FacesConverter(value="ObjectStringConv") 
public class ObjectStringConv implements Converter { 

    @Override 
    public Object getAsObject(FacesContext context, UIComponent component, String svalue) { 
     System.out.print("String version of object is:" + svalue); 
     return "test"; 
    } 

    @Override 
    public String getAsString(FacesContext context, UIComponent component, Object ovalue) { 

      return ovalue.toString(); 
     } 

    } 

請幫我弄清楚這一點。有沒有辦法像這樣得到實體對象的實例?

回答

4

您的轉換器需要這樣寫,以便它可以基於ProjectDetail的唯一標識符在ProjectDetailString之間進行轉換。通常實體有一個id。您需要使用此值作爲String值。這裏有一個例子開球沒有任何瑣碎的檢查,像空和的instanceof:

@Override 
public String getAsString(FacesContext context, UIComponent component, Object value) { 
    // Convert ProjectDetail to its unique String representation. 
    ProjectDetail projectDetail = (ProjectDetail) value; 
    String idAsString = String.valueOf(projectDetail.getId()) 
    return idAsString; 
} 

@Override 
public Object getAsObject(FacesContext context, UIComponent component, String value) { 
    // Convert unique String representation of ProjectDetail back to ProjectDetail object. 
    Long id = Long.valueOf(value); 
    ProjectDetail projectDetail = someProjectDetailService.find(id); 
    return projectDetail; 
} 

注意使用JSF轉換器(和驗證)的EJB需要一些兩輪牛車。另請參見How to inject @EJB, @PersistenceContext, @Inject, @Autowired, etc in @FacesConverter?

+0

你可以發表評論,我編輯你的答案 – VeerM

+0

公共對象的getAsObject(FacesContext的情況下,的UIComponent組件,字符串S值){ \t //轉換ProjectDetail的唯一的字符串表示回ProjectDetail對象。 \t Long id = Long.valueOf(svalue); \t ProjectDetailFacade projectDetailFacade = null; \t \t嘗試{ \t \t \t projectDetailFacade =(ProjectDetailFacade)新的InitialContext()查找( 「Java的:com.jira.sessionbean.ProjectDetailFacade」); \t \t}趕上(NamingException的發送){ \t \t \t // TODO自動生成的catch程序塊 \t \t \t e.printStackTrace(); \t \t} \t ProjectDetail projectDetail = projectDetailFacade.find(ID); \t return projectDetail; \t} – VeerM

+0

是用於注入EJB – VeerM