java 如何使用 JSF 2.0 在一个页面中使用多个表单?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6291247/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 15:13:33  来源:igfitidea点击:

How to use multiple forms in one page with JSF 2.0?

javajsf-2primefaces

提问by flash

I try to use multiple forms with JSF 2.0 in one page. I use PrimeFaces 3.0M1 and trying to build an application with tabs and one form per tab.

我尝试在一个页面中使用 JSF 2.0 的多个表单。我使用 PrimeFaces 3.0M1 并尝试构建一个带有选项卡和每个选项卡一个表单的应用程序。

I've got a page like the following:

我有一个如下所示的页面:

<html xmlns="http://www.w3.org/1999/xhtml"

xmlns:f="http://java.sun.com/jsf/core" xmlns:h="http://java.sun.com/jsf/html"                 
xmlns:ui="http://java.sun.com/jsf/facelets" 
xmlns:fn="http://java.sun.com/jsp/jstl/functions" 
xmlns:p="http://primefaces.prime.com.tr/ui">

<div>
<p:tabView>
<p:tab title="Form1">
<h:form id="form1">
    <p:inputText id="txtInput" value="#{bean1.inputText}" />
    <p:commandButton title="Submit" value="Submit" actionListener="#{controller1.submitValues}">
</h:form>
</p:tab>
<p:tab title="Form2">
<h:form id="form2">
    <p:inputText id="txtInput2" value="#{bean2.inputText}" />
    <p:commandButton title="Submit" value="Submit" actionListener="#{controller2.submitValues}">
</h:form>
</p:tab>
</p:tabView>
</div>  
</html>

If I click on the submit button in tab 1, everything works like excpected. But, if I click on the button at the second tab, the command will not be executed in controller2.

如果我单击选项卡 1 中的提交按钮,则一切正常。但是,如果我单击第二个选项卡上的按钮,该命令将不会在 controller2 中执行。

What is the problem here? If I bind the execution command of button2 to button1 the command in controller2 is executed correctly, so I can exclude that there is a problem in the backing beans.

这里有什么问题?如果我将button2的执行命令绑定到button1,那么controller2中的命令就可以正确执行,这样就可以排除backing beans的问题了。

How can I solve this?

我该如何解决这个问题?

采纳答案by maple_shaft

The Primefaces wizard and tabview components must be enclosed by a single form.

Primefaces 向导和 tabview 组件必须包含在单个表单中。

There is no reason that you cannot have multiple Submit buttons within a tabview or wizard like this. Your confusion on this is likely because you concerned about the other properties of your managed bean that do not appear in the currently viewing tab.

没有理由在这样的 tabview 或向导中不能有多个提交按钮。您对此的困惑可能是因为您担心未出现在当前查看选项卡中的托管 bean 的其他属性。

回答by amFroz

I think you can go for specifying "process" attribute of a button

我认为您可以指定按钮的“ process”属性

try this. change your <h:form>tag and replace it with <h:panelGrid>and give their ID's as form1and form2and and change your 2 buttons like this

试试这个。更改您的<h:form>标签并将其替换为<h:panelGrid>并将其 ID 指定为form1form2并像这样更改您的 2 个按钮

<p:commandButton title="Submit" value="Submit" actionListener="#controller1.submitValues}" process="@this,form1">

<p:commandButton title="Submit" value="Submit" actionListener="#controller2.submitValues}" process="@this,form2">

this will work. :) update me with your result

这会起作用。:) 用你的结果更新我

回答by Djidji Ndiandia

You can create a list of items and fill it by values you want to browse in your backing bean. Put the <h:form>tag in <ui:repeat>tag like this:

您可以创建一个项目列表,并用您想要在支持 bean 中浏览的值填充它。将<h:form>标签放入标签中,<ui:repeat>如下所示:

<ui:repeat var="item" value="#{backingBean.items}">
   <h:form>
    Put here the content of your form
   </h:form>
</ui:repeat>
<p:commandButton value="Submit" action="desired action"/>