0

我正在尝试向我找到的项目添加一些功能。有查询选择指定的结果。现在我正在尝试获取第二个查询的结果。这是我的代码:

汽车.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;
    }   

我的问题是如何显示 carByName 查询的结果?上面的代码可以吗?第一次查询的结果由以下人员打印:

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

但它是如何完成的?我看不到名为 availableCars 的方法。

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

4

1 回答 1

2

在这种情况下,表达式#{carBean.availableCars}不是指方法,而是指 bean 的属性carBean。所以它评估为 的getAvailableCars()方法carBean。在表达式语言标签信息页面上搜索以了解有关此内容的更多信息。

请注意,在 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;
    }
于 2013-01-11T12:58:32.410 回答