简体   繁体   English

在JSF页面之间传递信息

[英]Passing information between JSF pages

I am developing an application where I have to show data from the database based on User Input (let's say field empid for employee id and I show some information from database based on this input). 我正在开发一个应用程序,在该应用程序中,我必须根据用户输入显示数据库中的数据(假设员工ID为空字段,并且根据此输入显示数据库中的一些信息)。 I am using JSF, PrimeFace and Hibernate. 我正在使用JSF,PrimeFace和Hibernate。

Now I have the index.xhtml page where I am taking input the empid. 现在,我有了index.xhtml页面,在该页面上输入空值。 After that, I have to redirect to another page page2.xhtml. 之后,我必须重定向到另一个页面page2.xhtml。

Here is a snippet from index.xhtml: 这是index.xhtml的片段:

     <h:form>
        <p>Employee ID:
            <p:inputText value="#{userBean.newuser.empid}" required="true"/></p>
     </h:form>
     <br />
     <h:form>
         <p:commandButton action="page2?faces-redirect=true" value="Enter" />      
     </h:form>  

But after re-directing to page2.xhtml, the empid input from index.xhtml page is lost. 但是在重定向到page2.xhtml之后,来自index.xhtml页的空输入丢失了。 I tried to read a few posts on this but was not able to understand. 我尝试阅读有关此内容的一些帖子,但听不懂。 Please help. 请帮忙。 I am sorry if it is a repeated question. 如果这是一个重复的问题,很抱歉。

I assume that viewscopes of managed bean in which you are insterested in are @RequestScope ? 我假设您感兴趣的托管bean的视域是@RequestScope You could specify more wide view scope for a managed bean, so it won't be destroyed after each request. 您可以为托管bean指定更大的视图范围,因此在每次请求后都不会销毁它。 However @RequestScope is really good when want to minimize session scope bloat. 但是,当要最小化会话范围膨胀时,@ @RequestScope确实很好。 To stay with this session scope you could use flash object which was introduced with JSF 2.0. 要保留此会话范围,可以使用JSF 2.0引入的flash对象。 Thanks to this object you can transfer data between requests. 由于有了这个对象,您可以在请求之间传输数据。 You could put following code snippet : 您可以输入以下代码段:

ExternalContext.getFlash().put("empid", newuser.getEmpid);

in your userBean . 在您的userBean After redirect you could retrieve that value on jsf page in the following way : 重定向后,您可以通过以下方式在jsf页面上检索该值:

#{flash.empid}

Or in your managed bean : 或在您的托管bean中:

ExternalContext.getFlash().get("empid");

More about flash with example can be found in this post or in JSF 2 Flash documentation 更多关于如flash都可以在此找到职位JSF 2的Flash文档

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

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