1
我正在顯示一個數據庫中的足球運動員列表,用戶可以選擇並添加到ArrayList中。顯示所有球員工作正常,但不是爲選定的。當我詢問普通對象或其散列碼時,它會返回它,但只要我引用屬於Player實體的屬性,就會給出錯誤。任何人?JSF/JPA錯誤:類'java.lang.String'沒有屬性
支持bean
@Named
@SessionScoped
public class PlayerBean implements Serializable {
private static final long serializableUID = 1L;
@Inject
PlayerFacade facade;
private List<Player> allPlayers;
private List<Player> selectedPlayers;
public PlayerBean() {
}
public List<Player> getAllPlayers() {
return facade.findAll();
}
public List<Player> getSelectedPlayers() {
return selectedPlayers;
}
public void setSelectedPlayers(List<Player> selectedPlayers) {
this.selectedPlayers= selectedPlayers;
}
}
實體類
@Entity
@Table(name = "player")
@XmlRootElement
public class Player implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "PlayerID")
private Integer playerID;
@Size(max = 45)
@Column(name = "Name")
private String name;
@Size(max = 45)
@Column(name = "Position")
private String position;
// @Max(value=?) @Min(value=?)//if you know range of your decimal fields consider using these annotations to enforce field validation
@Column(name = "Weight")
private BigDecimal weight;
public Player() {
}
public Player(Integer playerID) {
this.playerID = playerID;
}
public Integer getPlayerID() {
return playerID;
}
public void setPlayerID(Integer playerID) {
this.playerID = playerID;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPosition() {
return position;
}
public void setPosition(String position) {
this.position = position;
}
public BigDecimal getWeight() {
return weight;
}
public void setWeight(BigDecimal weight) {
this.weight = weight;
}
@Override
public int hashCode() {
int hash = 0;
hash += (playerID != null ? playerID.hashCode() : 0);
return hash;
}
@Override
public boolean equals(Object object) {
// TODO: Warning - this method won't work in the case the id fields are not set
if (!(object instanceof Player)) {
return false;
}
Player other = (Player) object;
if ((this.playerID == null && other.playerID != null) || (this.playerID != null && !this.playerID.equals(other.playerID))) {
return false;
}
return true;
}
@Override
public String toString() {
return "playerID=" + playerID + " ]";
}
}
JSF頁面
<h:form id="allPlayers" style="text-align: center;" >
<h:selectManyCheckbox value="#{playerBean.selectedPlayers}">
<f:selectItems value="#{playerBean.allPlayers}" var="p" itemLabel="#{p.name} #{p.position} #{p.weight}" />
</h:selectManyCheckbox>
<h:commandButton value="Add to Team" action="team"/>
</h:form>
</p>
<p>
<h:form id="selectedPlayers" style="text-align: center;" >
<h:dataTable value="#{playerBean.selectedPlayers}" var="players">
<h:column>
#{players}
</h:column>
<h:column>
#{players.name}
</h:column>
</h:dataTable>
<h:commandButton value="Set Team" action="team"/>
</h:form>
錯誤消息
javax.el.ELException: /team.xhtml: The class 'java.lang.String' does not have the property 'name'.
at com.sun.faces.facelets.compiler.TextInstruction.write(TextInstruction.java:88)
at com.sun.faces.facelets.compiler.UIInstructions.encodeBegin(UIInstructions.java:82)
at com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.encodeRecursive(HtmlBasicRenderer.java:302)
at com.sun.faces.renderkit.html_basic.TableRenderer.renderRow(TableRenderer.java:385)
at com.sun.faces.renderkit.html_basic.TableRenderer.encodeChildren(TableRenderer.java:162)
at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:894)
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1856)
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:724)
符合:它真的可以包含一個列表,因爲它只是一個選擇,它應該只接受字符串,所以p就是那個字符串 –