从primefaces组件获取值

时间:2012-02-20 11:59:08

标签: java jsf primefaces

我有facelets页面和与之关联的托管bean。 我已经使用了primefaces组件,我的问题是我希望在单击命令按钮时获取每​​个组件的用户选择的值。 当我尝试编写JoptionPane或system.out.print时,它不起作用。我已将commandbutton action属性设置为在managedbean中找到的btnsearchFlight方法。那么任何一个我错过的问题是什么。或者非常感谢一个例子。

这是Facelets页面

<?xml version='1.0' encoding='UTF-8' ?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core" xmlns:p="http://primefaces.org/ui">

    <h:head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <link href="./resources/css/default.css" rel="stylesheet" type="text/css" />
        <link href="./resources/css/cssLayout.css" rel="stylesheet" type="text/css" />
        <title>Airline Travel Planner</title>
    </h:head>

    <h:body>

        <h:form id="form"> 
            <div id="top">
                <ui:insert name="top">AirLine Travel Planner</ui:insert>
            </div>
            <div>
                <div id="left">
                    <ui:insert name="left"></ui:insert>
                </div>
                <div>
                    <div id="right">
                        <ui:insert name="right"></ui:insert>
                    </div>
                    <div id="content" class="right_content" style="height: 500px">
                        <ui:insert name="content">
                            <p:selectOneRadio binding="#{calendarBean1.rdbTripType}" id="rdbTripType" value="#{calendarBean1.rdbTripType}">  
                                <f:selectItem itemLabel="One Way" itemValue="1" />  
                                <f:selectItem itemLabel="Round Trip" itemValue="2" />  
                            </p:selectOneRadio>  
                            <br/>
                            <h:outputLabel>From:</h:outputLabel>  
                            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                            <p:selectOneMenu value="#{calendarBean1.cityInfo}" style="" effect="fold" editable="true">  
                                <f:selectItems value="#{calendarBean1.cityInfo}" />  
                            </p:selectOneMenu> 
                            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                            <h:outputLabel style="position: relative">To:</h:outputLabel>
                            <p:selectOneMenu value="#{calendarBean1.cityInfo}" effect="fold" editable="true">  
                                <f:selectItems value="#{calendarBean1.cityInfo}" />  
                            </p:selectOneMenu> 
                            <br/><br/>
                            <h:outputLabel>Depart On:</h:outputLabel>  
                            <p:calendar value="#{calendarBean1.date3}" id="popupButtonDepartOn" showOn="button" /> 
                            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                            <h:outputLabel>Arrive On:</h:outputLabel>  
                            <p:calendar value="#{calendarBean1.date2}" id="popupButtonArriveOn" showOn="button" /> 
                            <br/>  <br/> 

                            <h:outputText value="Passenger Type" />  
                            <p:selectOneMenu id="selectOneMenuPassengerType" binding="#{calendarBean1.selectOneMenuPassengerType}" value="#{calendarBean1.selectOneMenuPassengerType}" >  
                                <f:selectItem itemLabel="Select One" itemValue="" />  
                                <f:selectItem itemLabel="Adult" itemValue="1" />  
                                <f:selectItem itemLabel="Child" itemValue="2" />  
                                <f:selectItem itemLabel="Infant" itemValue="3" />  
                            </p:selectOneMenu>  
                            <br/> <br/> 
                            <p:selectBooleanCheckbox value="#{calendarBean1.lowestFareChecked}" />
                            <h:outputText value="Lowest Fare" />  
                            <br/> 
                            <p:commandButton  id="btnSearchFlight" value="Search"  action="#{calendarBean1.searchFlight}" > 

                            </p:commandButton>

这是托管bean类

package test.sample;

import java.io.Serializable;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Date;
import javax.faces.bean.ManagedBean;

