jsf2:不使用selectOneMenu调用commandButton

时间:2013-03-28 15:02:29

标签: jsf-2 selectonemenu commandbutton

点击它时,以下代码中的commandButton不起作用。但在另一个xhtml文件中,commandButtoninputText一起使用。当我尝试使用selectOneMenu时,我会错过什么吗?

<h:form>
    <table cellspacing="0" cellpadding="0">
        <tr>
            <td width="180"><h:outputText value="&#160;&#160;ID1" /></td>
            <td width="400">
                <h:selectOneMenu value="#{AdminRelationBean.user1ID}">
                    <f:selectItems value="#{AdminRelationBean.user1IDs}"></f:selectItems>
                </h:selectOneMenu>
            </td>
        </tr>
        <tr class="table_content">
            <td><h:outputText value="&#160;&#160;ID2" /></td>
            <td>
                <h:selectOneMenu value="#{AdminRelationBean.user2ID}">
                    <f:selectItems value="#{AdminRelationBean.user2IDs}"></f:selectItems>
                </h:selectOneMenu>
            </td>
        </tr>
        <tr>
            <td></td>
            <td id="last_td">
                <h:commandButton class="button" value="Create Relation" action="#{AdminRelationBean.registerRelation}"></h:commandButton>
            </td>
        </tr>
    </table>
</h:form>

以下是课程AdminRelationBean的代码:

public class AdminRelationBean implements Serializable{ 
@Resource(name="jdbc/test")
private DataSource ds;

private String user1ID;
private String user2ID;
private List<SelectItem> user1IDs;
private List<SelectItem> user2IDs;

@PostConstruct
public void init(){
    try {
        this.user1IDs = getUser1IDs();
        this.user2IDs = getUser2IDs();
    } catch (SQLException e) {
        e.printStackTrace();
    }
}

public String registerRelation() throws SQLException{
    Connection conn = ds.getConnection();
    if(conn == null){
        return "error";
    }
    PreparedStatement pst = null;
    String stm = "insert into Relations(user1ID, user2ID) values(?, ?)";
    pst = conn.prepareStatement(stm);
    pst.setInt(1, Integer.parseInt(user1ID));
    pst.setInt(2, Integer.parseInt(user2ID));
    pst.executeUpdate();
    return "success";
}

public List<SelectItem> getUser1IDs() throws SQLException{
    List<SelectItem> items = new ArrayList<SelectItem>();
    Connection conn = ds.getConnection();
    if(conn == null){
        return null;
    }
    ResultSet rs = null;
    PreparedStatement pst = null;
    String stm = "Select * from User1";
    pst = conn.prepareStatement(stm);
    pst.execute();
    rs = pst.getResultSet();
    while(rs.next()){
        items.add(new SelectItem(rs.getInt("ID")));
    }
    rs.close();
    pst.close();
    conn.close();
    return items;
}

public List<SelectItem> getPatientNames() throws SQLException{
    List<SelectItem> items = new ArrayList<SelectItem>();
    Connection conn = ds.getConnection();
    if(conn == null){
        return null;
    }
    ResultSet rs = null;
    PreparedStatement pst = null;
    String stm = "Select * from User2";
    pst = conn.prepareStatement(stm);
    pst.execute();
    rs = pst.getResultSet();
    while(rs.next()){
        items.add(new SelectItem(rs.getInt("ID")));
    }
    rs.close();
    pst.close();
    conn.close();
    return items;
}

public String getUser1ID() {
    return user1ID;
}

public void setUser1ID(String user1ID) {
    this.user1ID = user1ID;
}

public String getuser2ID() {
    return user2ID;
}

public void setUser2ID(String user2ID) {
    this.user2ID = user2ID;
}

public void setUser1IDs(List<SelectItem> user1IDs) {
    this.user1IDs = user1IDs;
}

public void setUser2IDs(List<SelectItem> user2IDs) {
    this.user2IDs = user2IDs;
}

}

0 个答案:

没有答案