Java JSF2:如何从SelectOneMenu将值刷新到支持bean中?

Java JSF2:如何从SelectOneMenu将值刷新到支持bean中?,java,ajax,jsf-2,url-parameters,prettyfaces,Java,Ajax,Jsf 2,Url Parameters,Prettyfaces,我使用漂亮的面孔和JSF2 <navigation-rule> <from-view-id>/home.xhtml</from-view-id> <navigation-case> <from-outcome>participant</from-outcome> <to-view-id>/addParticipant.xhtml<

我使用漂亮的面孔和JSF2

    <navigation-rule>
       <from-view-id>/home.xhtml</from-view-id> 
       <navigation-case>
          <from-outcome>participant</from-outcome>
          <to-view-id>/addParticipant.xhtml</to-view-id>
       </navigation-case>
    </navigation-rule>


    /*I use viewparam but if I don't use it it's working too, I clearly don't understand what viewparam is used for  */

    <f:metadata>
          <f:viewParam name="study_name" value="#{home.study_name}" required="true"/>
     </f:metadata>

   /* What is the best way to retrieve information? Then how i load param in a backing bean who this destination page ? */ 
   <h:outputFormat value="Hello,{0},{1},">
                <f:param value="#{home.study_name}" />
                <f:param value="#{param.studynameItem}" />
    </h:outputFormat>
我有一个选择菜单来选择一个值。 然后,我想通过命令按钮将所选值传递到第二个表单,然后再传递到另一个页面。我不想在URL中看到参数

    <navigation-rule>
       <from-view-id>/home.xhtml</from-view-id> 
       <navigation-case>
          <from-outcome>participant</from-outcome>
          <to-view-id>/addParticipant.xhtml</to-view-id>
       </navigation-case>
    </navigation-rule>


    /*I use viewparam but if I don't use it it's working too, I clearly don't understand what viewparam is used for  */

    <f:metadata>
          <f:viewParam name="study_name" value="#{home.study_name}" required="true"/>
     </f:metadata>

   /* What is the best way to retrieve information? Then how i load param in a backing bean who this destination page ? */ 
   <h:outputFormat value="Hello,{0},{1},">
                <f:param value="#{home.study_name}" />
                <f:param value="#{param.studynameItem}" />
    </h:outputFormat>
我不希望url是这样的:addpartpant.xhtml或addparticipant.xhtml?study_name=TEST或/Addparticpant/New/TEST

    <navigation-rule>
       <from-view-id>/home.xhtml</from-view-id> 
       <navigation-case>
          <from-outcome>participant</from-outcome>
          <to-view-id>/addParticipant.xhtml</to-view-id>
       </navigation-case>
    </navigation-rule>


    /*I use viewparam but if I don't use it it's working too, I clearly don't understand what viewparam is used for  */

    <f:metadata>
          <f:viewParam name="study_name" value="#{home.study_name}" required="true"/>
     </f:metadata>

   /* What is the best way to retrieve information? Then how i load param in a backing bean who this destination page ? */ 
   <h:outputFormat value="Hello,{0},{1},">
                <f:param value="#{home.study_name}" />
                <f:param value="#{param.studynameItem}" />
    </h:outputFormat>
我只想要这样的URL/Addparticipant/New,但如果我这样做,我就无法检索好的参数值,我只检索bean中默认设置的参数值,而不是页面刷新值

    <navigation-rule>
       <from-view-id>/home.xhtml</from-view-id> 
       <navigation-case>
          <from-outcome>participant</from-outcome>
          <to-view-id>/addParticipant.xhtml</to-view-id>
       </navigation-case>
    </navigation-rule>


    /*I use viewparam but if I don't use it it's working too, I clearly don't understand what viewparam is used for  */

    <f:metadata>
          <f:viewParam name="study_name" value="#{home.study_name}" required="true"/>
     </f:metadata>

   /* What is the best way to retrieve information? Then how i load param in a backing bean who this destination page ? */ 
   <h:outputFormat value="Hello,{0},{1},">
                <f:param value="#{home.study_name}" />
                <f:param value="#{param.studynameItem}" />
    </h:outputFormat>
