1

my problem is the following.

I would like to make a questionnaire with JSF and I want to increment a counter all along the pages. May be the best way to do it is to also set an ajax event.

Here is the 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>

    Question :
    <h:form id="questionForm">

        <h:outputLabel value="#{questionBean.questions[0].askedQuestion}">
        </h:outputLabel>

        <h:selectOneRadio>
            <f:selectItems value="#{questionBean.questions[0].proposals}"
                var="proposal" itemValue="#{proposal}" />
        </h:selectOneRadio>

    </h:form>

</h:body>
</html>

As you notice, the value for the first question is the first element of a list which is created. I would like to set a counter in order to go the the n+1 question at each click.

Here is the questionBean :

package trainforjava.question;

import java.util.Collections;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;

@Controller
public class QuestionBean {

    static Logger log = Logger.getLogger(QuestionBean.class.getName());

    private List<Question> questions;
    private int counter = 0;

    public QuestionBean() {
        QuestionCreator creator = new QuestionCreator();
        questions = creator.createQuestions();
        System.out.println(questions);
        Collections.shuffle(questions);
        System.out.println(questions);
        log.info(questions);

    }

    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 int increment() {
        counter++;
        return counter;

    }

}

I would like to refer the method increment() as a parameter of the page, and then increment it all along the questionnaire. I tried to use but it's lonly for backing bean.

Someone can help me on this ?

Thank u

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
jozinho22
  • 459
  • 2
  • 7
  • 24
  • You've got several options. The easiest one is probably to use a single bean for the whole questionaire and render the concrete piece depending on the counter parameter value. If you still want to use different views, you can pass the counter value as a [request parameter](http://stackoverflow.com/a/39000013/1199132) or even as a [parameter in the flash scope](http://stackoverflow.com/questions/11194112/understand-flash-scope-in-jsf2/21277621#21277621). Other solution would be to store it in the session, but that's not recommended if the user is intended to work in different browser tabs. – Aritz Aug 27 '16 at 12:25
  • By the way, how are you managing your beans? Do you use Spring for that? If you use Spring's `@Controller` annotation with no declared scope the bean will be a singleton (application scoped). – Aritz Aug 27 '16 at 12:26
  • Thank you. I post how I implement it. I don't know if it's really optimized but it's one proposal : – jozinho22 Aug 31 '16 at 19:48

1 Answers1

0

Thank you. I post how I implement it. I don't know if it's really optimized but it's one proposal :

<!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>

    Question :
    <h:form id="questionForm">

        <h:outputLabel
            value="#{questionBean.questions[questionBean.counter].askedQuestion}">
        </h:outputLabel>

        <h:selectOneRadio>
            <f:selectItems value="#{questionBean.questions[questionBean.counter].proposals}"
                var="proposal" itemValue="#{proposal}" />
        </h:selectOneRadio>

        <h:link value="Question suivante" outcome="index.xhtml"/>

        <f:metadata>
            <f:viewParam name="id" value="#{questionBean.counter}" />
            <f:viewAction action="#{questionBean.increment()}" />
        </f:metadata>

    </h:form>

</h:body>
</html>

Here is the bean ;

package trainforjava.question;

import java.util.Collections;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

//@ManagedBean
//@ApplicationScoped
@Controller
@Scope(value = "session")
public class QuestionBean {

    static Logger log = Logger.getLogger(QuestionBean.class.getName());

    private List<Question> questions;
    private int counter;
    private User user;

    public QuestionBean() {
        counter = -1;
        QuestionCreator creator = new QuestionCreator();
        questions = creator.createQuestions();
        Collections.shuffle(questions);
        System.out.println(questions);

    }

    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 increment() {

        System.out.println("counter " + counter);
        counter += 1;

        if (counter > 2) {
            System.out.println("counter < 2");
            return "results";

        }

        System.out.println("counter " + counter);
        return "index";

    }

}

I just did not manage to set a variable to stock "questionBean.counter".

jozinho22
  • 459
  • 2
  • 7
  • 24