0

I'm trying to add some features to project which I find. There was query which selects specified results. Now I'm trying to get second query's results. Here's my code:

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;
    }   

My question is how to display the results of carByName query? Is the code above OK? Results of first query are printed by:

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

But how is it done? I can't see method called availableCars.

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

Donato Szilagyi
  • 4,279
  • 4
  • 36
  • 53

1 Answers1

2

In this case, the expression #{carBean.availableCars} does not refer to a method, but to a property of the bean carBean. So it evaluates to the method getAvailableCars() of the carBean. Take a loot at the Expression Language tag info page to learn more about this.

Note that it's not a good practice to call service methods on a getter (see this question). You'd be better with an initialization method annotated with @PostConstruct.

Also, you don't need a separate lists for the results. Just make an action method that fills the list of cars with the search results - say searchCars(), and make your search button to call it:

<h:commandButton value="Search" action="#{carBean.searchCars}" />

So, putting this together, this is what your bean code should look like:

@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;
    }
Community
  • 1
  • 1
Elias Dorneles
  • 22,556
  • 11
  • 85
  • 107
  • Thank You for fast response. I changed my code as you wrote. Now how to print results of the query? – user1877249 Jan 11 '13 at 13:21
  • Well, if you removed the `cars.setWrappedData(cm.getAvailableCars());` in the method `getAvailableCars()`, the results should be shown in your `` after calling the method `searchCars()`. – Elias Dorneles Jan 11 '13 at 13:26
  • I've updated the code in the answer with a more complete example, see if that helps. – Elias Dorneles Jan 11 '13 at 13:37
  • Now i can see the results of car.unsold query but still i can't see results of car.findByName query. Note that when I fill my search form both tables are empty (cant see both query results). – user1877249 Jan 11 '13 at 13:48
  • **SearchCar page:** ... – user1877249 Jan 11 '13 at 13:56
  • sorry, could you update the question with the full XHTML code? – Elias Dorneles Jan 11 '13 at 14:01
  • Note that how your `CarManager` is implemented, the query searchByName searches for a car with `make` exactly equal the value searched. – Elias Dorneles Jan 11 '13 at 14:26
  • man, I see nothing wrong in your XHTML. can you please post the full code of your backing bean too? – Elias Dorneles Jan 11 '13 at 17:01
  • I copied your solution. Now i'm trying to display list (cars list)-this should be easier for me. – user1877249 Jan 11 '13 at 17:45
  • what exactly is happening? How it ist, from I understand, when you open the page it should load and display the existing cars. Then, when you search for a car, it would filter the cars according to the `make` attribute. What is exactly the behavior you are getting? – Elias Dorneles Jan 11 '13 at 18:00
  • ops, I think I found it! the `h:inputText` on the search page should be using the attribute `value` instead of id! it should be `` – Elias Dorneles Jan 11 '13 at 18:02
  • I want to have two pages. One with all available cars and one with search form that's why I wanted to display list :) But after your answers I would do it by myself. Still. Searching is not working. After pressing button I don't see any reaction. – user1877249 Jan 11 '13 at 18:41
  • did you corrected the `h:inputText` in the search form, as per in my comment? – Elias Dorneles Jan 11 '13 at 19:28
  • yes I did. I think I'm doing it in bad way. First I should print a List, then I will be able to develop it in the proper way. @elias- Thank you for your time. – user1877249 Jan 12 '13 at 21:41
  • @user1877249 hmmm... yeah, you may try doing one thing at the time. If the list isn't showing up, first make it show up. Then, create a button with an action that just prints "button clicked" on the console. Then, make the button action to change the list contents... You can do all that in one xhtml only, to simplify. – Elias Dorneles Jan 13 '13 at 13:22