Я пытаюсь реализовать приложение для викторины. Приложение загружает вопросы с помощью ajax один за другим. Когда пользователь нажимает кнопку «к следующему вопросу», его / ее ответ сохраняется в кеше. Но когда я отлаживаю, список кеша всегда равен нулю ...
Этот код создает первый массив кеша:
public static void viewQuiz(@Required String user, @Required String test) {
if(validation.hasErrors()) {
flash.error("Hoop kullanıcı lazım…");
index();
} else{
TestClass selectedTest = TestClass.find("title", test).first();
List<String> choiceList = new ArrayList<String>();
session.put("testID", selectedTest.id);
Cache.set("choices", choiceList, "30mn");
render();
}
}
И этот код пытается сохранить ответы один за другим:
public static void question(@Required Long id, String answer){
Long testId = Long.parseLong(session.get("testID"));
TestClass test = TestClass.findById(testId);
List<Question> questList = Question.find("test_id", test.id.intValue()).fetch();
Question quest = questList.get(id.intValue());
if(answer != null){
List<String> choiceList= Cache.get("choices",List.class);
choiceList.add(id.intValue(), answer);
Cache.set("choices", choiceList, "30mn");
}
int count = questList.size()-1;
render(quest, count, id);
}
И этот код является представлением html второго:
#{extends 'main.html' /}
#{set title:'question.html' /}
<script type="text/javascript">
var questionId = ${id};
$('#nextButton').click(function(){
$('#questionDiv').html('<p><img id = "loaderGif" src="public/images/loading.gif"/></p>');
$('#questionDiv').load("/test/" + ++questionId);
});
$('#endButton').click(function(){
$('#questionDiv').html('<p><img id = "loaderGif" src="public/images/loading.gif"/></p>');
$('#questionDiv').load("/result");
});
</script>
<legend>Soru ${id+1}</legend>
<p>&{quest.question}</p>
#{list items:quest.choices, as:'choice'}
<p><input type="radio" name = "answer" id = "answer" size="30" value="${choice}"/>&{choice}</p>
#{/list}
#{if id < count}
<input id = "nextButton" name="nextButton" type="button" value="İleri"/>
#{/if}
#{else}
<input id = "endButton" name="endButton" type="button" value="Bitti"/>
#{/else}