JSF下拉框获取值的问题

zbh6260 2010-12-02
我做了一个动态的下拉框,初始的时候,下拉框的value为空,当我按了value按键的时候,可以通过后台bean赋值,下拉框会有value,但当我选择了value后,我希望当我按下另一个按键check时,页面会跳转到相应的页面,根据选择的不同的value。但目前的情况是,不管我选择什么value,页面还是停留在原页面。下面是我的代码:
bean:
public class myBean {
private String[] items = {""};
private String selectedItem;
private SelectItem[] selectOneItems;
public void setItems(String[] items) {
this.items = items;
}
public String[] getItems() {
return items;
}
public void setSelectedItem(String selectedItem) {
this.selectedItem = selectedItem;
}
public String getSelectedItem() {
return selectedItem;
}
public void setSelectOneItems(SelectItem[] selectOneItems) {
this.selectOneItems = selectOneItems;
}
public void value(ActionEvent e){
String[] options = {"lyz","wb"};
setItems(options);
}
public SelectItem[] getSelectOneItems() {
int n = items.length;
selectOneItems = new SelectItem[n];
for(int i=0;i<n;i++){
selectOneItems[i] = new SelectItem(items[i],items[i]);
}
return selectOneItems;
}
public String check(){
String result = "failure";
if(selectedItem == "lyz"){
result = "lyz";
}
if(selectedItem == "wb"){
result = "wb";
}
return result;
}
}

页面:
<f:view>
    <h:form>
        <h:selectOneMenu id="selectedItem" value="#{myBean.selectedItem}">
            <f:selectItems value="#{myBean.selectOneItems}"/>
        </h:selectOneMenu>
        <h:commandButton value="Value" actionListener="#{myBean.value}"></h:commandButton>
        <h:commandButton value="Check" action="#{myBean.check}"></h:commandButton>
    </h:form>
</f:view>

xml:
<managed-bean>
  <managed-bean-name>myBean</managed-bean-name>
  <managed-bean-class>bean.myBean</managed-bean-class>
  <managed-bean-scope>session</managed-bean-scope>
</managed-bean>
<navigation-rule>
  <from-view-id>/DropDown.jsp</from-view-id>
  <navigation-case>
   <from-outcome>lyz</from-outcome>
   <to-view-id>/lyz.jsp</to-view-id>
  </navigation-case>
  <navigation-case>
   <from-outcome>wb</from-outcome>
   <to-view-id>/wb.jsp</to-view-id>
  </navigation-case>
</navigation-rule>
<navigation-rule>
  <from-view-id>/lyz.jsp</from-view-id>
</navigation-rule>
<navigation-rule>
  <from-view-id>/wb.jsp</from-view-id>
</navigation-rule>
zbh6260 2010-12-02
。。。等待中
zbh6260 2010-12-06
继续等待中。。。
gardfield.vv 2010-12-09
public void value(ActionEvent e){
String[] options = {"lyz","wb"};
setItems(options);
}   有问题~  看看ActionEvent 的用法..
zbh6260 2010-12-09
我换了一种方式 可以完成我的功能了 呵呵
archwuke 2010-12-09
试了一下
check()里面的字符串比较改成:
        if (selectedItem.equals("lyz")) {
            result = "lyz";
        }

这样就可以了

我是用netbeans 7.0试的
zbh6260 2010-12-11
呵呵 这个问题我解决了 不过还是谢谢哈
Global site tag (gtag.js) - Google Analytics