我的问题如下:
我想用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个问题。
下面是问题:
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。
有人能帮我一下吗?感谢u
谢谢。我张贴了我是如何实现它的。我不知道它是否真的优化了,但它是一个建议:
<!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"。