更令人不安的是,当我更改页面中的默认值时,会出现一个按钮。它可以工作,但看起来参数的bean值没有影响,仍然保持默认值

    <navigation-rule>
       <from-view-id>/home.xhtml</from-view-id> 
       <navigation-case>
          <from-outcome>participant</from-outcome>
          <to-view-id>/addParticipant.xhtml</to-view-id>
       </navigation-case>
    </navigation-rule>


    /*I use viewparam but if I don't use it it's working too, I clearly don't understand what viewparam is used for  */

    <f:metadata>
          <f:viewParam name="study_name" value="#{home.study_name}" required="true"/>
     </f:metadata>

   /* What is the best way to retrieve information? Then how i load param in a backing bean who this destination page ? */ 
   <h:outputFormat value="Hello,{0},{1},">
                <f:param value="#{home.study_name}" />
                <f:param value="#{param.studynameItem}" />
    </h:outputFormat>
我该怎么做呢?谢谢你的帮助。我试着在ajax调用中设置bean中的值param,但实际上不起作用

    <navigation-rule>
       <from-view-id>/home.xhtml</from-view-id> 
       <navigation-case>
          <from-outcome>participant</from-outcome>
          <to-view-id>/addParticipant.xhtml</to-view-id>
       </navigation-case>
    </navigation-rule>


    /*I use viewparam but if I don't use it it's working too, I clearly don't understand what viewparam is used for  */

    <f:metadata>
          <f:viewParam name="study_name" value="#{home.study_name}" required="true"/>
     </f:metadata>

   /* What is the best way to retrieve information? Then how i load param in a backing bean who this destination page ? */ 
   <h:outputFormat value="Hello,{0},{1},">
                <f:param value="#{home.study_name}" />
                <f:param value="#{param.studynameItem}" />
    </h:outputFormat>
/*表单,当您更改 选择OneMenu可将值作为参数添加到第二个表单中*/

    <navigation-rule>
       <from-view-id>/home.xhtml</from-view-id> 
       <navigation-case>
          <from-outcome>participant</from-outcome>
          <to-view-id>/addParticipant.xhtml</to-view-id>
       </navigation-case>
    </navigation-rule>


    /*I use viewparam but if I don't use it it's working too, I clearly don't understand what viewparam is used for  */

    <f:metadata>
          <f:viewParam name="study_name" value="#{home.study_name}" required="true"/>
     </f:metadata>

   /* What is the best way to retrieve information? Then how i load param in a backing bean who this destination page ? */ 
   <h:outputFormat value="Hello,{0},{1},">
                <f:param value="#{home.study_name}" />
                <f:param value="#{param.studynameItem}" />
    </h:outputFormat>

/*第二种形式是从ajax注入要传递的参数 从下表中的移动事件调用*/

    <navigation-rule>
       <from-view-id>/home.xhtml</from-view-id> 
       <navigation-case>
          <from-outcome>participant</from-outcome>
          <to-view-id>/addParticipant.xhtml</to-view-id>
       </navigation-case>
    </navigation-rule>


    /*I use viewparam but if I don't use it it's working too, I clearly don't understand what viewparam is used for  */

    <f:metadata>
          <f:viewParam name="study_name" value="#{home.study_name}" required="true"/>
     </f:metadata>

   /* What is the best way to retrieve information? Then how i load param in a backing bean who this destination page ? */ 
   <h:outputFormat value="Hello,{0},{1},">
                <f:param value="#{home.study_name}" />
                <f:param value="#{param.studynameItem}" />
    </h:outputFormat>

