Spring 使用复合JSF组件时Bean范围丢失&;永远不会调用操作方法
我被要求重构以下代码,这些代码是有效的:Spring 使用复合JSF组件时Bean范围丢失&;永远不会调用操作方法,spring,jsf-2,scope,richfaces,composite-component,Spring,Jsf 2,Scope,Richfaces,Composite Component,我被要求重构以下代码,这些代码是有效的: <a4j:commandButton type="image" image="/someImage.gif" action="#{SomeViewController.someDeleteAction}" onclick="return confirm('#{msg['a.message']}');" render="someDataTableWithItems"> <f:setPropertyA
<a4j:commandButton type="image"
image="/someImage.gif"
action="#{SomeViewController.someDeleteAction}"
onclick="return confirm('#{msg['a.message']}');"
render="someDataTableWithItems">
<f:setPropertyActionListener
target="#{SomeViewModel.selectedItem}" value="#{item}" />
</a4j:commandButton>
..并将以前的a4j:commandButton
替换为:
<my:buttonConfirm type="image" id="someID"
image="/someImage.gif"
action="#{SomeViewController.someDeleteAction}"
message="#{msg['a.confirmation.message']}"
render="someDataTableWithItems"
tooltip="#{msg['a.tooltip.message']}">
<f:setPropertyActionListener for="someID"
target="#{SomeViewModel.selectedItem}" value="#{item}" />
</my:buttonConfirm>
虽然我尝试使用@ManagedBean
而不是@Component
,但应用程序给了我自动布线错误,所以我离开了@Component
。这个范围现在被保留了下来。
我不知道以这种方式混合使用JSF和Spring注释是否会产生任何其他后果
但是,SomeViewModel
的作用域现在很好,从未设置f:setPropertyActionListener
目标,也从未调用动作{SomeViewController.someDeleteAction}
。我无法调试它(不确定在何处放置断点以查看中间发生的情况)
提前感谢您的帮助。该
不会附加到组件id,而是附加到ActionSource。看看如何做
编辑:
基本上,您将拥有
<cc:interface>
<cc:actionSource name="source" targets="buttonId" />
…
</cc:interface>
<cc:implementation>
<a4jcommandButton id="buttonId" … />
</cc:implementation>
…
通过
指出,在尝试了一些建议并自己做了一些研究之后,我得出结论,在rich:popupanel
内部使用a4j:commandButton
时存在某种问题。未调用操作方法,并且未设置在f:setPropertyActionListener
上定义的属性。我一直无法找出到底是什么在那里迷失了方向
我在互联网上看到过弹出窗口中有a4j:commandButton
的例子,所以我不确定这是否是由我的依赖性引起的。我使用JSFAPI 2.1.19、jsf impl 2.1.19-jbossorg-1和richfaces 4.3.5.Final
这是我最后做的变通方法。我希望它能对任何与我有同样问题的人有所帮助:
confirButton.xhtml
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:a4j="http://richfaces.org/a4j"
xmlns:rich="http://richfaces.org/rich"
xmlns:cc="http://java.sun.com/jsf/composite">
<cc:interface>
<cc:attribute name="message"
default="Some message" />
<cc:attribute name="header"
default="Some header" />
<cc:attribute name="cancelBtn" default="No" />
<cc:attribute name="confirmBtn" default="Yes" />
<cc:attribute name="type" default="submit" />
<cc:attribute name="icon" required="false" />
<cc:attribute name="image" required="false" />
<cc:attribute name="action"
targets="popupConfirmButton" />
<cc:actionSource name="confirmListeners"
targets="popupConfirmButton" />
</cc:interface>
<cc:implementation>
<a4j:commandButton type="#{cc.attrs.type}"
image="#{cc.attrs.image}"
oncomplete="#{rich:component('popupConfirm')}.show()">
</a4j:commandButton>
<a4j:commandButton id="popupConfirmButton"
style="visibility: hidden;" render="#{cc.attrs.render}">
</a4j:commandButton>
<rich:popupPanel id="popupConfirm" header="#{cc.attrs.header}"
autosized="true" width="475" resizeable="false">
<f:facet name="controls">
<h:outputLink value="#"
onclick="#{rich:component('popupConfirm')}.hide(); return false;" />
</f:facet>
<h:panelGrid columns="2">
<h:graphicImage value="#{cc.attrs.icon}" height="64" width="64" />
<p>#{cc.attrs.message}</p>
</h:panelGrid>
<br />
<div align="right">
<a4j:commandButton value="#{cc.attrs.confirmBtn}"
onclick="#{rich:element('popupConfirmButton')}.click();
#{rich:component('popupConfirm')}.hide();" />
 
