0

I have two pages, page1.xhtml and page2.xhtml with simple forms and h:commandLinks. Those links should invoke methods void save1() and void save2() inside the bean SendPaper.

The first one works fine. It runs the method and performs the redirection to the Page 2. The second one, however, won't invoke the method at all. No error messages, no server output, nothing.

I am pasting the whole xhtml code, because I have no idea what's the difference between the two pages and why the first one works while the second one doesn't.

page1.xhtml:

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://xmlns.jcp.org/jsf/html"
      xmlns:f="http://xmlns.jcp.org/jsf/core"
      xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
      xmlns:p="http://primefaces.org/ui">
    <h:head>
        <title>Facelet Title</title>
    </h:head>
    <h:body>        
        <f:metadata>
            <f:viewParam name="conferenceId" value="#{sendPaper.conferenceId}" />
            <f:viewAction action="#{sendPaper.init}" />
        </f:metadata>        
        <h:head>
            <title><h:outputText value="#{msgs.site_title}"/></title>
        </h:head>
        <ui:composition template="/resources/templates/template.xhtml">
            <ui:define name="mainStripe">
                <h:panelGrid columns="2">
                    <h:outputLabel value="#{msgs.conference_name}"/>
                    <h:outputText value="#{sendPaper.conference.title}"/>
                    <h:outputLabel value="#{msgs.place}"/>
                    <h:outputText value="#{sendPaper.conference.place}"/>
                    <h:outputLabel value="#{msgs.start_time}"/>
                    <h:outputText value="#{sendPaper.conference.startTime}"/>
                    <h:outputLabel value="#{msgs.submit_start}"/>
                    <h:outputText value="#{sendPaper.conference.submitStart}"/>
                    <h:outputLabel value="#{msgs.submit_end}"/>
                    <h:outputText value="#{sendPaper.conference.submitEnd}"/>
                </h:panelGrid>
                <h:form class="formFullWidth">
                    <h3><h:outputLabel value="#{msgs.basic_paper_data}"/></h3>
                    <h:panelGrid columns="2">
                        <h:outputLabel value="ID" rendered="#{sendPaper.paper.id>0}"/>
                        <h:outputText value="#{sendPaper.paper.id}" rendered="#{sendPaper.paper.id>0}"/>
                        <h:outputLabel value="#{msgs.title}"/>
                        <h:inputText value="#{sendPaper.paper.title}"/>
                        <h:outputLabel value="#{msgs.keywords}"/>
                        <h:inputText value="#{sendPaper.paper.keywords}"/>
                        <h:outputText value="#{msgs.paper_abstract}"/>
                        <h:inputTextarea value="#{sendPaper.paper.paperAbstract}" />
                        <h:outputText value="#{msgs.main_author}"/>
                        <h:selectOneMenu value="#{sendPaper.paper.mainAuthor}">
                            <f:selectItems value="#{sendPaper.authors}" var="row" itemLabel="#{row.lastName} #{row.firstName}" itemValue="#{row.id}"/>
                        </h:selectOneMenu>      
                    </h:panelGrid>
                    <h:commandLink value="#{msgs.save}" action="#{sendPaper.save1}" styleClass="commandLink"/>
                    <h:commandLink value="#{msgs.back}" action="adminConferenceList" styleClass="commandLink"/>
                </h:form>
            </ui:define>
        </ui:composition>
    </h:body>
</html>

