PickList PrimeFaces无效

时间:2013-05-30 10:58:41

标签: jsf primefaces converter picklist

我尝试使用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>

1 个答案:

答案 0 :(得分: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());

不相关:

您不需要使用getKeygetStringKey,您可以像这样替换它们:

getKey(value)  // this

Integer.valueOf(value)  // by this

getStringKey(o.getIdTeam()) // this

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

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