PrettyFaces配置文件:

    <navigation-rule>
       <from-view-id>/home.xhtml</from-view-id> 
       <navigation-case>
          <from-outcome>participant</from-outcome>
          <to-view-id>/addParticipant.xhtml</to-view-id>
       </navigation-case>
    </navigation-rule>


    /*I use viewparam but if I don't use it it's working too, I clearly don't understand what viewparam is used for  */

    <f:metadata>
          <f:viewParam name="study_name" value="#{home.study_name}" required="true"/>
     </f:metadata>

   /* What is the best way to retrieve information? Then how i load param in a backing bean who this destination page ? */ 
   <h:outputFormat value="Hello,{0},{1},">
                <f:param value="#{home.study_name}" />
                <f:param value="#{param.studynameItem}" />
    </h:outputFormat>

页面结果addParticipant.xhtml:

    <navigation-rule>
       <from-view-id>/home.xhtml</from-view-id> 
       <navigation-case>
          <from-outcome>participant</from-outcome>
          <to-view-id>/addParticipant.xhtml</to-view-id>
       </navigation-case>
    </navigation-rule>


    /*I use viewparam but if I don't use it it's working too, I clearly don't understand what viewparam is used for  */

    <f:metadata>
          <f:viewParam name="study_name" value="#{home.study_name}" required="true"/>
     </f:metadata>

   /* What is the best way to retrieve information? Then how i load param in a backing bean who this destination page ? */ 
   <h:outputFormat value="Hello,{0},{1},">
                <f:param value="#{home.study_name}" />
                <f:param value="#{param.studynameItem}" />
    </h:outputFormat>

Prettyfaces模式来执行导航。因此,您可以在
h:commandButton
操作方法中调用托管bean方法,而不是返回导航结果本身并将参数放入:

    <navigation-rule>
       <from-view-id>/home.xhtml</from-view-id> 
       <navigation-case>
          <from-outcome>participant</from-outcome>
          <to-view-id>/addParticipant.xhtml</to-view-id>
       </navigation-case>
    </navigation-rule>


    /*I use viewparam but if I don't use it it's working too, I clearly don't understand what viewparam is used for  */

    <f:metadata>
          <f:viewParam name="study_name" value="#{home.study_name}" required="true"/>
     </f:metadata>

   /* What is the best way to retrieve information? Then how i load param in a backing bean who this destination page ? */ 
   <h:outputFormat value="Hello,{0},{1},">
                <f:param value="#{home.study_name}" />
                <f:param value="#{param.studynameItem}" />
    </h:outputFormat>

在托管bean中:

    <navigation-rule>
       <from-view-id>/home.xhtml</from-view-id> 
       <navigation-case>
          <from-outcome>participant</from-outcome>
          <to-view-id>/addParticipant.xhtml</to-view-id>
       </navigation-case>
    </navigation-rule>


    /*I use viewparam but if I don't use it it's working too, I clearly don't understand what viewparam is used for  */

    <f:metadata>
          <f:viewParam name="study_name" value="#{home.study_name}" required="true"/>
     </f:metadata>

   /* What is the best way to retrieve information? Then how i load param in a backing bean who this destination page ? */ 
   <h:outputFormat value="Hello,{0},{1},">
                <f:param value="#{home.study_name}" />
                <f:param value="#{param.studynameItem}" />
    </h:outputFormat>
公共字符串actionGoToadStudent(){
FacesContext.getCurrentInstance().getExternalContext().getFlash().put(“参数1”,研究名称);
返回“pretty:participant”;//或只返回“/addParticipant.xhtml”,Prettyfaces将转换为pretty url
}
之后,您可以从目标托管bean中检索
param1
。这是用于使用隐藏参数的

    <navigation-rule>
       <from-view-id>/home.xhtml</from-view-id> 
       <navigation-case>
          <from-outcome>participant</from-outcome>
          <to-view-id>/addParticipant.xhtml</to-view-id>
       </navigation-case>
    </navigation-rule>


    /*I use viewparam but if I don't use it it's working too, I clearly don't understand what viewparam is used for  */

    <f:metadata>
          <f:viewParam name="study_name" value="#{home.study_name}" required="true"/>
     </f:metadata>

   /* What is the best way to retrieve information? Then how i load param in a backing bean who this destination page ? */ 
   <h:outputFormat value="Hello,{0},{1},">
                <f:param value="#{home.study_name}" />
                <f:param value="#{param.studynameItem}" />
    </h:outputFormat>
