2016-12-13 82 views
0

Primefaces 5.3 JSF 2.1的GlassFish 4.0採用F發送所選行:屬性賦予ClassCastException異常

我知道有發送所選行的方法很多,但我使用F:屬性我已經使用了很多很多次,但對於由於某種原因,它給了我一個javax.servlet.ServletException:ng.requests.model.Request不能轉換爲java.lang.String

支持bean

@Named(value = "rtBean") 
@ViewScoped 
public class BeanRequests implements Serializable{ 

    /** 
    * 
    */ 
    private static final long serialVersionUID = 1L; 
    @Inject 
    ManagerRequests rtm; 
    private StreamedContent file; 


    public List<Request> getRequests(){ 

     return rtm.findAll(); 

    } 

    public void buttonAction(ActionEvent actionEvent) throws IOException { 

     Request form=(Request) actionEvent.getComponent().getAttributes().get("form");  
     Requestattachment ra=form.getRequestattachment(); 

     InputStream stream = new ByteArrayInputStream(ra.getAttachment()); 
     //System.out.println(ra.getRaid()); 
     file = new DefaultStreamedContent(stream,"application/pdf",form.getTrackingno()+".pdf"); 

    } 

    public StreamedContent getFile() { 
     return file; 
    } 

    public void setFile(StreamedContent file) { 
     this.file = file; 
    } 



} 

視圖

<!DOCTYPE html> 
<html lang="en" 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>Request Form Records</title> 
    <f:facet name="first"> 
     <meta http-equiv="X-UA-Compatible" content="IE=edge" /> 
    </f:facet> 
</h:head> 
<h:body styleClass="body"> 
    <h:form> 
<p:growl id="growl" showDetail="true" life="2000"/> 
     <p:dataTable value="#{rtBean.getRequests()}" var="rf" 
     id="tb1" paginator="true" rows="50" style="font-size:70%" 
     > 

      <p:column headerText="Attachment" exportable="false"> 
      <p:commandButton icon="ui-icon-arrowthickstop-1-s" disabled="#{rf.attachmentAvailable()}" onclick="PrimeFaces.monitorDownload(showStatus, hideStatus)" actionListener="#{rtBean.buttonAction}"> 
         <f:attribute name="form" value="#{rf}"></f:attribute> 

         <p:fileDownload value="#{rtBean.file}"/> 
       </p:commandButton> 
</p:column> 
     </p:dataTable> 

    </h:form> 

</h:body> 
</html> 

模型

package ng.requests.model; 

import java.io.Serializable; 

import javax.persistence.*; 

import java.util.Date; 


/** 
* The persistent class for the request database table. 
* 
*/ 
@Entity 
@NamedQuery(name="Request.findAll", query="SELECT r FROM Request r") 
public class Request implements Serializable { 
    private static final long serialVersionUID = 1L; 

    @Id 
    @GeneratedValue(strategy=GenerationType.IDENTITY) 
    private int idrequest; 

    @Temporal(TemporalType.TIMESTAMP) 
    private Date date; 

    @Temporal(TemporalType.TIMESTAMP) 
    private Date expdate; 

    private String requester; 

    private String status; 

    //bi-directional many-to-one association to User 
    @ManyToOne 
    @JoinColumn(name="userid") 
    private User user; 

    //bi-directional one-to-one association to Requestattachment 
    @OneToOne(fetch=FetchType.LAZY, cascade=CascadeType.PERSIST) 
    @JoinColumn(name="raid") 
    private Requestattachment requestattachment; 

    @Transient 
    private String trackingno; 

    public Request() { 
    } 

    public int getIdrequest() { 
     return this.idrequest; 
    } 

    public void setIdrequest(int idrequest) { 
     this.idrequest = idrequest; 
    } 

    public Date getDate() { 
     return this.date; 
    } 

    public void setDate(Date date) { 
     this.date = date; 
    } 

    public Date getExpdate() { 
     return this.expdate; 
    } 

    public void setExpdate(Date expdate) { 
     this.expdate = expdate; 
    } 

    public String getRequester() { 
     return this.requester; 
    } 

    public void setRequester(String requester) { 
     this.requester = requester; 
    } 

    public String getStatus() { 
     return this.status; 
    } 

    public void setStatus(String status) { 
     this.status = status; 
    } 

    public User getUser() { 
     return this.user; 
    } 

