【问题标题】:JSF : One Text Input Box Is Controlling All My ButtonsJSF:一个文本输入框控制着我所有的按钮
【发布时间】:2013-04-16 22:18:34
【问题描述】:

我在这里尝试将 JSF 与三个不同的输入文本框一起使用,这些输入文本框与三个单独的按钮一起使用。

但是,我发现前两个文本框根本不起作用,最后一个文本框的用户输入文本是唯一可以与所有三个按钮一起使用的文本。

谁能告诉我我的代码有什么问题?谢谢。

            <h:panelGrid columns="1" style="padding-left:20px;" cellspacing="30">
            <h:panelGroup style="color: black; font-size:medium;" >
                <b style="color: black;">Search for species reviews</b> by name (scientific or common)<br/>
                    <p>&nbsp;&nbsp;&nbsp;&nbsp;<h:inputText id="name" size="20" value="#{searchBean.inputName}" required="false" />                                 
                    &nbsp;&nbsp;<h:commandLink action="#{searchBean.searchByName}" class="buttons">Search</h:commandLink></p>
                <h:message for="name" errorClass="errors" style="color: red;" />   
                </h:panelGroup>

                <h:panelGroup style="color: black; font-size:medium;">
                <p><b style="color: black; font-size:medium; padding-bottom:25px;">Find Species Reviews</b><br/></p>
                    &nbsp;&nbsp;&nbsp;&nbsp;<h:inputText id="searchOne" size="20" value="#{searchBean.inputName}" required="false" /> &nbsp; <h:commandLink action="#{searchBean.searchByOther}" class="buttons">Enter Species</h:commandLink>&nbsp; or &nbsp;&nbsp;<h:commandLink action="#{searchBean.reviewSearchAction}" class="buttons
">Andvanced Search</h:commandLink>
                </h:panelGroup>

                <h:panelGroup style="color: black; font-size:medium;">
                <p><b style="color: black; font-size:medium;">Find Fire Studies</b><br/></p>
                    &nbsp;&nbsp;&nbsp;&nbsp;<h:inputText id="searchTwo" size="20" value="#{searchBean.inputName}" required="false" /> &nbsp; <h:commandLink action="#{searchBean.searchByFireStudy}" class="buttons">Enter Species</h:commandLink>&nbsp; or &nbsp; <h:commandLink action="#{searchBean.fireStudySearchAction}" class="buttons">Advanced Search</h:commandLink>
                </h:panelGroup>

【问题讨论】:

  • 是否所有代码都包含在单个 &lt;h:form&gt; 中?
  • 是的,看起来是这样。是这个问题吗?
  • 做一件事,为每个&lt;h:panelGroup&gt;s 使用不同的&lt;h:form&gt;s。
  • 好的,所以我将它们添加到每个 中,现在它对任何按钮都没有任何作用。
  • 你应该从代码中删除之前的&lt;h:form&gt;,不应该有嵌套的&lt;h:form&gt;,也就是说不应该有外部的&lt;h:form&gt;&lt;h:form&gt; 里面的 &lt;h:form&gt; 不起作用。查看以下问题中给出的答案stackoverflow.com/questions/2118656/…

标签: html jsf jsf-2


【解决方案1】:

将所有内容放在同一个形式中没有错。但是,由于您对所有三个&lt;h:inputText&gt; 使用相同的value="#{searchBean.inputName}",所以当您提交整个表单时,最后一个&lt;h:inputText&gt; 的值将覆盖前两个的值。

要解决这个问题,我认为您可以尝试以下方法之一:

  1. 使用 bean 的不同属性来保存不同输入文本的值(即,第一个使用 inputName1,第二个使用 &lt;h:inputText&gt;,第二个使用 inputName2,依此类推)。
  2. 如果您在同一页面上呈现结果,您可以使用&lt;f:ajax&gt; 并使用其execute 属性告诉JSF 要提交什么。您的按钮将如下所示:

    <h:inputText id="input1" value="searchBean.inputName" />
    
    <h:commandLink value="Search 1" action="#{searchBean.searchByName}" class="buttons">
        <f:ajax execute="input1" render="result" />
    </h:commandLink>
    

【讨论】:

  • “结果”是否只是它应该从 faces_config 转到的页面?
【解决方案2】:

您需要为每个&lt;h:panelGroup&gt;s 使用单独的&lt;h:form&gt;s,因为&lt;h:commandLink&gt; 需要位于&lt;h:form&gt; 内,如果单个&lt;h:commandLink&gt; 中有多个&lt;h:commandLink&gt; 按钮,则每个&lt;h:form&gt;按钮将提交相同的表单。

        <h:panelGrid columns="1" style="padding-left:20px;" cellspacing="30">
<h:form>
        <h:panelGroup style="color: black; font-size:medium;" >
            <b style="color: black;">Search for species reviews</b> by name (scientific or common)<br/>
                <p>&nbsp;&nbsp;&nbsp;&nbsp;<h:inputText id="name" size="20" value="#{searchBean.inputName}" required="false" />                                 
                &nbsp;&nbsp;<h:commandLink action="#{searchBean.searchByName}" class="buttons">Search</h:commandLink></p>
            <h:message for="name" errorClass="errors" style="color: red;" />   
            </h:panelGroup>
</h:form>
<h:form>
            <h:panelGroup style="color: black; font-size:medium;">
            <p><b style="color: black; font-size:medium; padding-bottom:25px;">Find Species Reviews</b><br/></p>
                &nbsp;&nbsp;&nbsp;&nbsp;<h:inputText id="searchOne" size="20" value="#{searchBean.inputName}" required="false" /> &nbsp; <h:commandLink action="#{searchBean.searchByOther}" class="buttons">Enter Species</h:commandLink>&nbsp; or &nbsp;&nbsp;<h:commandLink action="#{searchBean.reviewSearchAction}" class="buttons">Andvanced Search</h:commandLink>
            </h:panelGroup>
</h:form>
<h:form>
            <h:panelGroup style="color: black; font-size:medium;">
            <p><b style="color: black; font-size:medium;">Find Fire Studies</b><br/></p>
                &nbsp;&nbsp;&nbsp;&nbsp;<h:inputText id="searchTwo" size="20" value="#{searchBean.inputName}" required="false" /> &nbsp; <h:commandLink action="#{searchBean.searchByFireStudy}" class="buttons">Enter Species</h:commandLink>&nbsp; or &nbsp; <h:commandLink action="#{searchBean.fireStudySearchAction}" class="buttons">Advanced Search</h:commandLink>
            </h:panelGroup>
</h:form>

【讨论】:

    猜你喜欢
    • 2018-02-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-30
    • 2011-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多