【问题标题】:problem binding selectOneMenu to fire and action with and ok button问题将 selectOneMenu 绑定到触发并使用 ok 按钮执行操作
【发布时间】:2021-11-23 09:56:25
【问题描述】:

我想知道从组合框中选择的操作来调用方法A或方法B,具体取决于组合框中选择的值。

这是我的 web.xhtml:

<h:selectOneMenu value="#{webUserBackingBean.selectedAction}">
                                                            <f:selectItem itemValue="a"   itemLabel="aValue" />
                                                            <f:selectItem 
                                                                            itemValue="b" itemLabel="bValue" />
                                                        </h:selectOneMenu>

还有:

<p:commandButton
                                                            id= "submitActionButton"
                                                            action="#{webUserBackingBean.executeMethod()}"
                                                            
                                                            class="btn btn-secondary btn-md btn-block"
                                                            style="width: 30px; margin-top: auto !important; margin-left: 10px;
                                                                                         border-color: #006cd8 !important"
                                                            value="Ok" 
...

当用户点击确定时 webUserBackingBean.executeMethod() 被触发 在这种方法中我有一个问题:

public void executeMethod() {
... 字符串动作 = this.getSelectedAction(); ...

}

因为动作总是“a”。为什么即使我从组合框中选择“b”值也不起作用?

【问题讨论】:

    标签: java jsf jsf-2.2


    【解决方案1】:

    工作正常 试试这个

    `@Named
        @ViewScoped
        public class SelectMB implements Serializable {
            private String selectedAction;
            public String getSelectedAction() {
                return selectedAction;
            }
        
            public void setSelectedAction(String selectedAction) {
                this.selectedAction = selectedAction;
            }
        
            public String executeMethod() {
                System.out.println(selectedAction);
                return null;
            }
        }`
    
    ` <h:form>
            <h:selectOneMenu value="#{selectMB.selectedAction}">
                <f:selectItem itemValue="a"   itemLabel="aValue" />
                <f:selectItem
                        itemValue="b" itemLabel="bValue" />
            </h:selectOneMenu>
            <p:commandButton
                    id= "submitActionButton"
                    action="#{selectMB.executeMethod()}"
    
                    class="btn btn-secondary btn-md btn-block"
                    style="width: 30px; margin-top: auto !important; margin-left: 10px;border-color: #006cd8 !important"
                    value="Ok"/>
        </h:form>`
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-11
      • 1970-01-01
      • 2020-02-14
      • 2021-04-02
      相关资源
      最近更新 更多