我的问题如下。
我想用JSF进行问卷调查,我想在整个页面上增加一个计数器。可能是最好的方法是设置一个ajax事件。
这是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>
正如您所注意到的,第一个问题的值是创建的列表的第一个元素。我想设置一个计数器,以便在每次点击时转到n + 1个问题。
这是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;
}
}
我想将方法increment()作为页面的参数引用,然后在问卷中将其递增。我尝试使用,但它只是支持bean。
有人可以帮我这个吗?
谢谢你
答案 0 :(得分:0)
谢谢。我发布了如何实现它。我不知道它是否真的优化了,但它是一个提议:
<!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>
这是豆子;
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";
}
}
我只是没有设法将变量设置为“questionBean.counter”。