0

my problem is that i achieve to redirect in a new page, "results" at the end of the process. But in the navigator, it's still the name of the page it was working after redirect to this page. Therefore, I can't display an attribute of managed bean on this page "results".

Here is my "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>

Here is the bean :

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");

        }

    }

}

And here is the "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>

Like i said results can't appear; so the end page dont display the attribute :

Votre résultat est de {questionBean.finalResult}Entrez votre adresse mail pour les résultats
Kukeltje
  • 12,223
  • 4
  • 24
  • 47
jozinho22
  • 459
  • 2
  • 7
  • 24
  • Possible duplicate of [How to navigate in JSF? How to make URL reflect current page (and not previous one)](http://stackoverflow.com/questions/15521451/how-to-navigate-in-jsf-how-to-make-url-reflect-current-page-and-not-previous-o) – Kukeltje Sep 03 '16 at 07:04
  • Have you noted the missing # in your EL? – Jasper de Vries Sep 03 '16 at 07:51
  • And there is more wrong in your code. `p:ajax` does not have a render attribute, and you mix jsf and spring managed imports – Kukeltje Sep 03 '16 at 08:54

0 Answers0