h:commandLink不会调用动作

时间:2016-05-31 14:26:27

标签: jsf jsf-2 commandlink

我有两个页面page1.xhtmlpage2.xhtml,其中包含简单的表单和h:commandLink。这些链接应调用bean void save1()中的方法void save2()SendPaper

第一个工作正常。它运行该方法并执行重定向到第2页。然而,第二个方法根本不会调用该方法。没有错误消息,没有服务器输出,没有。

我正在粘贴整个xhtml代码,因为我不知道这两个页面之间的区别是什么,为什么第一个有效,而第二个无效。

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>

豆:

@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
    }
...
}

更新:我认为它与复选框有关,即h:selectOneMenu。当我将其从page2.xhtml移至page1.xhtml时,page1.xhtml上的保存按钮停止工作。当我删除h:selectOneMenu时,按钮再次工作......

0 个答案:

没有答案