< p:selectOneMenu侦听器方法在选择值时返回null

编程入门 行业动态 更新时间:2024-10-11 17:29:28
本文介绍了< p:selectOneMenu侦听器方法在选择值时返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在将JSF 2.0与Primefaces 3.4.2一起使用

I am using JSF 2.0 with Primefaces 3.4.2

我在JSF页面中有以下内容

I have the following in JSF page

<p:selectOneMenu value="#{mb.employee}"> <f:selectItems value="#{mb.employeeList}" var="emp" itemLabel="#{emp.employeeName}" itemValue="#{emp.employeeCode}"/> <p:ajax listener="#{mb.changeMethod}" /> </p:selectOneMenu>

问题是当我在selectOneMenu中选择一个值时,对于此System.out.println("val "+employee.getEmployeeName());

Problem is when I select a value in selectOneMenu, I am getting null in changeMethod of ManagedBean, for this System.out.println("val "+employee.getEmployeeName());

这可能是什么原因?我该如何解决这个问题? 任何麻将都是非常可观的.

What could be the reason for this? How can I resolve this problem? Any hep is highly appreciable.

ManagedBean 代码

@Named("mb") @ViewAccessScoped public class MyBean implements Serializable { private Employee employee; private List<Employee> employeeList; @Inject EmployeeService employeeService; @PostConstruct public void loadEmployees() { employeeList = employeeService.getEmployees(); } public void changeMethod() { System.out.println("val "+employee.getEmployeeName()); } /* getters and setters for employee and employeeList */ .... methods /* */

推荐答案

您没有在<p:ajax>组件上指定要处理的表单元素,因此ajax请求可能正在提交可能与其他字段验证冲突的多个值.请记住,如果提交了一个值并且验证失败,则不会将任何请求值设置为模型.当您到达Application(Event)阶段时,模型值将不会反映任何已提交的请求值.

You are not specifying on the <p:ajax> component which form elements to process, so the ajax request may be submitting multiple values that could be conflicting with other field validation. Remmeber that if a value is submitted and it fails validation, then none of the request values get set to the model. When you reach the Application(Event) phase, the model values will not reflect any submitted request values.

尝试一下:

<p:selectOneMenu value="#{mb.employee}"> <f:selectItems value="#{mb.employeeList}" var="emp" itemLabel="#{emp.employeeName}" itemValue="#{emp.employeeCode}"/> <p:ajax process="@this" partialSubmit="true" listener="#{mb.changeMethod}" /> </p:selectOneMenu>

上面,您将仅提交要应用于模型的当前组件请求值.

Above you will be submitting just the current component request value to be applied to the model.

实际上,这可能不是验证问题,因为ajax事件上没有提交任何请求值.

Actually it probably isn't a validation issue so much as there are no request values being submitted on the ajax event.

根据Primefaces手册:

According to the Primefaces Manual:

处理空字符串组件以在部分请求中处理.

process null String Component(s) to process in partial request.

更新null字符串组件以使用ajax更新.

update null String Component(s) to update with ajax.

第二列是默认值.在标准<f:ajax>中,execute属性默认为@this,但是对于<p:ajax>则不是这种情况.如果要提交当前组件,则必须在process属性中指定它.

The second column is Default Value. In the standard <f:ajax> the execute attribute defaults to @this however this is not the case for <p:ajax>. If you want to submit the current component then you must specify this in the process attribute.

更多推荐

&lt; p:selectOneMenu侦听器方法在选择值时返回null

本文发布于:2023-11-24 19:37:18,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1626544.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:侦听器   方法   lt   amp   null

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!