无论何时您想要使用标准的visible GET参数,您都会发现这些参数非常有用。使用GET参数需要将它们与GET请求一起发送,因此您的目标页面必须使用
捕捉它们,并将它们保存在目标托管bean属性中。在这里,您可以参考我的:

    <navigation-rule>
       <from-view-id>/home.xhtml</from-view-id> 
       <navigation-case>
          <from-outcome>participant</from-outcome>
          <to-view-id>/addParticipant.xhtml</to-view-id>
       </navigation-case>
    </navigation-rule>


    /*I use viewparam but if I don't use it it's working too, I clearly don't understand what viewparam is used for  */

    <f:metadata>
          <f:viewParam name="study_name" value="#{home.study_name}" required="true"/>
     </f:metadata>

   /* What is the best way to retrieve information? Then how i load param in a backing bean who this destination page ? */ 
   <h:outputFormat value="Hello,{0},{1},">
                <f:param value="#{home.study_name}" />
                <f:param value="#{param.studynameItem}" />
    </h:outputFormat>
源页面:

    <navigation-rule>
       <from-view-id>/home.xhtml</from-view-id> 
       <navigation-case>
          <from-outcome>participant</from-outcome>
          <to-view-id>/addParticipant.xhtml</to-view-id>
       </navigation-case>
    </navigation-rule>


    /*I use viewparam but if I don't use it it's working too, I clearly don't understand what viewparam is used for  */

    <f:metadata>
          <f:viewParam name="study_name" value="#{home.study_name}" required="true"/>
     </f:metadata>

   /* What is the best way to retrieve information? Then how i load param in a backing bean who this destination page ? */ 
   <h:outputFormat value="Hello,{0},{1},">
                <f:param value="#{home.study_name}" />
                <f:param value="#{param.studynameItem}" />
    </h:outputFormat>

目的地页面:

    <navigation-rule>
       <from-view-id>/home.xhtml</from-view-id> 
       <navigation-case>
          <from-outcome>participant</from-outcome>
          <to-view-id>/addParticipant.xhtml</to-view-id>
       </navigation-case>
    </navigation-rule>


    /*I use viewparam but if I don't use it it's working too, I clearly don't understand what viewparam is used for  */

    <f:metadata>
          <f:viewParam name="study_name" value="#{home.study_name}" required="true"/>
     </f:metadata>

   /* What is the best way to retrieve information? Then how i load param in a backing bean who this destination page ? */ 
   <h:outputFormat value="Hello,{0},{1},">
                <f:param value="#{home.study_name}" />
                <f:param value="#{param.studynameItem}" />
    </h:outputFormat>

正如您之前所说的,另一个解决方案可能是简单地将forward与JSF导航结合使用。参数被隐藏在第一个导航url页面上(例如,我们可以说/Home)。但是好吧,您将不会有/Addparticpant/New/TEST样式

    <navigation-rule>
       <from-view-id>/home.xhtml</from-view-id> 
       <navigation-case>
          <from-outcome>participant</from-outcome>
          <to-view-id>/addParticipant.xhtml</to-view-id>
       </navigation-case>
    </navigation-rule>


    /*I use viewparam but if I don't use it it's working too, I clearly don't understand what viewparam is used for  */

    <f:metadata>
          <f:viewParam name="study_name" value="#{home.study_name}" required="true"/>
     </f:metadata>

   /* What is the best way to retrieve information? Then how i load param in a backing bean who this destination page ? */ 
   <h:outputFormat value="Hello,{0},{1},">
                <f:param value="#{home.study_name}" />
                <f:param value="#{param.studynameItem}" />
    </h:outputFormat>
