I'm a JSF Newbie and having problem with displaying data using datatable. These is my scenario:
I want to display the contract(s) assigned to the Customer
JSF page's Datatable : customerdetail.jsp
<h:dataTable id="dt_contract_list" value="#{customerBean.customer.contracts}" var="item">
<h:column>
<f:facet name="header">
<h:outputText value="Contract Identifier"/>
</f:facet>
<h:outputText style="" value="#{item.contractIdentifier}"></h:outputText>
</h:column>
</h:dataTable>
Entity Bean : Customer.java
#Entity
#NamedQueries({
#NamedQuery(name="getCustomerByName", query="SELECT customer FROM Customer customer WHERE customer.name = :name"),
#NamedQuery(name="getAllCustomer", query="SELECT customer FROM Customer customer")
})
public class Customer {
#Id
#GeneratedValue(strategy=GenerationType.IDENTITY)
private int id;
private String name;
#OneToMany(targetEntity = Contract.class,
mappedBy = "customer",
cascade = CascadeType.ALL,
fetch=FetchType.EAGER)
private List<Contract> contracts = new ArrayList<Contract>();
public Customer() {
super();
}
public int getId() {
return this.id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public List<Contract> getContracts() {
return this.contracts;
}
#Override
public boolean equals(Object object) {
if (this.getClass().isInstance(object)) {
return this.getName().equals(((Customer) object).getName());
} else {
return false;
}
}
}
BackingBean : CustomerBean.java
public class CustomerBean implements Serializable {
#EJB
private CustomerControllerLocal customerController;
private Customer customer;
public CustomerBean() {
customer = new Customer();
}
public String createCustomer() {
// create customer
}
public String updateCustomer() {
// update customer
}
public String deleteCustomer() {
// delete custoer
}
public String getCustomerByName() {
try {
customerController.getCustomerByName(customer.getName());
} catch (NotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "customer_got";
}
public String assignContractsToCustomer() {
// assign contract to customer
}
public Customer getCustomer() {
return customer;
}
public void setCustomer(Customer customer) {
this.customer = customer;
}
}
There's no errors and the whole business logic layer, data layer were tested and work fine but the result is null (and it shouldn't!) and no sign of any exception. Can anyone help me figure it out what's wrong with this? T___T
Your facelet code looks ok.
In your entity class there is one thing that looks suspicious. It is this line:
private List<Contract> contracts = new ArrayList<Contract>();
In the entity classe for my own projects (classes generated by netbeans) the relationship lists are never initialized. I am no expert in JPA but you could try it without the initialization part.
Related
I'm trying to pass the changed value of the variable to a bean method:
<h:panelGroup rendered="#{! empty gradesBean.getAllGrades()}">
<h:dataTable value="#{gradesBean.getAllGrades()}" var="g">
<h:column>
<f:facet name="header">#{msg['outputSubject']}</f:facet>
<h:inputText value="#{g.subject}" onchange="#{g.subject}" />
</h:column>
<h:column>
<f:facet name="header">#{msg['outputGrade']}</f:facet>
<h:inputText value="#{g.mark}" onchange="#{g.mark}"/>
</h:column>
<h:column>
<h:form>
<h:commandButton value="#{msg['actionSave']}" action="#{gradesBean.edit(g)}" />
</h:form>
</h:column>
</h:dataTable>
(.........)
</h:panelGroup>
I want the changes of the user he does in the inputText on g.subject and g.mark to be passed to gradesBean.edit(g). When I try to do that somehow the same values of the original values of the both variables are passed. There are getter and setter methods in the respective object
#Named
#ViewScoped
public class GradesBean extends AbstractBean implements Serializable {
private static final long serialVersionUID = 320401008216711886L;
private static final String NO_GRADES_PRESENT = "keine Noten eingetragen";
private static final Logger loggerLogger.getLogger(GradesBean.class);
#Inject
private transient GradeDAO gradeDAO;
#Inject
private UserDAO userDAO;
private Grade grade;
private List<Grade> allGrades;
#PostConstruct
public void init() {
if (!isLoggedIn()) {
return;
}
grade = new Grade();
allGrades = getSession().getUser().getGrades();
}
public Grade getGrade() {
return grade;
}
public List<Grade> getAllGrades() {
return allGrades;
}
public String getGradeAverage() {
final List<BigDecimal> theDecimals = new ArrayList<>(allGrades.size());
for (final Grade g : allGrades) {
theDecimals.add(g.getMark());
}
final Configuration config = Configuration.getDefault();
final int scale = config.getScale();
final RoundingMode roundingMode = config.getRoundingMode();
try {
final BigDecimal average = de.unibremen.st.gradelog.businesslogic.Math
.average(theDecimals, scale, roundingMode);
return average.stripTrailingZeros().toPlainString();
} catch (final ArithmeticException e) {
logger.debug(
"Calculation of grade average has been called without any grades.", e);
return NO_GRADES_PRESENT;
}
}
public String getGradeMedian() {
final List<BigDecimal> theDecimals = new ArrayList<>(allGrades.size());
for (final Grade g : allGrades) {
theDecimals.add(g.getMark());
}
try {
final BigDecimal median = de.unibremen.st.gradelog.businesslogic.Math
.median(theDecimals);
return median.stripTrailingZeros().toPlainString();
} catch (final ArithmeticException e) {
logger.debug(
"Calculation of grades median has been called without any grades.", e);
return NO_GRADES_PRESENT;
}
}
public String save() {
if (!isLoggedIn()) {
return null;
}
final User user = getSession().getUser();
grade.setUser(user);
user.addGrade(grade);
gradeDAO.save(grade);
try {
userDAO.update(user);
} catch (final DuplicateUniqueFieldException e) {
throw new UnexpectedUniqueViolationException(e);
}
init();
return null;
}
public String edit() {
if (!isLoggedIn()) {
return null;
}
assertNotNull(grade);
final User user = getSession().getUser();
gradeDAO.update(grade);
try {
userDAO.update(user);
} catch (final DuplicateUniqueFieldException e) {
throw new UnexpectedUniqueViolationException(e);
}
init();
return null;
}
public String remove(final Grade theGrade) {
if (!isLoggedIn()) {
return null;
}
assertNotNull(theGrade);
final User user = getSession().getUser();
user.removeGrade(theGrade);
gradeDAO.remove(theGrade);
try {
userDAO.update(user);
} catch (final DuplicateUniqueFieldException e) {
throw new UnexpectedUniqueViolationException(e);
}
init();
return null;
}
}`
g.mark and g.subject will already call the corresponding setters on your backing bean class (I assume that public setters getMark() and getSubject() exist).
Since action="#{gradesBean.edit(g)}" is resolved at the server (when the page is first displayed), it will have the original values , not the changed values. Anyway , to see the changed values you can use ajax (f:ajax tag and the corresponding listener attribute should be set). But you don't have to do this. Simply change your handler to
action="#{gradesBean.edit()}" //no argument
and get the latest values from your bean class instance.
I could be more specific if you want.
I am studying a PrimeFaces AutoComplete demo. I shortenied it from the full showcase demo. http://www.primefaces.org/showcase/ui/input/autoComplete.xhtml
AutoCompleteBean.java
#ManagedBean
public class AutoCompleteBean {
private Query query;
private List<Query> queries = new ArrayList<Query>();
#PostConstruct
public void init() {
queries.add(new Query(0, "Afterdark", "afterdark"));
queries.add(new Query(1, "Afternoon", "afternoon"));
queries.add(new Query(2, "Afterwork", "afterwork"));
queries.add(new Query(3, "Aristo", "aristo"));
}
public List<Query> completeQuery(String query) {
List<Query> filteredQueries = new ArrayList<Query>();
for (int i = 0; i < queries.size(); i++) {
Query skin = queries.get(i);
if(skin.getName().toLowerCase().contains(query)) {
filteredQueries.add(skin);
}
}
return filteredQueries;
}
public void onItemSelect(SelectEvent event) {
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Item Selected", event.getObject().toString()));
}
public Query getQuery() {
return query;
}
public void setQuery(Query query) {
this.query = query;
}
}
Query.java
public class Query {
private int id;
private String displayName;
private String name;
public Query() {}
public Query(int id, String displayName, String name) {
this.id = id;
this.displayName = displayName;
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getDisplayName() {
return displayName;
}
public void setDisplayName(String displayName) {
this.displayName = displayName;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
#Override
public String toString() {
return name;
}
}
I omitted a Convert class, which I think is not that relevant.
search.xhtml
<h:form>
<p:growl id="msgs" showDetail="true" />
<h:panelGrid columns="2" cellpadding="5">
<p:autoComplete id="queryPojo" value="#{autoCompleteView.query}"
completeMethod="#{autoCompleteView.completeQuery}" var="query"
itemLabel="#{query.displayName}" itemValue="#{query}"
converter="queryConverter" forceSelection="true" />
<p:commandButton value="search" oncomplete="PF('dlg').show()"/>
</h:panelGrid>
</h:form>
I have three questions for this:
1) completeMethod="#{autoCompleteView.completeQuery}": completeQuery method is called without passing a parameter, but it's defined as completeQuery(String query). How does this work?
2) value="#{autoCompleteView.query}". Query is an object defined in AutoCompleteBean. How can this Query object take user input string as its value? Usually InputText's value is good for taking user's input, which is a String value.
3) Can I still add an attribute "action=..." to the p:autoComplete componenet?
The converter class that you omitted here plays the real game.... Lets see your questions
As you see converter class overrides 2 methods
getAsString and getAsObject
1)the value
completeMethod="#{autoCompleteView.completeQuery}
gets refactred to
autoCompleteView.completeQuery(autoCompleteView.query);
as you can find to string method in Query class.
2).as converter is defined for autocomplete it calls getAsString method to render on screen. when selected getAsObject method is called to convert string value to object(Query).
3)you can use ajax select event
<p:ajax event="select" listener="#{autoCompleteView.someMethod}">
or call a remoteCommand by onSelect attribute in p:autoComplete
<p:autoComplete id="queryPojo" value="#{autoCompleteView.query}" onSelect="someRemoteCommand();"
completeMethod="#{autoCompleteView.completeQuery}" var="query"
itemLabel="#{query.displayName}" itemValue="#{query}"
converter="queryConverter" forceSelection="true" />
<p:remoteCommand name="someRemoteCommand" update="queryPojo" actionListener="#{autoCompleteView.execute}" />
In my xhtml there are 3 input field which calculate remaining day of two <p:calendar> dates. In next step I want to store calculated remaining day to MY DB.
<p:dataTable styleClass="vtable" editable="true" var="user"
editMode="cell" value="#{userBean.employeeList}">
<p:column styleClass="columntd" headerText="#{text['user.startedDate']}">
<p:calendar widgetVar="fromCal" value="#{vacationBean.vacation.beginDate}">
<p:ajax event="dateSelect" listener="#{dayDiffBean.fromSelected}"
update="diff" />
</p:calendar>
</p:column>
<p:column styleClass="columntd"
headerText="#{text['user.finishedDate']}">
<p:calendar widgetVar="toCal" value="#{vacationBean.vacation.endDate}">
<p:ajax event="dateSelect" listener="#{dayDiffBean.toSelected}"
update="diff" />
</p:calendar>
</p:column>
<p:column styleClass="columntd"
headerText="#{text['employee.remainingdays']}">
<p:inputText id="diff" styleClass="daysNumber"
value="#{dayDiffBean.diff}" />
</p:column>
</p:dataTable>
<h:commandButton styleClass="sndbutton1"
value="#{text['employee.send']}" action="#{vacationBean.addVac}"/>
I used value="#{dayDiffBean.diff} to get remaining day and now I also want to use my vacationbean to store remaingday to my db using like this : value="#{vacationBean.vacation.balanceDay}"
But I cant use 2 value in inputtext field like this:
<p:inputText value="dayDiffBean.diff" value1="vacationBean.vacation.balanceDay">
How can i solve this problem?
This is my vacation bean code:
#ManagedBean(name="vacationBean")
#ViewScoped
public class VacationBean implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
private Date vEndDate;
private boolean selected;
private Date vStartDate;
private Date createdDate;
private String isNobody;
Requestrelax vacation;
Employee e;
Calendar javaCalendar = null;
private short balanceDay;
#EJB
VacationLocal vacations;
#ManagedProperty(value="#{loginBean.userId}")
Integer userId;
#EJB
EmployeesLocal employees;
#PostConstruct
public void init(){
System.out.println("0");
//System.out.println("STATrtsg >> . "+ diff.getDiff());
vacation=new Requestrelax();
e=employees.getEmployee(userId);
vacation.setEmployee(e);
System.out.println("balanday is:"+balanceDay);
}
public void addVac(){
System.out.println("1");
javaCalendar = Calendar.getInstance();
Date currenDate=Calendar.getInstance().getTime();
vacation.setCreatedDate(currenDate);
vacation.setBalanceDay(balanceDay);
vacations.addEmployeeVacation(vacation);
}
public Integer getUserId() {
return userId;
}
public void setUserId(Integer userId) {
this.userId = userId;
}
public Employee getE() {
return e;
}
public void setE(Employee e) {
this.e = e;
}
public Requestrelax getVacation() {
return vacation;
}
public void setVacation(Requestrelax vacation) {
this.vacation = vacation;
}
public Date getvEndDate() {
return vEndDate;
}
public void setvEndDate(Date vEndDate) {
this.vEndDate = vEndDate;
}
public Date getvStartDate() {
return vStartDate;
}
public void setvStartDate(Date vStartDate) {
this.vStartDate = vStartDate;
}
public short getBalanceDay() {
return balanceDay;
}
public void setBalanceDay(short balanceDay) {
this.balanceDay = balanceDay;
}
public Date getCreatedDate() {
return createdDate;
}
public void setCreatedDate(Date createdDate) {
this.createdDate = createdDate;
}
public String getIsNobody() {
return isNobody;
}
public void setIsNobody(String isNobody) {
this.isNobody = isNobody;
}
}
And daydiffbean code :
#ManagedBean(name="dayDiffBean")
#SessionScoped
public class DayDiffBean implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private Date from;
private Date to;
private String diff="";
private final long oneDay=1000*60*60*24;
public void fromSelected(SelectEvent event){
from=(Date) event.getObject();
calDiff();
}
public void toSelected(SelectEvent event){
to=(Date) event.getObject();
calDiff();
}
public void calDiff(){
if(from==null||to==null){
diff="N/A";
return;
}
diff=(to.getTime()-from.getTime())/oneDay+"";
}
public String getDiff() {
return diff;
}
public void setDiff(String diff) {
this.diff = diff;
}
public void setFrom(Date from) {
this.from = from;
}
public Date getFrom() {
return from;
}
public Date getTo() {
return to;
}
public void setTo(Date to) {
this.to = to;
}
}
From the code, one way to add balanceDay to your vacationBean is by passing the diff string as a parameter to addVac() method (notice action in the second line):
<h:commandButton styleClass="sndbutton1" value="#{text['employee.send']}"
action="#{vacationBean.addVac(dayDiffBean.diff)}"/>
Then, for your VacationBean.addVac():
// 'diff' is now being passed in as a parameter
public void addVac(String diff) {
System.out.println("1");
javaCalendar = Calendar.getInstance();
Date currenDate=Calendar.getInstance().getTime();
vacation.setCreatedDate(currenDate);
vacation.setBalanceDay(balanceDay);
// UPDATED
// so now you can set balanceDay
setBalanceDay(Short.parseShort(diff));
vacations.addEmployeeVacation(vacation);
}
I use netbeans 7.2.1. Is JSF CRUD Generated code efficient for real applications?
I have created a test database and used netbeans CRUD generator. It uses DataModel and PaginationHelper instead of Lists for CRUD operations. There is an Entity Test.java, a TestFacade.java and TestController.java. and jsf files list.xhtml, edit.xhtml, view.xhtml, create.xhtml. I added a NemdQuery to entity file:
#Entity
#NamedQuery(name = "Test.findByTestCriteria", query = "SELECT t FROM Test t WHERE t.testCriteria = true")
public class Test implements Serializable {
private static final long serialVersionUID = 1L;
#Id
#GeneratedValue
#Column(name = "id")
private Integer id;
#Column(name = "title")
private String title;
#Column(name = "testCrieteria")
private boolean testCrieteria;
public Test() {
}
//Getters and Setters
And created the query in TestFacade.java:
#Stateless
public class TestFacade extends AbstractFacade<Test> {
#PersistenceContext(unitName = "testPU")
private EntityManager em;
#Override
protected EntityManager getEntityManager() {
return em;
}
public TestFacade() {
super(Test.class);
}
public List<Test> testCriteria(){
Query q = em.createNamedQuery("Test.findByTestCriteria",Test.class);
return q.getResultList();
}
}
And I have added a method in TestController.java to retrieve testCriteria Query:
#ManagedBean(name = "testController")
#SessionScoped
public class TestController implements Serializable {
private Test current;
private DataModel items = null;
private DataModel testCriteria = null;
#EJB
private com.test.TestFacade ejbFacade;
private PaginationHelper pagination;
private int selectedItemIndex;
public Test getSelected() {
if (current == null) {
current = new Test();
selectedItemIndex = -1;
}
return current;
}
public PaginationHelper getPagination() {
if (pagination == null) {
pagination = new PaginationHelper(10) {
#Override
public int getItemsCount() {
return getFacade().count();
}
#Override
public DataModel createPageDataModel() {
return new ListDataModel(getFacade().findRange(new int[]{getPageFirstItem(), getPageFirstItem() + getPageSize()}));
}
};
}
return pagination;
}
public DataModel getItems() {
if (items == null) {
items = getPagination().createPageDataModel();
}
return items;
}
public String prepareList() {
recreateModel();
return "List";
}
public String prepareView() {
current = (Test) getItems().getRowData();
selectedItemIndex = pagination.getPageFirstItem() + getItems().getRowIndex();
return "View";
}
//getting testCriteria items
public DataModel getTestCriteria(){
if(items == null){
items = getPagination().createPageDataModel();
}
testCriteria = new ListDataModel(ejbFacade.testCriteria());
return testCriteria;
}
//custom view page
public String viewTest(){
current = (Test) getItems().getRowData();
selectedItemIndex = pagination.getPageFirstItem() + getItems().getRowIndex();
return "ViewTest?faces-redirect=true";
}
//custom viewTestCriteria
public String viewTestCriteria(){
current = (Test) getTestCriteria ().getRowData();
selectedItemIndex = pagination.getPageFirstItem() + getItems().getRowIndex();
return "ViewTest?faces-redirect=true";
}
And Retrieving testCriteria in a p:dataGrid in index.xhtml:
<h:form>
<p:dataGrid value="#{testController.testCriteria()}" var="item" columns="4">
<p:column>
<h:panelGrid columns="1">
<h:commandLink id="viewTestCriteria" value="#{item.title}" action="#{testController.viewTestCriteria()}"/>
</h:panelGrid>
</p:column>
</p:dataGrid>
</h:form>
With this code all testCriteria data are there in index.xhtml but when I click the commadnButton to view them they all show the first Item. It seems that in the DataGrid it doesn't get the selected item. And if I refresh the List.xhtml which contains all test data, and then coming back to index.xhtml and pressing commandLink it throws a NoRowAvailable exception.
I hope I have stated my question clearly and I would appreciate any guide because I am new to this technology.
Update:
After googling and research for two days and thinking I thought of using <f:setPropertyActionListener value="#{item.id}" target="#{testController.selected.id}"> and <f:param name="#{testController.selected.id}" value="#{item.id}"> but it didn't work.
Update:
So far I'm almost confident that no row is getting selected so the data grid returns first row. But I'm still not sure how to modify viewTestCriteria() to set the current item and selectedItemIndex correctly.
hey I am using the following code to create the number of text fields as the user wants
<h:form>
<p>Number Of News <h:inputText value="#{news.noOfFields}" /></p>
<ui:repeat value="#{news.values}" var="item">
<hr/>
News #{item.no}
<h:inputText value="#{item.news}" /><br/>
</ui:repeat>
<hr/>
<h:commandButton styleClass="btn btn-blue" action="#{news.submit}" value="Save" />
</h:form>
The managed bean news has a class News as
#ManagedBean
#SessionScoped
public class News
{
private String noOfFields;
private List<NewsVO> values;
public News()
{
this.values = new ArrayList<NewsVO>();
}
public String submit() {
for(NewsVO newsVO : this.values)
{
System.out.println(newsVO.getNews());
System.out.println(newsVO.getNo());
}
return null;
// save values in database
}
public String getNoOfFields() {
return noOfFields;
}
public List<NewsVO> getValues() {
return values;
}
public void setValues(List<NewsVO> values) {
this.values = values;
}
public void setNoOfFields(String noOfFields) {
this.values = new ArrayList<NewsVO>();
try {
for(int i=0;i<Integer.valueOf(noOfFields);i++)
{
NewsVO newsVO = new NewsVO();
newsVO.setNo(i+1);
this.values.add(newsVO);
}
this.noOfFields = noOfFields;
}
catch(NumberFormatException ex) {
/*values = new String[1];*/
noOfFields = "1";
}
}
}
The NewsVO is just a javaBean class as follows
public class NewsVO
{
public int no;
public String news;
public int getNo() {
return no;
}
public void setNo(int no) {
this.no = no;
}
public String getNews() {
return news;
}
public void setNews(String news) {
this.news = news;
}
}
The problem is the values inside the input Text doesn't get reflected on pressing the save button. It gives me null, even though, I have written something inside all the textfields.
<h:inputText value="#{item.news}" />
Everytime you push the submit button, all setters in the bean are called (including setNoOfFields()). In this setter you are resetting your list, that's why you loose your values. Since you only need to modify your list if there is a size change, here is a simple way doing it :
#ManagedBean
#SessionScoped
public class News
{
private int noOfFields;
private List<NewsVO> values;
public News()
{
this.values = new ArrayList<NewsVO>();
}
public String submit()
{
for(NewsVO newsVO : this.values)
{
System.out.println(newsVO.getNews());
System.out.println(newsVO.getNo());
}
return null;
// save values in database
}
public int getNoOfFields()
{
return noOfFields;
}
public List<NewsVO> getValues()
{
return values;
}
public void setValues(List<NewsVO> values)
{
this.values = values;
}
public void setNoOfFields(int noOfFields)
{
if(noOfFields < this.noOfFields)
{
for(int i = this.noOfFields - 1;i >= noOfFields;i--)
{
getValues().remove(i);
}
}
else if(noOfFields > this.noOfFields)
{
for(int i = this.noOfFields;i < noOfFields;i++)
{
NewsVO newsVO = new NewsVO();
newsVO.setNo(i+1);
getValues().add(newsVO);
}
}
}
}
Note : I've also changed your noOfFields getter/setter for simple int, JSF will do the conversion for you.