    public void setUser(User user) { 
     this.user = user; 
    } 

    public Requestattachment getRequestattachment() { 
     return this.requestattachment; 
    } 

    public void setRequestattachment(Requestattachment requestattachment) { 
     this.requestattachment = requestattachment; 
    } 

    public String getTrackingno() { 
     trackingno="TCD-"+getIdrequest(); 
     return trackingno; 
    } 

    public void setTrackingno(String trackingno) { 
     this.trackingno = trackingno; 
    } 



    public Boolean attachmentAvailable(){ 
     if(requestattachment==null){ 
      return true; 
     } 
     else 
      return false; 
    } 

    } 

例外

2016-12-13T18:58:45.147+0300|Severe: Error Rendering View[/requests.xhtml] 
java.lang.ClassCastException: ng.requests.model.Request cannot be cast to java.lang.String 
    at org.primefaces.component.commandbutton.CommandButton.getForm(CommandButton.java:254) 
    at org.primefaces.renderkit.CoreRenderer.buildAjaxRequest(CoreRenderer.java:362) 
    at org.primefaces.component.commandbutton.CommandButtonRenderer.buildRequest(CommandButtonRenderer.java:123) 
    at org.primefaces.component.commandbutton.CommandButtonRenderer.encodeMarkup(CommandButtonRenderer.java:65) 
    at org.primefaces.component.commandbutton.CommandButtonRenderer.encodeEnd(CommandButtonRenderer.java:54) 
    at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:924) 
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1863) 
    at org.primefaces.component.column.Column.renderChildren(Column.java:336) 
    at org.primefaces.component.datatable.DataTableRenderer.encodeCell(DataTableRenderer.java:1022) 
    at org.primefaces.component.datatable.DataTableRenderer.encodeRow(DataTableRenderer.java:970) 
    at org.primefaces.component.datatable.DataTableRenderer.encodeRows(DataTableRenderer.java:878) 
    at org.primefaces.component.datatable.DataTableRenderer.encodeTbody(DataTableRenderer.java:825) 
    at org.primefaces.component.datatable.DataTableRenderer.encodeTbody(DataTableRenderer.java:788) 
    at org.primefaces.component.datatable.DataTableRenderer.encodeRegularTable(DataTableRenderer.java:281) 
    at org.primefaces.component.datatable.DataTableRenderer.encodeMarkup(DataTableRenderer.java:243) 
    at org.primefaces.component.datatable.DataTableRenderer.encodeEnd(DataTableRenderer.java:85) 
    at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:924) 
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1863) 
    at javax.faces.render.Renderer.encodeChildren(Renderer.java:176) 
    at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:894) 
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1856) 
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1859) 
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1859) 
    at com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:443) 
    at com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:131) 
    at javax.faces.application.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:337) 
    at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:120) 
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101) 
    at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:219) 
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:647) 
    at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682) 
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:318) 
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160) 
    at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734) 
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673) 
    at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99) 
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174) 
    at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:357) 
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:260) 
    at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:188) 
    at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:191) 
    at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:168) 
    at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:189) 
    at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119) 
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:288) 
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:206) 
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:136) 
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:114) 
    at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77) 
    at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:838) 
    at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:113) 
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:115) 
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:55) 
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:135) 
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:564) 
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:544) 
    at java.lang.Thread.run(Thread.java:744) 

