我的问题是我实现了在新页面中重定向,“结果”在流程结束时。但是在导航器中,它仍然是重定向到此页面后它正在工作的页面的名称。 因此,我无法在此页面“结果”上显示托管bean的属性。
这是我的“index.xhtml”:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.org/ui">
<h:head>
<title>Test</title>
</h:head>
<h:body>
<h:outputLabel value="Question :">
</h:outputLabel>
<h:form id="questionForm">
<h:outputLabel
value="#{questionBean.questions[questionBean.counter - 1].askedQuestion}">
</h:outputLabel>
<h:selectOneRadio valueChangeListener="#{questionBean.selectQuestion}">
<f:selectItems
value="#{questionBean.questions[questionBean.counter - 1].proposals}"
var="proposal" itemValue="#{proposal}" />
<p:ajax render="selectedQ" />
</h:selectOneRadio>
<h:commandLink value="Question suivante"
action="#{questionBean.increment()}">
<f:param name="pageId" value="1" />
</h:commandLink>
</h:form>
</h:body>
</html>
这是豆子:
package trainforjava.question;
import java.io.Serializable;
import java.util.List;
import javax.faces.bean.ManagedProperty;
import javax.faces.event.AbortProcessingException;
import javax.faces.event.ValueChangeEvent;
import org.apache.log4j.Logger;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
@Component
@Scope("application")
public class QuestionBean implements Serializable {
/**
*
*/
private static final long serialVersionUID = 3443847881847328276L;
static Logger log = Logger.getLogger(QuestionBean.class.getName());
private List<Question> questions;
private int counter;
private User user;
private String selectedQuestion;
private int resultCounter;
private double finalResult;
public QuestionBean() {
QuestionCreator creator = new QuestionCreator();
questions = creator.createQuestions();
// Collections.shuffle(questions);
System.out.println(questions);
user = new User();
counter = 1;
}
public List<Question> getQuestions() {
return questions;
}
public void setQuestions(List<Question> questions) {
this.questions = questions;
}
public int getCounter() {
return counter;
}
public void setCounter(int counter) {
this.counter = counter;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public String getSelectedQuestion() {
return selectedQuestion;
}
public void setSelectedQuestion(String selectedQuestion) {
this.selectedQuestion = selectedQuestion;
}
public int getResultCounter() {
return resultCounter;
}
public void setResultCounter(int resultCounter) {
this.resultCounter = resultCounter;
}
public double getFinalResult() {
return finalResult;
}
public void setFinalResult(double finalresult) {
this.finalResult = finalresult;
}
public String increment() {
System.out.println("counter in " + counter);
if (counter > 0) {
user.getAnswers().add(selectedQuestion);
System.out.println("answers " + user.getAnswers());
}
if (counter == questions.size()) {
System.out.println("counter finished");
for (int i = 0; i < user.getAnswers().size(); i++) {
System.out.println("userAnswer " + user.getAnswers().get(i));
System.out.println("goodAnswer " + questions.get(i).getGoodAnswer());
if (user.getAnswers().get(i).equals(questions.get(i).getGoodAnswer())) {
resultCounter += 1;
System.out.println("resultCounter " + resultCounter);
}
}
System.out.println("resultCounter final : " + resultCounter);
finalResult = resultCounter / (questions.size()) * 100;
System.out.println("finalResult : " + finalResult);
return "results";
} else {
counter += 1;
System.out.println("counter out " + counter);
return "index";
}
}
public void selectQuestion(ValueChangeEvent event) throws AbortProcessingException {
selectedQuestion = (String) event.getNewValue();
String goodAnswer = questions.get(counter - 1).getGoodAnswer();
if (selectedQuestion.equals(goodAnswer)) {
System.out.println("good answer selected");
}
}
}
这是“results.xhtml”:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.org/ui">
<h:head>
<title>Test</title>
</h:head>
<h:body>
<h:outputLabel>Votre résultat est de </h:outputLabel>
<h:outputText value="{questionBean.finalResult}"/>
<h:outputLabel>Entrez votre adresse mail pour les résultats</h:outputLabel>
<h:inputText></h:inputText>
</h:body>
</html>
像我说的结果不能出现;所以结束页面不显示属性:
Votre résultat est de {questionBean.finalResult}Entrez votre adresse mail pour les résultats