简体   繁体   English

如何使用commandButton在JSF中的页面之间导航?

[英]How to navigate between pages in JSF using commandButton?

I have a list of Strings (in my case the names of Projects) displayed using JSF, with each String having a corresponding 'view' button created in JSF. 我有一个使用JSF显示的字符串列表(在我的情况下是项目的名称),每个字符串都有一个在JSF中创建的相应“视图”按钮。 When the user presses the view button, I want to navigate to a new page that shows a description for the project in question. 当用户按下查看按钮时,我想导航到一个新页面,该页面显示了有关项目的描述。 This is achieved by passing the project_id through the method as shown below. 这是通过将project_id通过如下所示的方法来实现的。 I know how to navigate between JSF pages using faces-config by navigating based on the string returned (ie return "success"). 我知道如何通过基于返回的字符串进行导航(即返回“成功”),使用faces-config在JSF页面之间进行导航。 The problem that I am having is that the method I am calling is already displaying a string (that is dynamic) in it's return type. 我遇到的问题是我正在调用的方法已经在其返回类型中显示了一个字符串(动态的)。 As my method already displays a string, how do I configure this with JSF, so that I navigate to the appropriate page. 由于我的方法已经显示了一个字符串,因此如何使用JSF进行配置,以便导航至相应的页面。 The code below shows the problem more clearly. 下面的代码更清楚地显示了问题。

xhtml file: xhtml文件:

        <tr>
            <td><H2>Past Projects</H2>
            <h:dataTable value="#{projectBean.projectList}" var="p" border="1">

                <h:column>
                <f:facet name="header">
                Project Name
                </f:facet>
                #{p.projectName}
                <h:commandButton id="viewProject" action="#{projectBean.displayProjectDetails(p.project_id)}" value="View">
               </h:commandButton>
                </h:column>
            </h:dataTable>

            </td>
        </tr>

Java Class: Java类:

public String displayProjectDetails(int id) {
    factory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);
    EntityManager em = factory.createEntityManager();   

        em.getTransaction().begin();

        Query myQuery = em.createQuery("SELECT p FROM Projects p WHERE p.project_id=:projectId");
        myQuery.setParameter("projectId", id);
        List<Projects> currentProject=myQuery.getResultList();

        em.getTransaction().commit();
        em.close();

        String projectName=currentProject.get(0).getProjectName();
        String projectDescription=currentProject.get(0).getProjectDescription();

        String output=(projectName+"<br/>"+projectDescription+"<br/>");

        return output;

    } 

XML file: XML档案:

//Faces file
 <navigation-case>
    <from-outcome>output</from-outcome>
    <to-view-id>/projectDescription.xhtml</to-view-id>
 </navigation-case>

To me this looks like some false design. 对我来说,这似乎是一些错误的设计。 You should use an action method which appropriately returns the navigation case string (or void if you want to stay on the page). 您应该使用一种操作方法,该方法适当地返回导航用例字符串(如果要保留在页面上,则为空)。 The method should not return anything else. 该方法不应返回任何其他内容。 The other string should be returned from a getter, as appropriate. 另一个字符串应根据需要从getter返回。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM