【问题标题】:How to grab a value from one managed bean and use it to set another managed bean's property on post back如何从一个托管 bean 中获取一个值并在回发时使用它来设置另一个托管 bean 的属性
【发布时间】:2012-05-23 00:40:05
【问题描述】:

基本上,我希望能够在页面加载时从一个托管 bean 中获取一个值,然后使用该原始值回传到另一个 bean(表单中的其他值)......

这是我目前所拥有的部分内容(这都是一种形式并且有效......)

 <h:selectOneMenu id="categoryMenu" required="true"
                                         value="#{expense.categoryID}" label="Category" onchange="

                                                var value = myJQuery(this).val().toLowerCase();
                                                alert('You chose ' + value)">
                            <f:selectItem itemValue="0" itemLabel=""/>
                            <f:selectItem itemValue="1" itemLabel="Food"/>
                            <f:selectItem itemValue="2" itemLabel="Gas"/>
                            <f:selectItem itemValue="3" itemLabel="Clothing"/>
                            <f:selectItem itemValue="4" itemLabel="Recreation"/>
                            <f:selectItem itemValue="5" itemLabel="Other"/>

                        </h:selectOneMenu>
                        <h:message for="categoryMenu"/>
                        <h:outputLabel for="amount" value="Amount" styleClass="requiredLbl"/>
                        <h:inputText id="amount" value="#{expense.amount}" required="true" label="Amount"/>
                        <h:message for="amount"/>
                        <br/>
                        <p:commandButton id="btnSave" value="Save" action="#{expense.saveExpense}" ajax="false"/>
                    </h:panelGrid>

但我也想在回帖中包含这个值(不一定是隐藏形式,但你明白我的意思)......

 <input type="hidden" value="#{loginController.userID}" id="hiddenCategory"/>

有什么想法吗?

【问题讨论】:

    标签: java forms jsf postback hidden


    【解决方案1】:

    其中一种方法是将其作为请求参数传递。

    <p:commandButton id="btnSave" value="Save" action="#{expense.saveExpense}" ajax="false">
        <f:param name="userID" value="#{loginController.userID}" />
    </p:commandButton>
    

    如果#{expense}是request作用域,只需如下设置:

    @ManagedProperty("#{param.userID}")
    private Long userID; // +setter
    

    但是,您需要了解最终用户可以完全控制此值。如果这确实代表当前登录的用户,正如变量名称所暗示的那样,那么您不应该通过表单提交来传递它。

    假设#{loginController}是一个会话范围的bean,只需将它注入#{expense} bean,如下所示:

    @ManagedProperty("#{loginController}")
    private LoginController loginController; // +setter
    

    @ManagedProperty("#{loginController.userID}")
    private Long userID; // +setter
    

    【讨论】:

      猜你喜欢
      • 2012-09-03
      • 1970-01-01
      • 2012-12-12
      • 1970-01-01
      • 2016-12-26
      • 2012-03-24
      • 2011-11-04
      • 1970-01-01
      • 2012-05-22
      相关资源
      最近更新 更多