Jsf 拾取列表面不工作

Jsf 拾取列表面不工作,jsf,primefaces,converter,picklist,Jsf,Primefaces,Converter,Picklist,我尝试使用Primefaces的pickList组件。我的转换器不能正常工作,我不知道为什么 这是我的ManagedBean: @ManagedBean(name = "comMB") @SessionScoped public class TeamCompetitionBean implements Serializable { /** * */ private static final long serialVersionUID = 1L; p

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

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

@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());
        }
    }

}
@facescoverter(forClass=Team.class,value=“teamConverter”)
公共类TeamConverter实现转换器{
团队;
公共对象getAsObject(FacesContext FacesContext、UIComponent组件、,
字符串值){
System.out.println(“hello对象”);
if(value==null | | value.length()==0){
返回null;
}
ApplicationContext ctx=面上下文
.getWebApplicationContext(FacesContext.getCurrentInstance());
TeamBean控制器=(TeamBean)ctx.getBean(“teamMB”);
List liststagiaire=controller.getTeamList();
对于(int i=0;i
最后这是我的XHTML页面:

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

您的问题来自这一行(在您的班级TeamConverter):

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

if (team.getIdTeam().intValue() == getKey(value).intValue()) {
你在班上有同样的问题团队

if (team.getIdTeam() == getKey(value)) {
return (this.idTeam == f.getIdTeam());
应替换为:

return (this.idTeam.intValue() == f.getIdTeam().intValue());
不相关:

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

getKey(value)  // this

Integer.valueOf(value)  // by this


此外,在您的视图中,您应该将
itemlab=“#{team}”
替换为
itemlab=“#{team.teamName}”

什么是“我的转换器工作不正常”?没有人打电话?它是否返回错误的值?
List teamNameList
何时初始化?我看不出来。与问题无关,但您可以替换
String I=getStringKey(o.getIdTeam())
String i=o.getIdTeam().toString()teamserviceinpl
类。我已经按照你的建议编辑了我的代码……但问题仍然没有解决。这是堆栈跟踪中的异常:java.lang.IllegalArgumentException:object com.arobase.service.Impl。TeamServiceImpl@58e99405类型为com.sun.proxy.$Proxy863;预期类型:com.arobase.model.Team,位于com.arobase.managed.bean.TeamConverter.getAsString(TeamConverter.java:79)@用户2436180您是否尝试删除团队验证的
实例?当我删除它时,异常变成:java.lang.ClassCastException:com.sun.proxy。$Proxy176无法在com.arobase.managed.bean.TeamConverter.getAsString(TeamConverter.java:73)处强制转换到com.arobase.model.Team
getKey(value)  // this

Integer.valueOf(value)  // by this
getStringKey(o.getIdTeam()) // this

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