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