使用来自多个 selectOneMenus 和按钮的内容填充 inputTextArea
我想构建一个小型 UI,它将使用户能够以更简单的方式编写一些数学公式(因此是 UI),并能够将它们保存到 .txt (或类似的文件)文件中。
例如:我有 3 个提供各种选项的选择框、一些代表数学运算的按钮和一个 inputTextArea。到目前为止,我已经成功链接了每个 selectOneMenu 和几个 inputText 中的选定内容,并将它们与我需要的内容连接起来,并将创建的数学公式写入 .txt 文件。
现在我想为用户提供查看他正在编写的公式的可能性(通过选择内容并按下按钮)。到目前为止,我一直在尝试使用 inputTextArea 来做到这一点,但我所能做到的最好的是将所选内容从仅一个 selectOneMenu 传输到 textArea,然后我尝试添加用户的每个操作对文本区域的操作失败了(有人建议使用
但我一无所获,我尝试将用于填充菜单的所有 bean 合并到一个 bean 中,然后以某种方式去从那里,但这也不起作用)。
到目前为止我一直在研究的 JSF 部分的示例,带有 2 个选择框和 TextArea:
<h:outputText value="Formula Name"></h:outputText>
<h:inputText value="#{output.formulaName}" id="formulaName"></h:inputText>
<br /><br />
The formula so far:
<h:inputTextarea value="#{output.propertyReferenceValue}"></h:inputTextarea>
<br/><br/><br/>
Property1:
<h:selectOneMenu value ="#{output.propertyReferenceValue}" id="selectTwo"
valueChangeListener="#{output.propertyReferenceValueChanged}" onchange="submit()">
<f:selectItems value="#{property.propertyReference}"/>
</h:selectOneMenu>
<br/><br/>
Property2:
<h:selectOneMenu value ="#{output.property2ReferenceValue}" id="selectThree"
valueChangeListener="#{output.property2ReferenceChangedValue}" onchange="submit()">
<f:selectItems value="#{property2.property2Refference}" />
</h:selectOneMenu>
任何帮助将不胜感激。
附言。我对 JSF 非常陌生(大约 2 天前开始),所以我什至会很感激有人指出一些可能涵盖此问题或类似问题的文档。
I want to build a small UI which will offer the user the posibility to write some math formulas in an easier way(hence the UI) and be able to save them to a .txt (or something similar) file.
For example: I have 3 selectboxes which offer a variety of options, some buttons which represent math operations and an inputTextArea. So far I've managed to link selected content from each selectOneMenu and from a few inputText's and concatenate them with what i need and write the created math formulas to a .txt file.
Now I want to offer the user the possibility to see the formula he is writing(by selecting content and pressing buttons) as he is doing it. So far I've been trying to do that using an inputTextArea but the best I managed was to transfer the selected content from only one selectOneMenu to the textArea and after that all my attempts to add every action the user makes to the text area have failed(someone suggested using <h:panelGroup>
but I got nowhere with that, I tried to combine all the beans I used to fill the menus into one bean and somehow go from there , but that didn't work either).
A sample of the JSF part I've been working on so far, with 2 selectboxes and the TextArea:
<h:outputText value="Formula Name"></h:outputText>
<h:inputText value="#{output.formulaName}" id="formulaName"></h:inputText>
<br /><br />
The formula so far:
<h:inputTextarea value="#{output.propertyReferenceValue}"></h:inputTextarea>
<br/><br/><br/>
Property1:
<h:selectOneMenu value ="#{output.propertyReferenceValue}" id="selectTwo"
valueChangeListener="#{output.propertyReferenceValueChanged}" onchange="submit()">
<f:selectItems value="#{property.propertyReference}"/>
</h:selectOneMenu>
<br/><br/>
Property2:
<h:selectOneMenu value ="#{output.property2ReferenceValue}" id="selectThree"
valueChangeListener="#{output.property2ReferenceChangedValue}" onchange="submit()">
<f:selectItems value="#{property2.property2Refference}" />
</h:selectOneMenu>
Any help would be appreciated.
PS. I'm very new to JSF(started about 2 days ago) so I will appreciate even being pointed to some documentation which might cover this or similar problems.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您基本上在这里滥用了
valueChangeListener
。您对值更改并不完全感兴趣(即您对新旧值都不感兴趣),您只对提交的值感兴趣。当您没有/不能/不会使用基于 ajax 的组件库时,JSF 1.x 中可以容忍这种情况。有一些(相当复杂的)方法可以使用valueChangeListener
来解决您的特定问题,但是在带有内置 ajax 功能的 JSF 2.x 中,这确实不能再这样做了。您应该使用
标签来引入异步提交和部分更新。重写你的观点如下:
仅此而已。您的
#{output}
bean 应通过@ViewScoped
放入视图范围内。不需要额外的侦听器方法。仅当您想在显示值之前对其进行操作时,才需要一个 ajax 侦听器。例如,使用eg(这个启动示例在逗号上进行了简单的连接,但是您可以完全自由地按照您想要的方式构建
公式
值):You're basically abusing the
valueChangeListener
here. You're not exactly interested in the value change (i.e. you aren't interested in both the old and new value), you're only interested in the submitted value. This was tolerated in JSF 1.x when you didn't/can't/won't use an ajax based component library. There are (rather complicated) ways to fix your particular problem with thevalueChangeListener
, but in JSF 2.x, which ships with builtin ajax capabilities, this really can't be done that way anymore. You should rather use the<f:ajax>
tag to introduce asynchronous submits and partial updates.Rewrite your view as follows:
That's all. Your
#{output}
bean should be put in the view scope by@ViewScoped
. No addidional listener methods are necessary. Only if you ever want to manipulate the values before displaying it, then you need an ajax listener. E.g.with e.g. (this kickoff example does a simple concatenation on comma, you've however the full freedom to build the
formula
value the way you want):