数据库加载selectOneMenu项时,JSF保存错误

时间:2016-08-26 04:02:06

标签: jsf jsf-2

我的应用程序有三个bean,问题,答案,答案类型和xhtml我想加载answertypes(已经在Db中可用)到一旦我点击保存我得到错误

  

sourceId = j_idt4:j_idt8 [severity =(ERROR 2),summary =(j_idt4:j_idt8:验证错误:值无效),detail =(j_idt4:j_idt8:验证错误:值无效)]

我的编码是

<h:form> 
            <h:outputLabel for="questions" value="Question:" />
            <h:inputText id="questions" value="#{questions.question}"/>
            <h:outputLabel for="answers" value="Answer:" />
            <h:inputText id="answers" value="#{answers.answer}"/>
            <h:outputLabel for="answers" value="Answer Type:" />

            <h:selectOneMenu for="answers" value="#{answers.answerTypeId}">
                <f:selectItems value="#{answers.answeTypesMap.entrySet()}" var="answeTypesMap" 
                itemValue="#{answeTypesMap.key}" itemLabel="#{answeTypesMap.value}" />
            </h:selectOneMenu>

            <h:commandButton value="Save" action="#{utilizerManager.saveQuestions}"/>

    </h:form>

这是XHTML

class Answer{
  private int answerTypeId = 0;
  private HashMap<Integer, String> answeTypesMap;

  public void init(ComponentSystemEvent event) {
    FacesContext facesContext = FacesContext.getCurrentInstance();
    if (!facesContext.isPostback() && !facesContext.isValidationFailed()) {
        AnswerTypes ansTypes = new AnswerTypes();
        setAnsweTypesMap(ansTypes.loadAnswerTypes());
    }
}


}

Class Answer and AnswerTypes Map Loaded

class Question{
 public void saveQuestion(){
    DbConnector.connectToDatabase();
    int autoTempId = DbConnector.getPrimaryKeyLastValue("questions", "questionId");
    questionId = autoTempId+1;
    String insertQuery = "insert into questions(questionId,question,autoId,correctAnswer) values("+questionId+",'"+question+"',"+autoID+","+correctAnswer+")";
    DbConnector.InsertionQuery(insertQuery);
    DbConnector.ClearConnection();
}
}

我的问题课

最后是我的UtilizerManager类

class UtilizeManager{
  // I wanted to save answer and then get the aswerId to set questions bean
  public void saveQuestions(){
    answersBean.saveAnswer();
    questionBean.setCorrectAnswer(answersBean.getAnswerId());
    questionBean.saveQuestion();
    }
}

所以请在这个场景中提供帮助,据我所知(根本原因分析)错误是

<h:selectOneMenu for="answers" value="#{answers.answerTypeId}">
                <f:selectItems value="#{answers.answeTypesMap.entrySet()}" var="answeTypesMap" 
                itemValue="#{answeTypesMap.key}" itemLabel="#{answeTypesMap.value}" />
            </h:selectOneMenu>

在xhtml中的代码路径下面,请告诉我如何解决这个地图是HashMap

使用这样的原因是在保存项目时我需要保存整数并在显示时我需要显示文本

0 个答案:

没有答案