<h:commandButton value="#{cc.attrs.cancelBtn}"
onclick="#{rich:component('popupConfirm')}.hide(); return false;" />
</div>
</rich:popupPanel>
</cc:implementation>
</html>
#{cc.attrs.message}
组件使用情况
<my:confirmButton type="image" image="someButtonImage.gif"
icon="someWarningImage.gif"
action="#{SomeViewController.doStuff}"
message="Some message"
render="someComponentID">
<f:setPropertyActionListener for="confirmListeners"
target="#{SomeViewModel.someProperty}" value="foo" />
</my:confirmButton>
我不知道如何实施你的建议。谢谢你的帮助,@Makhiel。我已经尝试了与您解释的相同的方法,但是既没有启动setter,也没有调用定义为action的函数。完成后,弹出窗口甚至没有关闭。当我在javascript控制台上看到这条消息时,我认为视图中有些东西丢失了:uncaughttypeerror:cannotcallmethod'hide'of undefined
。该隐藏函数可能是在a4j:commandButton
的oncompletion
属性上调用的hide
弹出函数。关于可能发生的事情还有其他想法吗?提前谢谢。你好,马基尔。我已经尝试过你的建议,在复合组件中的rich:popupanel
外有一个a4j:commandButton
,它按预期工作。但是,在弹出窗口中使用相同按钮时,会向服务器发送非回发HTTP POST(视图中的所有其他弹出窗口仅发送回发),从而执行视图初始化方法。虽然它的执行似乎很顺利,没有丢失任何bean状态,但没有触发a4j:commandButton
操作方法。其他所有按钮都使用actionListener
,其中一个按钮使用action
,改为使用actionListener
试试。嗨@Makhiel,它也不起作用。我做了一个变通方法,现在我将发布。再次感谢你的帮助。
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:a4j="http://richfaces.org/a4j"
xmlns:rich="http://richfaces.org/rich"
xmlns:cc="http://java.sun.com/jsf/composite">
<cc:interface>
<cc:attribute name="message"
default="Some message" />
<cc:attribute name="header"
default="Some header" />
<cc:attribute name="cancelBtn" default="No" />
<cc:attribute name="confirmBtn" default="Yes" />
<cc:attribute name="type" default="submit" />
<cc:attribute name="icon" required="false" />
<cc:attribute name="image" required="false" />
<cc:attribute name="action"
targets="popupConfirmButton" />
<cc:actionSource name="confirmListeners"
targets="popupConfirmButton" />
</cc:interface>
<cc:implementation>
<a4j:commandButton type="#{cc.attrs.type}"
image="#{cc.attrs.image}"
oncomplete="#{rich:component('popupConfirm')}.show()">
</a4j:commandButton>
<a4j:commandButton id="popupConfirmButton"
style="visibility: hidden;" render="#{cc.attrs.render}">
</a4j:commandButton>
<rich:popupPanel id="popupConfirm" header="#{cc.attrs.header}"
autosized="true" width="475" resizeable="false">
<f:facet name="controls">
<h:outputLink value="#"
onclick="#{rich:component('popupConfirm')}.hide(); return false;" />
</f:facet>
<h:panelGrid columns="2">
<h:graphicImage value="#{cc.attrs.icon}" height="64" width="64" />
<p>#{cc.attrs.message}</p>
</h:panelGrid>
<br />
<div align="right">
<a4j:commandButton value="#{cc.attrs.confirmBtn}"
onclick="#{rich:element('popupConfirmButton')}.click();
#{rich:component('popupConfirm')}.hide();" />
 
<h:commandButton value="#{cc.attrs.cancelBtn}"
onclick="#{rich:component('popupConfirm')}.hide(); return false;" />
</div>
</rich:popupPanel>
</cc:implementation>
</html>
<my:confirmButton type="image" image="someButtonImage.gif"
icon="someWarningImage.gif"
action="#{SomeViewController.doStuff}"
message="Some message"
render="someComponentID">
<f:setPropertyActionListener for="confirmListeners"
target="#{SomeViewModel.someProperty}" value="foo" />
</my:confirmButton>