2016-12-13T18:58:45.150+0300|Warning: StandardWrapperValve[Faces Servlet]: Servlet.service() for servlet Faces Servlet threw exception 
java.lang.ClassCastException: ng.requests.model.Request cannot be cast to java.lang.String 
    at org.primefaces.component.commandbutton.CommandButton.getForm(CommandButton.java:254) 
    at org.primefaces.renderkit.CoreRenderer.buildAjaxRequest(CoreRenderer.java:362) 
    at org.primefaces.component.commandbutton.CommandButtonRenderer.buildRequest(CommandButtonRenderer.java:123) 
    at org.primefaces.component.commandbutton.CommandButtonRenderer.encodeMarkup(CommandButtonRenderer.java:65) 
    at org.primefaces.component.commandbutton.CommandButtonRenderer.encodeEnd(CommandButtonRenderer.java:54) 
    at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:924) 
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1863) 
    at org.primefaces.component.column.Column.renderChildren(Column.java:336) 
    at org.primefaces.component.datatable.DataTableRenderer.encodeCell(DataTableRenderer.java:1022) 
    at org.primefaces.component.datatable.DataTableRenderer.encodeRow(DataTableRenderer.java:970) 
    at org.primefaces.component.datatable.DataTableRenderer.encodeRows(DataTableRenderer.java:878) 
    at org.primefaces.component.datatable.DataTableRenderer.encodeTbody(DataTableRenderer.java:825) 
    at org.primefaces.component.datatable.DataTableRenderer.encodeTbody(DataTableRenderer.java:788) 
    at org.primefaces.component.datatable.DataTableRenderer.encodeRegularTable(DataTableRenderer.java:281) 
    at org.primefaces.component.datatable.DataTableRenderer.encodeMarkup(DataTableRenderer.java:243) 
    at org.primefaces.component.datatable.DataTableRenderer.encodeEnd(DataTableRenderer.java:85) 
    at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:924) 
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1863) 
    at javax.faces.render.Renderer.encodeChildren(Renderer.java:176) 
    at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:894) 
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1856) 
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1859) 
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1859) 
    at com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:443) 
    at com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:131) 
    at javax.faces.application.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:337) 
    at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:120) 
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101) 
    at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:219) 
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:647) 
    at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682) 
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:318) 
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160) 
    at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734) 
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673) 
    at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99) 
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174) 
    at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:357) 
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:260) 
    at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:188) 
    at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:191) 
    at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:168) 
    at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:189) 
    at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119) 
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:288) 
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:206) 
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:136) 
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:114) 
    at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77) 
    at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:838) 
    at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:113) 
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:115) 
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:55) 
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:135) 
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:564) 
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:544) 
    at java.lang.Thread.run(Thread.java:744) 

根據建議here我試圖使用f:setPropertyActionListener但我不斷收到NullPointerException異常的形式

<p:column headerText="Attachment" exportable="false"> 
      <p:commandButton icon="ui-icon-arrowthickstop-1-s" disabled="#{rf.attachmentAvailable()}" onclick="PrimeFaces.monitorDownload(showStatus, hideStatus)" actionListener="#{rtBean.buttonAction}"> 
         <f:setPropertyActionListener target="#{rtBean.form}" value="#{rf}"/> 

         <p:fileDownload value="#{rtBean.file}"/> 
       </p:commandButton> 
</p:column> 

支持豆

package ng.requests.view; 




@Named(value = "rtBean") 
@ViewScoped 
public class BeanRequests implements Serializable{ 

    /** 
    * 
    */ 
    private static final long serialVersionUID = 1L; 
    @Inject 
    ManagerRequests rtm; 
    private StreamedContent file; 
    private Request form; 

    public List<Request> getRequests(){ 

     return rtm.findAll(); 

    } 

    public void buttonAction(ActionEvent actionEvent) throws IOException { 

     System.out.println("here "+form.getIdrequest()); 

     Requestattachment ra=form.getRequestattachment(); 

     InputStream stream = new ByteArrayInputStream(ra.getAttachment()); 
     //System.out.println(ra.getRaid()); 
     file = new DefaultStreamedContent(stream,"application/pdf",form.getTrackingno()+".pdf"); 






    } 

    public StreamedContent getFile() { 
     return file; 
    } 

    public void setFile(StreamedContent file) { 
     this.file = file; 
    } 

    public Request getForm() { 
     return form; 
    } 

    public void setForm(Request form) { 
     this.form = form; 
    } 

} 

我寧願使用f:屬性如我在以前所有的項目中使用它,但我無法弄清楚,爲什麼轉換異常

+0

嘗試重命名 '形' 到例如'myForm的'。可能是'for,'是某種'保留'屬性。 – Kukeltje

+0

@Kukeltje哇,它工作的感謝 – yahh

回答

2

「形式」是現有attribute of the p:commandButton(由BalusC作爲好心狀態)因此通過f:attribute傳遞它會使組件獲得其他內容(您的對象)超出預期(引用表單)。有效的做法是

<p:commandButton form="#{rf}"... /> 

因此拋出ClasscastException。將屬性名稱更改爲其他名稱,例如'myform'將使其工作。因此,像

<f:attribute name="myform" value="#{rf}"/> 

,並在bean:

Request form=(Request) actionEvent.getComponent().getAttributes().get("myform");