有没有办法在不使用托管属性的情况下将输入值作为操作的参数传递?
即
<h:form>
<h:inputText id="input" />
<h:commandButton action="#{someBean.doSome(input)}" />
</h:form>
最佳答案
是的,在表单提交期间,JSF 组件状态已经存在。只需通过 binding
属性将输入组件绑定(bind)到 View ,该属性将引用 UIInput
实例,它又具有一个 getValue()
方法,用于检索输入值(以便您可以将其作为操作方法参数传递):
<h:form>
<h:inputText ... binding="#{input}" />
<h:commandButton ... action="#{someBean.doSome(input.value)}" />
</h:form>
然而,这种方法的正确性非常值得怀疑,并且取决于具体的功能需求。这种方法基本上将 View 与模型紧密耦合,因此被认为是一种不好的做法。
另请参阅:
关于jsf - 直接传递输入值作为操作方法参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18340851/