1

I'm trying to use JSF here with three different input text boxes that work with three separate buttons.

However, I'm finding that the first two text boxes aren't working at all and that the very last text box's user input text is the only one that can actually be used with all three of the buttons.

Can somebody tell me what's wrong with my code? Thanks.

            <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>
UndefinedReference
  • 1,223
  • 4
  • 22
  • 52
  • Is all the code wrapped inside a singe ``? – Wizard Sultan Apr 16 '13 at 22:22
  • Yeah, it looks like it is. Is that the issue? – UndefinedReference Apr 16 '13 at 22:26
  • Do onething, use different ``s for each of the ``s. – Wizard Sultan Apr 16 '13 at 22:27
  • Okay, so I added them to every , now it does nothing with any button. – UndefinedReference Apr 16 '13 at 22:41
  • You should remove the previous `` from the code, there should be no nested ``,in other words there should be no outer ``. `` inside `` doesn't work. Check out the answer given in the following question http://stackoverflow.com/questions/2118656/hcommandlink-hcommandbutton-is-not-being-invoked – Wizard Sultan Apr 16 '13 at 22:47
  • Amlan, if you submit this as an answerm, I will accept it. It was the tags, they were missing and in the wrong order. Thanks. – UndefinedReference Apr 16 '13 at 22:51
  • If you bind one bean property to 3 different input text boxes, the last one will always override the previous values. So instead of having multiple forms (not advisable in MyFaces Trinidad, dont know about other JSF libraries), you could just bind each input text box to its own property. – darthbinamira Apr 17 '13 at 08:36

2 Answers2

2

Putting everything in the same form is nothing wrong. However, since you are using the same value="#{searchBean.inputName}" for all the three <h:inputText>, when you submit the whole form, the value of the last <h:inputText> will override the value of the previous two.

To solve the issue, I think you can try 1 of the following ways:

  1. Use different properties of the bean to hold the value of different input texts (i.e. use inputName1 for the 1st <h:inputText>, inputName2 for the 2nd one and so on).
  2. If you're rendering the result on the same page, you can use <f:ajax> and tell JSF what to submit using its execute attribute. Your button will look like this:

    <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>
    
Mr.J4mes
  • 9,168
  • 9
  • 48
  • 90
1

You need to use separate <h:form>s for each of the <h:panelGroup>s because <h:commandLink> needs to be inside a <h:form>, if there are multiple <h:commandLink> buttons in single <h:form>, each of the buttons will submit the same form.

        <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>
Wizard Sultan
  • 830
  • 7
  • 22
  • 45