在JSF中显示两个查询结果



我试图添加一些功能的项目,我发现。存在选择指定结果的查询。现在我试图得到第二个查询的结果。下面是我的代码:

Car.java

@Entity
@NamedQueries({
@NamedQuery(name = "car.unsold", query = "Select c from Car c where c.sold = false"),
@NamedQuery(name="car.findByName", query=" SELECT C FROM Car c where c.make=?")
})
public class Car {  
private Long id;
private String make;
private String model;
//and all getters and setters   
 }

CarManager.java

@Stateless
public class CarManager {
@PersistenceContext
EntityManager em1;
@SuppressWarnings("unchecked")
public List<Car> getAvailableCars() {
    return em1.createNamedQuery("car.unsold").getResultList();
}
@SuppressWarnings("unchecked")
public List<Car> getCarsByName(String make){
    return em1.createNamedQuery("car.findByName").setParameter(1,   make).getResultList();
}}

CarBean.java

@SessionScoped
@Named("carBean")
public class CarFormBean implements Serializable {
private static final long serialVersionUID = 1L;
private Car car = new Car();
private ListDataModel<Car> cars = new ListDataModel<Car>();
private ListDataModel <Car> searchresult = new ListDataModel<Car>();
private String searchCar;
public String getSearchCar() {
    return searchCar;
}
public void setSearchCar(String searchCar) {
    this.searchCar = searchCar;
}
@Inject
private CarManager cm;  
public Car getCar() {
    return car;
}
public void setCar(Car car) {
    this.car = car;
}   
public ListDataModel<Car> getAvailableCars() {
    cars.setWrappedData(cm.getAvailableCars());
    return cars;
}
public ListDataModel<Car> getCarsByName(){
    searchresult.setWrappedData(cm.getCarsByName(searchCar));
    return searchresult;
}   
public String searchCar(){
    cm.getCarsByName(searchCar);
    return null;
    }   

我的问题是如何显示caryname查询的结果?上面的代码可以吗?第一个查询的结果打印为:

<rich:dataTable value="#{carBean.availableCars}" var="car" 

但是怎么做呢?我看不到availableCars方法

Search.xhtml http://wklej.org/id/921560/all.xhtml http://wklej.org/id/921561/

在本例中,表达式#{carBean.availableCars}不是指方法,而是指bean carBean的属性。用carBean法求getAvailableCars()法。在表达式语言标签信息页面中获取战利品,以了解更多关于此的信息。

注意,在getter上调用服务方法不是一个好习惯(参见这个问题)。您最好使用带有@PostConstruct注释的初始化方法。

同样,您不需要单独的结果列表。只需创建一个操作方法,用搜索结果填充汽车列表-例如searchCars(),并将搜索按钮命名为:

<h:commandButton value="Search" action="#{carBean.searchCars}" />
所以,把这些放在一起,这就是你的bean代码应该是什么样子的:
@SessionScoped
@Named("carBean")
public class CarFormBean implements Serializable {
    private static final long serialVersionUID = 1L;        
    private Car car = new Car();
    // a list for storing the available cars and the search results
    private ListDataModel<Car> cars = new ListDataModel<Car>();        
    private String searchCar;
    @Inject
    private CarManager cm;
    // initialization method
    @PostConstruct
    public void init() {
        cars.setWrappedData(cm.getAvailableCars());
    }
    // action method
    public void searchCars() {
        cars.setWrappedData(cm.getCarsByName(searchCar));
    }
    // getters and setters
    public ListDataModel<Car> getAvailableCars() {
        return cars;
    }
    public Car getCar() {
        return car;
    }
    public void setCar(Car car) {
        this.car = car;
    }
    public String getSearchCar() {
        return searchCar;
    }
    public void setSearchCar(String searchCar) {
        this.searchCar = searchCar;
    }

相关内容

  • 没有找到相关文章

最新更新