JSF-将参数传递给valuechangelistener [英] JSF- passing a parameter to valuechangelistener
问题描述
我有一个像这样的单选按钮:
I have a litte radiobutton like this :
<h:selectOneRadio value="#{test.answer}" valueChangeListener="#{TestService.changeanswer}" immediate="true" id="answer">
<f:selectItem itemValue="A" itemLabel="Absolutely True"/>
<f:selectItem itemValue="B" itemLabel="True"/>
<f:selectItem itemValue="C" itemLabel="Partially True"/>
<f:selectItem itemValue="D" itemLabel="Not True"/>
<f:selectItem itemValue="E" itemLabel="Definitely Not True"/>
<f:ajax event="change" process="answer"></f:ajax></h:selectOneRadio>
我的听众就是这样:
public void changeanswer(ValueChangeEvent vcEvent) {
System.out.println("comeon= " + vcEvent.getOldValue());
System.out.println("comeon= " + vcEvent.getNewValue());}
我想将参数传递给changeanswer方法.例如,我想将questionid传递给changeanswer函数.我需要做一些安排.
I would like to pass a parameter to the changeanswer method.For example I want to pass the questionid to the changeanswer function. I need to make some arrangements in it.
我该怎么做?
非常感谢.
布拉德-新秀..
推荐答案
看到组件值是如何绑定的,我敢打赌它在数据表中.如果确实如此,则可以使用 DataModel
属性添加到TestService
bean中,如下所示:
Seeing how the component values are bound, I bet that it's inside a datatable. If that is indeed the case, you can use DataModel#getRowData()
to obtain the current row. Add a DataModel
property to the TestService
bean like follows:
private List<Question> questions;
private DataModel<Question> questionModel;
@PostConstruct
public void init() {
questions = getItSomehow();
questionModel = new ListDataModel<Question>(questions);
}
public void change(ValueChangeEvent event) {
Question currentQuestion = questionModel.getRowData();
// ...
}
,然后按如下所示更改视图:
and change the view as follows:
<h:dataTable value="#{TestService.questionModel}" var="test">
也就是说,我建议使用比TestService
,test
和change()
更合理的变量名,分别类似于Questionaire
,question
和changeAnswer()
.这使代码更具自记录性.
That said, I'd suggest to use more sensible variable names than TestService
, test
and change()
, like Questionaire
, question
and changeAnswer()
respectively. This makes the code more self-documenting.
这篇关于JSF-将参数传递给valuechangelistener的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!