当我尝试验证jsf输入时如何解决此错误ClassCastException

时间:2019-10-19 17:17:41

标签: java validation jsf classcastexception managed-bean

这是我的代码main.xhtml。我正在尝试验证输入。

This is a method-validator:
public void validateCorseCode(FacesContext context, UIComponent component,         Object value)
      throws ValidatorException {
    if(value == null) {
      return;
    }
    String data = value.toString();
    //CourseCode must start with LUV
    if(!data.startsWith("My")) {
      FacesMessage message = new FacesMessage("CourseCode must start with     LUV");
      throw new ValidatorException(message);
    }
  }

这是我在jsf中的代码:

<h:inputText value="#{studentValidFour.courseCode}" label="Course code"     id="courseCode" required="true"
                                    validator="#    {studentValidFour.validateCorseCode}" >
</h:inputText>
          <h:messages for="courseCode" styleClass="error"/> 

这是一条错误消息:

Caused by: java.lang.ClassCastException: class     javax.faces.application.FacesMessage cannot be cast to class java.lang.String     (javax.faces.application.FacesMessage is in unnamed module of loader     'javax.faces.api@2.3.9.SP02' @3983b87e; java.lang.String is in module     java.base of loader 'bootstrap')

0 个答案:

没有答案