Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
367 views
in Technique[技术] by (71.8m points)

java - JSF Validation Error While Using Custom Converter

I am setting up a form using JSF (I'm pretty new at this) and I am getting a Validation Error: Value is not valid message on one of the fields. This field is actually a separate object (as I will show below) that has a custom converter.

Here is what I have (with non-relevant code removed):

I have a Citation class:

@ManagedBean(name="citation")
public class Citation {
    private int id;
    private Status status;

    // getters and setters
}

I also have a Status class that you see referenced in the Citation class:

@ManagedBean(name="status")
public class Status {
    private int id;
    private String name;

    // getters and setters

    public List<Status> getAllStatuses() {
        Session session = HibernateUtil.getCurrentSession();
        session.beginTransaction();
        session.clear();

        Query query = session.createQuery("from Status");
        List<Status> statuses = query.list();

        try {
            session.getTransaction().commit();
        } catch (HibernateException e) {
            // TODO: handle exception
            session.getTransaction().rollback();
        }

        return statuses;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) return false;
        if (!(obj instanceof Status)) return false;

        if (this.id == ((Status)obj).getId()) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public int hashCode() {
        return this.name.hashCode();
    }
}

Then for my form, I have:

<h:selectOneMenu id="citation_status" value="#{citation.status}">
    <f:selectItems value="#{status.allStatuses} var="s" itemValue="#{s.id}" itemLabel="#{s.name}" />
</h:selectOneMenu>
<h:message for="citation_status" />

Lastly, for my converter, I have:

@FacesConverter(forClass=Status.class)
public class StatusConverter implements Converter {
    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) {
        // uses Hibernate to get the Status object (using a breakpoint in Eclipse, I have verified that this works)
        // I can post this code if needed, but just trying to keep it short :)
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {
        return String.valueOf(((Status) value).getId());
    }
}

Now when I get to my form and submit, I get the Validation Error next to the Status. I'm pretty new at this and thanks to @BalusC, I'm this far along.

Any help is greatly appreciated.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Validation Error: Value is not valid

In case of <h:selectOneMenu>, you will get this when error whenever the selected item does not match any of the items available in the list. I.e. selectedItem.equals(selectItem) has never returned true for any of the items.

Since it's apparently a custom object (the Status class), did you implement its Object#equals() (and #hashCode()) properly? You can if necessary let the IDE (Eclipse/Netbeans) autogenerate them.

See also:


Update: after having a closer look at your code, it turns out that you're actually submitting #{s.id} instead of #{s} (the whole Status object). Fix the itemValue accordingly and it should work (if equals() is still doing its job properly).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...