package cars; import java.util.List; import javax.inject.Named; import javax.enterprise.context.RequestScoped; import javax.inject.Inject; @Named(value = "ownerQuery") @RequestScoped public class OwnerQuery { @Inject private Database database; private String vin; private Car car; private List<String> ownerList; private String errorMessage; public OwnerQuery() { vin = ""; } public String getVin() { return vin; } public void setVin(String vin) { this.vin = vin; } public Car getCar() { return car; } public List<String> getOwnerList() { return ownerList; } public String getErrorMessage() { return errorMessage; } public String ownerListAction() { try { car = database.getCar(vin); ownerList = database.getOwnerList(vin); return "owners"; } catch (Exception ex) { errorMessage = ex.getMessage(); return "error"; } } }
The OwnerQuery class defines a request scoped ownerQuery bean. Its vin property captures the choice made by the user from a list in the index.xhtml page. Its ownerListAction() method is an action method invoked by the "Find Owners" command button in the index.xhtml page.
If no exceptions occur when database bean methods are invoked in ownerListAction() it sets up read-only car and ownerList properties. These properties are used in the owners.xhtml response page and the action method directs navigation to that page.
If an exception occurs then ownerListAction() sets up the read-only errorMessage property. This property is used in the error.xhtml response page and the action method directs navigation to that page.