selectOneMenu j_idt31的问题验证错误:值无效

时间:2013-05-29 22:16:38

标签: validation jsf

我使用SelectOneMenu,首先我选择位置,然后选择此位置的建筑物,然后选择建筑物中的区域和此区域中的子区域。前两个选择是好的,但当我采取建设选择没有区域显示,我有错误:j_idt31:selectedBld:验证错误:值无效。我不知道为什么; /

这是xhtml:

<h:form>
    <p:panelGrid columns="2" position="center top">
        <h:outputLabel for="selectedLoca" value="#{loc['location']}" />
        <p:selectOneMenu value="#{caretakerCautionBean.location}" id="selectedLoca" converter="#{locationConverter}">
            <f:selectItem itemLabel="" itemValue="#{null}" />
            <f:selectItems value="#{caretakerCautionBean.locationList}" var="loca" itemLabel="#{loca.name}" itemValue="#{loca}"/>
            <p:ajax update="selectedBld :messages" event="change" process="selectedLoca" listener="#{caretakerCautionBean.locationChanged}" /> 
        </p:selectOneMenu>
        <h:outputLabel for="selectedBld" value="#{loc['building']}" />
        <p:selectOneMenu value="#{caretakerCautionBean.building}" id="selectedBld" converter="#{buildingConverter}">
            <f:selectItem itemLabel="" itemValue="#{null}" />
            <f:selectItems value="#{caretakerCautionBean.buildingList}" var="bld" itemLabel="#{bld.name}" itemValue="#{bld}" />
            <p:ajax update="selectedZone :messages" event="change" process="selectedLoca selectedBld" listener="#{caretakerCautionBean.buildingChanged}" />
        </p:selectOneMenu>
        <h:outputLabel for="selectedZone" value="#{loc['zone']}" />
        <p:selectOneMenu value="#{caretakerCautionBean.zone}" id="selectedZone" converter="#{zoneConverter}">
            <f:selectItem itemLabel="" itemValue="#{null}" />
            <f:selectItems value="#{caretakerCautionBean.zoneList}" var="zone" itemLabel="#{zone.name}" itemValue="#{zone}" />
            <p:ajax update="selectedSubZone :messages" event="change" process="selectedLoca selectedBld selectedZone" listener="#{caretakerCautionBean.zoneChanged}" />
        </p:selectOneMenu>
        <h:outputLabel for="selectedSubZone" value="#{loc['cleaningPlanner.tableHeader.subzone']}" />
        <p:selectOneMenu value="#{caretakerCautionBean.subZone}" id="selectedSubZone" converter="#{subZoneConverter}">
            <f:selectItem itemLabel="" itemValue="#{null}" />
            <f:selectItems value="#{caretakerCautionBean.subZoneList}" var="subZone" itemLabel="#{subZone.name}" itemValue="#{subZone}" />
            <p:ajax update=":messages" event="change" process="selectedLoca selectedBld selectedZone selectedSubZone" listener="#{caretakerCautionBean.subZoneChanged}" />
        </p:selectOneMenu>
        <f:facet name="footer">
            <p:commandButton process="@form" value="#{loc['addComment']}" actionListener="#{caretakerCautionBean.addNewPosition()}" update=":messages" oncomplete="caretakerCautionsDialog.hide()"/>
        </f:facet>
    </p:panelGrid>
</h:form>

豆:

@ManagedBean
public class CaretakerCautionBean {

    @ManagedProperty(value="#{caretakerCautionRepository}")
    private CaretakerCautionRepository caretakerCautionRepo;
    @ManagedProperty(value="#{locationRepository}")
    private LocationRepository locationRepo;
    @ManagedProperty(value = "#{buildingRepository}")
    private BuildingRepository buildingRepo;
    @ManagedProperty(value="#{zoneRepository}")
    private ZoneRepository zoneRepo;
    @ManagedProperty(value="#{subZoneRepository}")
    private SubZoneRepository subZoneRepo;
    @ManagedProperty (value="#{userRepository}")
    private UserRepository userRepo;
    @ManagedProperty(value="#{userService}")
    private UserService userSvc;

    private List<CaretakerCaution> caretakerCautionList;
    private List<Location> locationList;
    private List<User> userList;
    private List<SubZone> subZoneList;
    private List<Building> buildingList;
    private List<Zone> zoneList;
    private Location location; 
    private SubZone subZone;
    private Building building;
    private Zone zone;
    private User user;
    private String description;
    private String comment;
    private String actionsTaken;
    private int id;

    private transient Logger logger = Logger.getLogger("aplikacjaLogger");

    public void locationChanged(AjaxBehaviorEvent ev) {
        if(location !=null)
            logger.log(Level.INFO, "locationChanged: "+location.getName());
        buildingList = null;
        getBuildingList();
    }

    public void buildingChanged(AjaxBehaviorEvent ev) {
        if(building != null)
            logger.log(Level.INFO, "buildingChanged: "+building.getName());
        zoneList = null;
        getZoneList();
    }

    public void zoneChanged(AjaxBehaviorEvent ev) {
        if(zone != null)
            logger.log(Level.INFO, "zoneChanged: "+zone.getName());
        subZoneList = null;
    getSubZoneList();
    }

    public void subZoneChanged(AjaxBehaviorEvent ev) {
        if(subZone != null)
            logger.log(Level.INFO, "subZoneChanged: "+subZone.getName());
    }

