gpt4 book ai didi

java - JSF selectManyCheckboxes 不起作用

转载 作者:行者123 更新时间:2023-11-30 04:35:45 25 4
gpt4 key购买 nike

我有一个循环问题和答案。我想填充 map Map> 检查答案值

<ui:repeat value="#{test.questionList}" var="question">
<h:outputText value="#{question.question}"
rendered="#{not empty question}" />
<p:selectManyCheckbox value="#{test.selectedItems[question.questionId]}"
layout="pageDirection" converter="#{answerConverter}">
<f:selectItems value="#{question.questionAnswers}" var="ans"
itemValue="#{ans.answer}" itemLabel="#{ans.answer.answer}" />
</p:selectManyCheckbox>
</ui:repeat>

在我的 bean 中我有

private Map<Long, List<Answer>> selectedItems;

private List<Question> questionList;
private Map<Long, List<Answer>> questionAnswerMap;

//getter- setter selectedItems

public Map<Long, List<Answer>> getQuestionAnswerMap() {
if (!selectedItems.isEmpty()) {
Set<Long> idsSet = selectedItems.keySet();
for (Long questionId : idsSet) {
List<Answer> answersOnPassedQuestion = selectedItems
.get(questionId);
questionAnswerMap.put(questionId, answersOnPassedQuestion);
}
}
return questionAnswerMap;

}
public void setQuestionAnswerMap(Map<Long, List<Answer>> questionAnswerMap) {
this.questionAnswerMap = questionAnswerMap;
}

我还展示了我的模型类

public class Question implements Serializable{

private Long questionId;
private String question;
private Integer complexity;
private Set<QuestionAnswer> questionAnswers;
}

其中 QuestionAnswer 也像这样建模类

public class QuestionAnswer implements Serializable{

private QuestionAnswerIdentifer questionAnswerIdentifer;
private Answer answer;
}

QuestionAnswerIdentifer 类,作为组件 ID 服务,由 Long answerId、Long QuestionId 组成

当我按下“通过测试按钮”时,出现错误

j_id280458803_1_639e37a6:0:j_id280458803_1_639e37cf: 
Validation Error: Value is not valid

///已更新 我尝试 BalusC 答案并编写转换器

@ManagedBean
@RequestScoped
public class AnswerConverter implements Converter{

@ManagedProperty(value="#{answerService}")
private IAnswerService answerService;

@Override
public Object getAsObject(FacesContext context, UIComponent component, String submittedValue)
throws ConverterException {
Answer answer = new Answer();
try {
answer = answerService.getById(Long.valueOf(submittedValue));
} catch (DAOException | NumberFormatException e) {
e.printStackTrace();
}
return answer;
}

@Override
public String getAsString(FacesContext context, final UIComponent component, Object modelValue)
throws ConverterException {

return String.valueOf(((Answer) modelValue).getAnswerId());
}

public void setAnswerService(IAnswerService answerService) {
this.answerService = answerService;
}
}

//等于答案的hashCode

  @Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((answer == null) ? 0 : answer.hashCode());
result = prime * result
+ ((answerId == null) ? 0 : answerId.hashCode());
result = prime * result
+ ((correctness == null) ? 0 : correctness.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;
Answer other = (Answer) obj;
if (answer == null) {
if (other.answer != null)
return false;
} else if (!answer.equals(other.answer))
return false;
if (answerId == null) {
if (other.answerId != null)
return false;
} else if (!answerId.equals(other.answerId))
return false;
if (correctness == null) {
if (other.correctness != null)
return false;
} else if (!correctness.equals(other.correctness))
return false;
return true;
}

但我仍然得到相同的错误验证(我不明白为什么会收到此错误

最佳答案

您需要创建一个 Converter它在 Answer 之间进行转换实例及其独特String表示并通过 converter 引用它<p:selectManyCheckbox> 的属性.

这是一个启动示例(省略运行时检查),前提是 Answer有一个 id代表唯一技术标识符的属性。

@FacesConverter("answerConverter")
public class AnswerConverter implements Converter {

@Override
public String getAsString(FacesContext context, UIComponent component, Object modelValue) throws ConverterException {
// Write code to convert Answer to its unique String representation for usage in HTML/HTTP. E.g.
return String.valueOf(((Answer) modelValue).getId());
}

@Override
public Object getAsObject(FacesContext context, UIComponent component, Object submittedValue) throws ConverterException {
// Write code to convert unique String representation of Answer to concrete Answer for usage in Java/JSF. E.g.
return yourAnswerService.find(Long.valueOf(submittedValue));
}

}

请注意 @FacesConverter(forClass=Answer.class)将不起作用,因为通用类型信息在运行时丢失。

另请参阅:

关于java - JSF selectManyCheckboxes 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13562667/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com