Primefaces 5.3 JSF 2.1 glassfish 4.0
我知道有很多方法可以发送一个选定的行,但我使用的是f:属性我之前已经使用了很多次但由于某种原因它给了我一个javax.servlet.ServletException:ng.requests.model。请求无法强制转换为java.lang.String
支持豆
@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 :(得分:2)
'form'是一个现有的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");