PrettyFaces提供的POST-REDIRECT-GET的优点是什么?而且f:param在使用PrettyFaces时实际上是毫无意义的,因为POST…除非您使用GET(这意味着您不使用PrettyFaces)&您将参数放在URL中。我将阅读您的链接,因为事实上,根据作用域、Post/get操作和位置(在JSF页面/backingbean中)检索参数时遇到了一些问题

    <navigation-rule>
       <from-view-id>/home.xhtml</from-view-id> 
       <navigation-case>
          <from-outcome>participant</from-outcome>
          <to-view-id>/addParticipant.xhtml</to-view-id>
       </navigation-case>
    </navigation-rule>


    /*I use viewparam but if I don't use it it's working too, I clearly don't understand what viewparam is used for  */

    <f:metadata>
          <f:viewParam name="study_name" value="#{home.study_name}" required="true"/>
     </f:metadata>

   /* What is the best way to retrieve information? Then how i load param in a backing bean who this destination page ? */ 
   <h:outputFormat value="Hello,{0},{1},">
                <f:param value="#{home.study_name}" />
                <f:param value="#{param.studynameItem}" />
    </h:outputFormat>
替代解决方案:  

    <navigation-rule>
       <from-view-id>/home.xhtml</from-view-id> 
       <navigation-case>
          <from-outcome>participant</from-outcome>
          <to-view-id>/addParticipant.xhtml</to-view-id>
       </navigation-case>
    </navigation-rule>


    /*I use viewparam but if I don't use it it's working too, I clearly don't understand what viewparam is used for  */

    <f:metadata>
          <f:viewParam name="study_name" value="#{home.study_name}" required="true"/>
     </f:metadata>

   /* What is the best way to retrieve information? Then how i load param in a backing bean who this destination page ? */ 
   <h:outputFormat value="Hello,{0},{1},">
                <f:param value="#{home.study_name}" />
                <f:param value="#{param.studynameItem}" />
    </h:outputFormat>

/home.xhtml
参与者
/addParticipant.xhtml
/*我使用viewparam,但如果我不使用它,它也会工作,我显然不理解viewparam的用途*/
/*检索信息的最佳方式是什么?那么我如何在一个支持bean中加载param,这个目标页面是谁?*/

您基本上有两种选择。一种是不执行
重定向
,因此浏览器中的url不会更改。第二种方法是使用flash scope或类似工具,以某种方式将参数值从一个视图保留到另一个视图?不事实上,我必须让漂亮的面孔失望,并使用类似thaht的action=“addParticipant”,我不知道如何检索参数,是否必须在目标页面中使用#{home.study_name}或#{thenameofstudy}。('home'是发件人页面中的bean)我提出了一个没有漂亮面孔的另一个解决方案,并向其他人提出了与主题相关的上述问题。谢谢.getFlash()抛出一个错误-java.lang.NoSuchMethodError:javax.servlet.http.Cookie.IshtPonly()。。。我从一个登录页面(Spring Security)来到my home.xhtml(会话范围,也许我会改为查看范围,我实际上正在阅读有关范围和如何使用范围的资料),然后我想去addParticipant.xhtml在我的数据库中添加一些东西(请求范围)。这是全局的想法。这取决于您的servlet api版本,您使用的是哪台服务器?此版本可以吗?Mojara是2.2.0。我还没有解决使用flash scope时的错误。在目标页面中使用
/Participant/New/#{Participant.study_name}
(注意,我在这里使用的是目标bean),并且仅将
/Participant/New/
f:viewParam
结合使用是多余的。这意味着,他们做同样的事情。看,与PrettyFaces的团队领导讨论。但是,我强烈建议您使用第二种方法,即不要在url映射中直接包含EL表达式。无论如何,很高兴看到它帮助解决了这个问题!你实际上是在混合param的概念。假设您有一个托管bean绑定到您的主页,另一个绑定到您的添加学生页面,
/addParticipant.xhtml
不必访问
主页
bean,决不。这就是视图参数的用途。还请记住,实际上,PrettyFaces用于执行GET请求,即获取内容页。我会更新我的答案给你一些提示。另外,POST-REDIRECT-GET也有它的优点,因为它是一种干净、清晰的模式,并且