page2.xhtml:

 <?xml version='1.0' encoding='UTF-8' ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml"
          xmlns:h="http://xmlns.jcp.org/jsf/html"
          xmlns:f="http://xmlns.jcp.org/jsf/core"
          xmlns:ui="http://xmlns.jcp.org/jsf/facelets">
        <h:head>
            <title>Facelet Title</title>
        </h:head>
        <h:body>        
            <f:metadata>
                <f:viewParam name="paperId" value="#{sendPaper.paperId}" />
                <f:viewAction action="#{sendPaper.init}" />
            </f:metadata>        
            <h:head>
                <title><h:outputText value="#{msgs.site_title}"/></title>
            </h:head>
            <ui:composition template="/resources/templates/template.xhtml">
                <ui:define name="mainStripe">                
                    <h:form class="formFullWidth">
                        <h:panelGrid columns="2">
                            <h:outputText value="Paper Id"/>
                            <h:outputText value="#{sendPaper.paperId}"/>
                        </h:panelGrid>
                        <h3><h:outputLabel value="#{msgs.select_authors}"/></h3>
                        <h:panelGrid columns="2">
                            <h:outputText value="#{msgs.select_authors}"/>
                            <h:selectManyCheckbox value="#{sendPaper.authorsArray}">
                                <f:selectItems value="#{sendPaper.authorsArray}" var="row" itemValue="#{row.id}" itemLabel="#{row.lastName} #{row.firstName}"/>
                            </h:selectManyCheckbox>      
                        </h:panelGrid>
                        <h:commandButton value="#{msgs.save}" actionListener="#{sendPaper.save2}" styleClass="commandLink"/>
                        <h:commandLink value="#{msgs.back}" action="adminConferenceList" styleClass="commandLink"/>
                    </h:form>
                </ui:define>
            </ui:composition>
        </h:body>
    </html>

Bean:

@ManagedBean
@ViewScoped
public class SendPaper implements Serializable {

    private Conference conference;
    private Paper paper;
    private long conferenceId;
    private long paperId;
    private LinkedList<User> authors;
    private User[] authorsArray;

    public SendPaper() {

    }
...
    public void init() {
        System.out.println("DEBUG ::: init start");
        if (conferenceId > 0) {
            conference = ConferenceDAO.getById(conferenceId);
        }
        if (paperId > 0) {
            paper = PaperDAO.getById(paperId);
        } else {
            paper = new Paper();
        }
        authors = UserDAO.getAllOrderBy("last_name");
        int size = authors.size();
        authorsArray = new User[size];
        for (int i = 0; i < size; authorsArray[i] = authors.get(i++));
        System.out.println("DEBUG ::: init end");
    }

public void save1() {
        System.out.println("DEBUG ::: save1 start");
        // Do something useful
        System.out.println("DEBUG ::: save1 end");
        // Redirect user to the Page 2
    }

public void save2() {
        System.out.println("DEBUG ::: save2 start");
        for (int i = 0; i < authorsArray.length; i++) {
            // Do something useful
        }            
        System.out.println("DEBUG ::: save2 end");
        // Redirect user to the Page 3
    }
...
}

Update: I think it has something to do with checkboxes i.e. h:selectOneMenu. When I moved it from page2.xhtml to page1.xhtml, the Save button on page1.xhtml stopped working. When I removed h:selectOneMenu, the button worked again...

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
vtomic85
  • 590
  • 1
  • 11
  • 32
  • I'm think you'd want to use styleClass and not class on the h:form's. Also try with save2() (with parentheses) – Jaqen H'ghar May 31 '16 at 15:50
  • You have to use **action** attribute instead of **actionListener**. – ujulu May 31 '16 at 16:22
  • Nope... I changed class to styleClass, actionListenet to action, save2 to save2() and nothing happens... – vtomic85 Jun 01 '16 at 05:29
  • @BalusC, I think that my problem is number 4 from the solution that you had posted... But I'm still unsure how to solve this. I'd be thankful for some additional hints. – vtomic85 Jun 01 '16 at 18:08
  • No, it's number 3. And, set project stage to development and pay attention to server logs as instructed in 1st paragraph of the duplicate. Any missing conversion/validation messages will be logged there. – BalusC Jun 01 '16 at 18:13
  • @BalusC, Thanks, that was useful. I added a Long[] array to use as a value for h:selectManyCheckbox. Now my button works and I have the IDs in an array. Good enough :). – vtomic85 Jun 01 '16 at 19:51

0 Answers0