2013-12-12 72 views
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) 
+0

符合:它真的可以包含一个列表,因为它只是一个选择,它应该只接受字符串,所以p就是那个字符串 –

回答

1

,如果你考虑到以下概念你会明白你的问题:

  1. 根据HTTP协议的客户端 - 服务器通信是通过处理仅限纯字符串。所以与表单一起提交的所有值都以字符串形式发送。
  2. 您宣称您的收藏为List<Player>。因为它的值将通过运行时和基于反射的EL来设置。到EL的那一刻,你的收藏的工作类型信息已经被删除,并且将是简单的List
  3. 鉴于此信息,EL收到一个字符串并尝试设计支持的模型的类型,但它不能提供此时的类型信息,因此它会写入从客户端接收的字符串。
  4. 您没有指定<f:selectItems>标签的itemValue属性,所以我认为它是在表单提交到其默认值后编写的,最有可能是p,toString()

如果你调试你的代码,看看提交了什么,你会发现你的列表实际上是由字符串填充的。现在您应该了解问题的原因以及抛出的异常,因为String类中确实没有名称属性。

至于解决办法基本上有两件事情,以保持工作的事情需要做:

  1. 指定itemValue<f:selectItems>的标签明确为itemValue="#{p}"告诉JSF期待您的播放器实例;
  2. 通过使用明确指定Converter,例如标签的converter属性。这可以通过提供您自己的@FacesConverter class来完成,或者,如果您不介意使用优秀的OmniFaces JSF实用程序库,只需使用它的SelectItemsConverter as converter="omnifaces.SelectItemsConverter"即可。这将使JSF能够将指定的类实例填充到您的支持列表中。
相关问题