    public void addNewPosition() {
        CaretakerCaution cc = new CaretakerCaution();
        cc.setLocation(location);
        cc.setBuilding(building);
        cc.setZone(zone);
        cc.setSubZone(subZone);
        getCaretakerCautionRepo().save(cc);
        logger.log(Level.INFO, "Dodano uwage.");
    }

    public BuildingRepository getBuildingRepo() {
        return buildingRepo;
    }

    public void setBuildingRepo(BuildingRepository buildingRepo) {
        this.buildingRepo = buildingRepo;
    }

    public ZoneRepository getZoneRepo() {
        return zoneRepo;
    }

    public void setZoneRepo(ZoneRepository zoneRepo) {
        this.zoneRepo = zoneRepo;
    }

    public UserRepository getUserRepo() {
        return userRepo;
    }

    public void setUserRepo(UserRepository userRepo) {
        this.userRepo = userRepo;
    }

    public Building getBuilding() {
        return building;
    }

    public void setBuilding(Building building) {
        this.building = building;
    }

    public Zone getZone() {
        return zone;
    }

    public void setZone(Zone zone) {
        this.zone = zone;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public List<User> getUserList() {
        return userList;
    }

    public void setUserList(List<User> userList) {
        this.userList = userList;
    }

    public List<Location> getLocationList() {
        if(locationList == null)
            locationList = getUserSvc().getLoggedIn().getCaretakenLocations();
        return locationList;    
    }

    public void setLocationList(List<Location> locationList) {
        this.locationList = locationList;
    }

    public List<CaretakerCaution> getCaretakerCautionList() {
        return caretakerCautionList;
    }

    public void setCaretakerCautionList(List<CaretakerCaution> caretakerCautionList) {
        this.caretakerCautionList = caretakerCautionList;
    }

    public SubZoneRepository getSubZoneRepo() {
        return subZoneRepo;
    }

    public void setSubZoneRepo(SubZoneRepository subZoneRepo) {
        this.subZoneRepo = subZoneRepo;
    }

    public List<SubZone> getSubZoneList() {
        if(subZoneList == null && zone != null)
            subZoneList = getSubZoneRepo().findByZone(zone);
        return subZoneList;
    }

    public void setSubZoneList(List<SubZone> subZoneList) {
        this.subZoneList = subZoneList;
    }

    public SubZone getSubZone() {
        return subZone;
    }

    public void setSubZone(SubZone subZone) {
        this.subZone = subZone;
    }

    public LocationRepository getLocationRepo() {
        return locationRepo;
    }

    public void setLocationRepo(LocationRepository locationRepo) {
        this.locationRepo = locationRepo;
    }

    public Location getLocation() {
        return location;
    }

    public void setLocation(Location location) {
        this.location = location;
    }

    public List<Building> getBuildingList() {
        if(buildingList == null && location != null)
            buildingList = getBuildingRepo().findByLocation(location);
        return buildingList;
    }

    public void setBuildingList(List<Building> buildingList) {
        this.buildingList = buildingList;
    }

    public List<Zone> getZoneList() {
        if(zoneList == null && building != null)
            zoneList = getZoneRepo().findByBuilding(building);
        return zoneList;
    }

    public void setZoneList(List<Zone> zoneList) {
        this.zoneList = zoneList;
    }

    public CaretakerCautionRepository getCaretakerCautionRepo() {
        return caretakerCautionRepo;
    }

    public void setCaretakerCautionRepo(
        CaretakerCautionRepository caretakerCautionRepo) {
        this.caretakerCautionRepo = caretakerCautionRepo;
    }

    public UserService getUserSvc() {
        return userSvc;
    }

    public void setUserSvc(UserService userSvc) {
        this.userSvc = userSvc;
    }

    public String getComment() {
        return comment;
    }

    public void setComment(String comment) {
        this.comment = comment;
    }

    public String getActionsTaken() {
        return actionsTaken;
    }

    public void setActionsTaken(String actionsTaken) {
        this.actionsTaken = actionsTaken;
    }
}

转换器:

@Override
public Object getAsObject(FacesContext fc, UIComponent uiComp, String stringVal) {
    if(stringVal == null || stringVal.trim().equals("")) return null;
    else {
        Integer userId = Integer.parseInt(stringVal);
        for(Zone zon: this.getZonRepo().findAll()) 
            if(zon.getId() == userId) {
                return zon;
            }   
        return null;
    }
}

@Override
public String getAsString(FacesContext fc, UIComponent uiComp, Object obj) {
    if(obj == null || obj.equals("")) {
        return "";
    } else {
        try {
            return String.valueOf(((Zone)obj).getId());
        } catch(ClassCastException cce) {
            return "";
        }
    }
}

public ZoneRepository getZonRepo() {
    return zonRepo;
}

public void setZonRepo(ZoneRepository zonRepo) {
    this.zonRepo = zonRepo;
}
}

1 个答案:

答案 0 :(得分:1)

您尚未指定bean范围,因此默认情况下为RequestScoped。目前您正在执行没有execute="" attibutes的AJAX请求,默认为@this,因此仅使用部分数据重新创建bean。

在您的情况下,您需要在操作之间保留值,因此您需要使用ViewScopedSessionScoped

我建议你像这样使用ViewScoped

@ManagedBean
@ViewScoped
public class CaretakerCautionBean {
    // ...
}