JSF-将参数传递给valuechangelistener [英] JSF- passing a parameter to valuechangelistener

查看:128
本文介绍了JSF-将参数传递给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">


也就是说,我建议使用比TestServicetestchange()更合理的变量名,分别类似于QuestionairequestionchangeAnswer().这使代码更具自记录性.


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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