import javax.faces.model.SelectItem;
import javax.swing.JOptionPane;
import org.primefaces.component.commandbutton.CommandButton;
import org.primefaces.component.selectonemenu.SelectOneMenu;
import org.primefaces.component.selectoneradio.SelectOneRadio;
import storageMethods.FlightMethod;

/**
 *
 * @author Nati
 */
@ManagedBean(name = "calendarBean1")
public class CalendarBean1 implements Serializable{

    /**
     * Creates a new instance of CalendarBean1
     */
    public CalendarBean1() {
    }
    private Date date1;
    private Date date2;
    private Date date3;
    private boolean lowestFareChecked;

    public boolean isLowestFareChecked() {
        return lowestFareChecked;
    }

    public void setLowestFareChecked(boolean lowestFareChecked) {
        this.lowestFareChecked = lowestFareChecked;
    }

    public Date getDate1() {
        return date1;
    }

    public void setDate1(Date date1) {
        this.date1 = date1;
    }

    public Date getDate2() {
        return date2;
    }

    public void setDate2(Date date2) {
        this.date2 = date2;
    }

    public Date getDate3() {
        return date3;
    }

    public void setDate3(Date date3) {
        this.date3 = date3;
    }
    private SelectOneRadio rdbTripType = new SelectOneRadio();

    public SelectOneRadio getRdbTripType() {
        return rdbTripType;
    }

    public void setRdbTripType(SelectOneRadio rdbTripType) {
        this.rdbTripType = rdbTripType;
    }
    public CommandButton btnSearchFlight = new CommandButton();

    public CommandButton getBtnSearchFlight() {
        return btnSearchFlight;
    }

    public void setBtnSearchFlight(CommandButton btnSearchFlight) {
        this.btnSearchFlight = btnSearchFlight;
    }
    private SelectOneMenu selectOneMenuPassengerType = new SelectOneMenu();

    public SelectOneMenu getSelectOneMenuPassengerType() {
        return selectOneMenuPassengerType;
    }

    public void setSelectOneMenuPassengerType(SelectOneMenu selectOneMenuPassengerType) {
        this.selectOneMenuPassengerType = selectOneMenuPassengerType;
    }
    public ArrayList<SelectItem> CityInfo;

    public ArrayList<SelectItem> getCityInfo() {
        CityInfo = CityInfo();
        return CityInfo;
    }

    public void setCityInfo(ArrayList<SelectItem> CityInfo) {
        this.CityInfo = CityInfo;
    }

    public String SearchFlight() {
//        JOptionPane.showMessageDialog(null, rdbTripType.getValue().toString());
//        JOptionPane.showMessageDialog(null,   selectOneMenuPassengerType.getValue().toString());
         JOptionPane.showMessageDialog(null,date3);
//        System.out.print("hi");
//        System.out.print(isLowestFareChecked());

        return null;
    }


}

2 个答案:

答案 0 :(得分:1)

在stackoverflow上发布时可能只是一个拼写错误,但您的方法是大写的:  SearchFlight 您在.xhtml按钮中的操作是小写的。  行动=“#{calendarBean1.searchFlight}

不应该是

public String searchFlight() {

我从未在JSf franework中使用过Swing组件。但是当注释掉时,system.out应该可以工作。如果你调用了方法。你的日志中有没有错误?

答案 1 :(得分:0)

我认为您将webapp开发(JSF)与桌面开发(Swing)混合在一起。使用JSF,您不需要为每个facelet组件创建一个支持bean组件。你不需要

  • org.primefaces.component.commandbutton.CommandButton
  • org.primefaces.component.selectonemenu.SelectOneMenu
  • org.primefaces.component.selectoneradio.SelectOneRadio
如果你只想绑定这些组件的输入值,那么在你的bean中

当然,在某些情况下,您将从组件绑定中获益,但在您的示例中没有必要。

在facelet中,您使用value属性和binding属性,但它们具有相同的内容。

如果您只对输入值感兴趣,则只需value属性即可。让这个属性指向一个只保留值的支持bean字段,例如intString