提交前验证表格?

时间:2014-09-02 00:43:01

标签: jquery ajax jsf primefaces managed-bean

我有一个包含某些字段的表单,当我尝试执行提交时,不会提交验证并提交表单而不会向<p:message/>返回异常。 我希望使用<f:ajax>提交表单,如果在<p:messagge/>

上发生任何异常返回消息

XHTML

<f:ajax>

    <h:form prependId="false">
        <p:fieldset legend="Turma">
            <h:panelGroup>
                <h:panelGrid columns="2">

                        <h:outputLabel value="Turma"/>
                        <h:inputText value="#{turmaMB.bean.turma}" id="turma"
                                                               required="#{turmaMB.bean.turma}"
                                                               requiredMessage="Informe a turma"
                                                               maxlength="50"
                                                               style="text-transform:uppercase"
                                                               >        
                            <f:validateBean/>           
                        </h:inputText>  
                        <p:message for="turma"/>                    

                        <h:outputLabel value="Ano"/>
                        <h:inputText value="#{turmaMB.bean.ano}" id="ano"
                                                               required="#{turmaMB.bean.ano}"
                                                               requiredMessage="Informe o ano"
                                                               maxlength="4"
                                                               >    
                            <f:convertNumber type="number"/>    
                            <f:validateBean/>           
                        </h:inputText>
                        <p:message for="ano"/>


                </h:panelGrid>
            </h:panelGroup>     
        </p:fieldset>   
        <h:commandButton actionListener="#{turmaMB.insert(turmaMB.bean)}" value="Salvar" />
        <h:commandButton onclick="createTurma.hide();" value="Cancelar"/>
    </h:form>
</f:ajax>

</p:dialog>

托管bean

@ManagedBean
@ViewScoped
public class TurmaMB {
    private Turma bean = new Turma();
    private GenericDAO<Turma> dao = new GenericDAO<Turma>(Turma.class);
    private List<Turma> turmas = dao.findAll();

    public void insert(Turma t){
        dao.insert(t);
        bean = new Turma();
    }

    public Turma getBean() {
        return bean;
    }

    public List<Turma> getTurmas() {
        return turmas;
    }

bean实体

@Entity
@NamedQueries({
    @NamedQuery(name=Turma.FIND_ALL, query="SELECT t FROM Turma t"),    
})
public class Turma implements Serializable{ 
    private static final long serialVersionUID = 1L;

    //named query
    public static final String FIND_ALL = "Turma.findALL";
    //

    @Id @GeneratedValue(strategy=GenerationType.AUTO)
    private Long id;

    @NotNull @NotEmpty @Column(unique=true)
    private String turma;

    @NotNull
    private Integer ano;

    public Turma() {
        super();
    }

    public Turma(Long id, String turma, Integer ano) {
        super();
        this.id = id;
        this.turma = turma;
        this.ano = ano;
    }

    public Long getId() {
        return id;
    }

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

    public String getTurma() {
        return turma;
    }

    public void setTurma(String turma) {
        this.turma = turma;
    }

    public Integer getAno() {
        return ano;
    }

    public void setAno(Integer ano) {
        this.ano = ano;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((ano == null) ? 0 : ano.hashCode());
        result = prime * result + ((id == null) ? 0 : id.hashCode());
        result = prime * result + ((turma == null) ? 0 : turma.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Turma other = (Turma) obj;
        if (ano == null) {
            if (other.ano != null)
                return false;
        } else if (!ano.equals(other.ano))
            return false;
        if (id == null) {
            if (other.id != null)
                return false;
        } else if (!id.equals(other.id))
            return false;
        if (turma == null) {
            if (other.turma != null)
                return false;
        } else if (!turma.equals(other.turma))
            return false;
        return true;
    }

    public String toString(){
        return turma;
    }   

}

0 个答案:

没有答案