java PrimeFaces 3.2 selsectOneMenu valueChangeListener 不工作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10780093/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PrimeFaces 3.2 selsectOneMenu valueChangeListener not working
提问by z22
i wish to retrieve List based on the selection of dropdownlist item. for that i am using the following code which is not working:
我希望根据下拉列表项的选择检索列表。为此,我正在使用以下不起作用的代码:
<p:selectOneMenu style="width: 150px" value="#{watchBean.exchange}">
<f:selectItem itemLabel="NSE" itemValue="nse"/>
<f:selectItem itemLabel="BSE" itemValue="bse"/>
<p:ajax event="change" update=":frm" listener="#{watchBean.doScripList}" />
</p:selectOneMenu>
bean code:
豆码:
public void doScripList(ValueChangeEvent e)
{
sl=getAllScripByExchange((String)e.getNewValue()); //sl is of type List<MasterScrip>
}
when i debug , i see that the event is not fired and i get the following error:
当我调试时,我看到事件没有被触发,我收到以下错误:
javax.el.MethodNotFoundException: Method not found: [email protected](javax.faces.event.AjaxBehaviorEvent)
at com.sun.el.util.ReflectionUtil.getMethod(ReflectionUtil.java:155)...
when i omit p:ajax the 'exchange' type is also not get/set what is causing this problem? what is its solution?
当我省略 p:ajax 时,'exchange' 类型也不是 get/set 是什么导致了这个问题?它的解决方案是什么?
editedrenamed the method to wow() still the same error :
编辑将方法重命名为 wow() 仍然是相同的错误:
javax.el.MethodNotFoundException: Method not found: [email protected](javax.faces.event.AjaxBehaviorEvent)
edited: managed bean code
编辑:托管 bean 代码
import java.util.List;
import javax.inject.Named;
import javax.enterprise.context.RequestScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ValueChangeEvent;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.xml.ws.WebServiceRef;
import service.MasterScrip;
import service.StatelessWebService_Service;
@Named(value = "watchBean")
@RequestScoped
public class watchBean {
@WebServiceRef(wsdlLocation = "WEB-INF/wsdl/localhost_8080/StatelessWebService/StatelessWebService.wsdl")
private StatelessWebService_Service service;
/** Creates a new instance of watchBean */
public watchBean() {
}
String uname,scripSym,exchange;
Integer scripID;
List<UserTrack> ut;
List<MasterScrip> sl;
public List<MasterScrip> getSl() {
return sl;
}
public void setSl(List<MasterScrip> sl) {
this.sl = sl;
}
public String getExchange() {
return exchange;
}
public void setExchange(String exchange) {
sl=getAllScripByExchange(exchange);
this.exchange = exchange;
}
public void wow(ValueChangeEvent e)
{
sl=getAllScripByExchange((String)e.getNewValue());
// setSl(sl);
//FacesContext.getCurrentInstance().renderResponse();
// sl=getAllScripByExchange(exchange);
}
回答by Kennet
Try changing
尝试改变
<p:ajax event="change" update=":frm" listener="#{watchBean.doScripList}" />
to
到
<p:ajax event="change" update=":frm" listener="#{watchBean.doScripList()}" />
add parenthesis at the end of the method.
在方法末尾添加括号。
JSF is looking for a method setDoScripList
in your backing bean, but when adding the parenthesis at the end you're calling this method doScripList
explicitly.
JSF 正在setDoScripList
您的支持 bean 中寻找一个方法,但是在最后添加括号时,您正在doScripList
显式调用此方法。
回答by Lokesh
Landed on the same problem as yours, but I figured out that the valueChangeListener
遇到与您相同的问题,但我发现 valueChangeListener
public void doScripList(ValueChangeEvent e)
works only when you use <f:ajax>
tag and not when you use <p:ajax>
.
But since selectOneMenu is of type <p:selectOneMenu>
, so <f:ajax>
won't work.
仅当您使用<f:ajax>
tag 而不是当您使用<p:ajax>
. 但是因为 selectOneMenu 是 type <p:selectOneMenu>
,所以<f:ajax>
不会工作。
I worked out by removing the parameter ValueChangeEvent e
and it worked.
So try out a no-parameter listener in case of <p:ajax>
tag.
我通过删除参数ValueChangeEvent e
来解决它并且它起作用了。因此,在<p:ajax>
标记的情况下尝试使用无参数侦听器。
public void doScripList()
{
sl=getAllScripByExchange(getExchangeName());
}
Note: here you don't have the event parameter so, it sets the new value by calling respective setter methods, and you can access updated values in the listener. For this case you also need to provide id to selectOneMenu like this:
注意:这里没有 event 参数,因此它通过调用相应的 setter 方法设置新值,您可以在侦听器中访问更新的值。对于这种情况,您还需要像这样为 selectOneMenu 提供 id:
<p:selectOneMenu id="exchangeName" style="width: 150px" value="#{watchBean.exchange}">
...
</p:selectOneMenu>
and then you add get and set methods for exchangeName in your Bean class:
然后在 Bean 类中为 exchangeName 添加 get 和 set 方法:
private String exchangeName;
public String getExchangeName(){
return exchangeName;
}
public void setExchangeName(String exchangeName) {
this.exchangeName = exchangeName;
}
回答by Sai Ye Yan Naing Aye
It is primefaces 3.2 bug, please replace your code as the following.
这是primefaces 3.2的错误,请将您的代码替换为以下内容。
<h:selectOneMenu style="width: 150px" value="#{watchBean.exchange}" valueChangeListener="#{watchBean.doScripList}" onchange="submit()">
<f:selectItem itemLabel="NSE" itemValue="nse"/>
<f:selectItem itemLabel="BSE" itemValue="bse"/>
</h:selectOneMenu>
Change your method likes here;
在这里改变你的方法;
public void doScripList(ValueChangeEvent e){
sl = getAllScripByExchange((String)e.getNewValue());
setAllScriptExchange(sl); //please write setAllScriptExchange method yourself
FacesContext.getCurrentInstance().renderResponse();
}
Your page is reloaded, because of onchange event.
由于 onchange 事件,您的页面已重新加载。