如何将a4j:actionparam的name属性值传递给后端Bean?
解决a4j:actionparam的name属性值无法获取的问题
我看了你的代码,问题出在对<a4j:actionparam>的name属性的理解偏差上——它不是组件的属性(attribute),而是用来定义AJAX请求中传递的参数名称,所以你用event.getComponent().getAttributes().get("minimum")肯定拿不到值。下面给你几种靠谱的解决思路:
方法1:直接使用已绑定的后端变量
你已经通过assignTo="#{accountManagementBeanJSF.newLimitPS}"把前端#{limit.ps}的值绑定到了后端Bean的newLimitPS变量上,这其实是<a4j:actionparam>的核心用法,直接用这个变量就行,完全不用额外去获取:
public void setLimitDefaultValues(ActionEvent event) { if (customerProfile != null) { // 直接读取已经赋值好的newLimitPS LOGGER.info("newLimitPS value: " + this.newLimitPS); } }
方法2:从请求参数映射中获取
如果你确实需要通过name属性指定的参数名来取值,可以从当前请求的参数映射里提取:
import javax.faces.context.FacesContext; public void setLimitDefaultValues(ActionEvent event) { if (customerProfile != null) { FacesContext facesContext = FacesContext.getCurrentInstance(); // 这里的"minimum"就是你在a4j:actionparam里设置的name属性值 String attributeName2 = facesContext.getExternalContext() .getRequestParameterMap() .get("minimum"); LOGGER.info("Attribute minimum " + attributeName2); } }
补充说明:为什么原方法失效?
<a4j:actionparam>的name属性是用来构造AJAX请求参数的,它不会被添加到组件的attributes集合里。你通过event.getComponent()拿到的是<a4j:support>组件,它的属性集合里本来就没有"minimum"这个键,所以返回null是正常的。
另外可以确认下onclick事件是否正常触发了AJAX请求,ajaxSingle="true"这里是针对<h:inputText>的,不会影响参数传递,不用太担心。
内容的提问来源于stack exchange,提问作者Mary
相关产品推荐
相关产品推荐

