问题描述
我有一个名称为 controller
的get参数.当我尝试将其(使用 propertyChangeListener
)传递到支持bean时,我得到的是 null
而不是该参数的实际值:
I have a get-parameter with name controller
. When I try to pass it (with propertyChangeListener
) to my backing bean I get null
instead of the real value of that parameter:
<h:commandButton value="#{msg['mail.send']}" styleClass="mailbutton" action="#{mailSender.sendMail}">
<f:setPropertyActionListener target="#{mailSender.controllerName}" value="{#param.controller}"/>
</h:commandButton>
所以,我有两个问题:
- 用get-parameter值设置bean属性的正确方法是什么?实际上,我已经从
ExternalContext#getRequestParam
中获取了值,但是也许还有其他解决方案. - 一个更有趣的问题:为什么propertyActionListener在这里不起作用?它实际上是做什么的?(同样,我对此有一些想法,但是阅读更全面的解释会很不错).
- What is the proper way to set bean property with a get-parameter value?Actually, I've already get the value from
ExternalContext#getRequestParam
but maybe there are some other solutions. - More interesting question: why propertyActionListener didn't work here? What does it do actually? (again I have some thoughts about it but it would be nice to read more comprehensive explanations).
推荐答案
首先, h:commandButton
触发 POST
请求,而不是 GET 代码>请求.要触发
GET
请求,您需要 h:outputLink
.要设置 GET
参数,您需要 f:param
.要将其设置为bean属性,请将其定义为 faces-config.xml
中的< managed-property>
,其值为#{param.name}
.在此处中可以找到更多的提示和代码示例.
First, a h:commandButton
fires a POST
request, not a GET
request. To fire a GET
request, you need h:outputLink
. To set a GET
parameter, you need f:param
. To set it as a bean property, define it as <managed-property>
in faces-config.xml
with a value of #{param.name}
. More hints and code examples can be found here.
第二,您的代码无效. f:setPropertyActionListener
值应为#{param.controller}
.为了使其正常工作,在最初的请求期间,#{param.controller} 应该已经在那里.它不会像您期望的那样在后续请求中自动设置值.为此,您需要< h:inputHidden value =#{mailSender.controllerName}"/>
.
Second, your code is invalid. The f:setPropertyActionListener
value should have been #{param.controller}
. And to get it to work, the #{param.controller}
should already be there during the initial request. It does not automagically set the value in the subsequent request as you seem to expect. For that you need <h:inputHidden value="#{mailSender.controllerName}"/>
instead.
这篇关于如何将get-parameter传递给jsf中的backing bean?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!