1

我尝试使用 Primefaces 的 pickList 组件。我的转换器无法正常工作,我不知道为什么。

这是我的 ManagedBean:

@ManagedBean(name = "comMB")
@SessionScoped
public class TeamCompetitionBean implements Serializable {
    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    private DualListModel<Team> teams;
    List<Team> source;
    List<Team> source1;
    List<Team> target;

    @ManagedProperty("#{team}")
    private TeamServiceI teamService;

    List<String> teamNameList ;

    // public TeamCompetitionBean() {

    public DualListModel<Team> getTeams() {

        // Players
        teamNameList = new ArrayList<String>();
        source = new ArrayList<Team>();
        target = new ArrayList<Team>();

        source.addAll(getTeamService().getTeam());

        teams = new DualListModel<Team>(source, target);
        return teams;

    }

    public void setTeams(DualListModel<Team> teams) {
        this.teams = teams;
    }

    public void onTransfer(TransferEvent event) {
        StringBuilder builder = new StringBuilder();
        for (Object item : event.getItems()) {
            builder.append(((Team) item).getTeamName()).append("<br />");
        }

        FacesMessage msg = new FacesMessage();
        msg.setSeverity(FacesMessage.SEVERITY_INFO);
        msg.setSummary("Items Transferred");
        msg.setDetail(builder.toString());

        FacesContext.getCurrentInstance().addMessage(null, msg);
    }

    public TeamServiceI getTeamService() {
        return teamService;
    }

    public void setTeamService(TeamServiceI teamService) {
        this.teamService = teamService;
    }

    public List<Team> getSource() {

        return source;
    }

    public void setSource(List<Team> source) {
        this.source = source;
    }

    public List<Team> getTarget() {
        return target;
    }

    public void setTarget(List<Team> target) {
        this.target = target;
    }


    public void afficher(){
        System.out.println(target);
        System.out.println(source);
    }

}

这是我想在我的pickList中加载的实体类:

@Entity
@Table(name = "team", catalog = "competition_manager")
public class Team implements java.io.Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private Integer idTeam;
    private Stadium stadium;
    private League league;
    private String teamName;

// getters and setters

@Override
    public String toString() {
        return teamName.toString();
    }

     @Override
     public boolean equals(Object obj) {
      if (!(obj instanceof Team)) {
       return false;
       }
      Team f = (Team) obj;

       return (this.idTeam == f.getIdTeam());

    }

现在,这是我的自定义转换器:

@FacesConverter(forClass = Team.class, value = "teamConverter")
public class TeamConverter implements Converter {


    Team team;

    public Object getAsObject(FacesContext facesContext, UIComponent component,
            String value) {

        System.out.println("hello object");

        if (value == null || value.length() == 0) {
            return null;
        }
        ApplicationContext ctx = FacesContextUtils
                .getWebApplicationContext(FacesContext.getCurrentInstance());
        TeamBean controller = (TeamBean) ctx.getBean("teamMB");

        List<Team> liststagiaire = controller.getTeamList();

        for (int i = 0; i < liststagiaire.size(); i++)

        {
            team = liststagiaire.get(i);
            if (team.getIdTeam() == getKey(value)) {
                break;
            }

        }

        return team;
    }

    java.lang.Integer getKey(String value) {
        java.lang.Integer key;
        key = Integer.valueOf(value);
        return key;
    }

    String getStringKey(java.lang.Integer value) {
        StringBuffer sb = new StringBuffer();
        sb.append(value);
        return sb.toString();
    }

    public String getAsString(FacesContext facesContext, UIComponent component,
            Object object) {

        System.out.println("hello string");

        if (object == null) {
            System.out.println("hello string null");
            return null;
        }
        if (object instanceof Team) {
            System.out.println("hello string intance of");
            Team o = (Team) object;
            String i = getStringKey(o.getIdTeam());

            return i;
        } else {
            System.out.println("hello throw");
            throw new IllegalArgumentException("object " + object
                    + " is of type " + object.getClass().getName()
                    + "; expected type: " + Team.class.getName());
        }
    }

}

最后这是我的 XHTML 页面:

<p:pickList id="teamPickList" value="#{comMB.teams}" var="team"
            itemValue="#{team}" itemLabel="#{team}" converter="teamConverter">          
        </p:pickList>
4

1 回答 1

0

您的问题来自这一行(在您的类TeamConverter 中):

if (team.getIdTeam() == getKey(value)) {

你不能Integer像那样比较对象,因为这样做你是在比较reference。您应该将此行替换为

if (team.getIdTeam().intValue() == getKey(value).intValue()) {

您在班级团队中遇到了同样的问题:

return (this.idTeam == f.getIdTeam());

应替换为:

return (this.idTeam.intValue() == f.getIdTeam().intValue());

不相关的 :

您不需要使用getKeyand getStringKey,您可以像这样简单地替换它们:

getKey(value)  // this

Integer.valueOf(value)  // by this

getStringKey(o.getIdTeam()) // this

o.getIdTeam().toString() // by this

此外,您应该在您的视图中替换itemLabel="#{team}"为。itemLabel="#{team.teamName}"

于 2013-05-30T11:12:27.367 回答