I'm new to Java EE, XHTML, JSF etc. So please don't insult me, if I write something in a wrong way. I've already searched on this site and on Google but I didn't find a proper solution to my problem.
I have created a Bean ("EventData.java") that takes from a database (named nyreg
with tables event,calendarevents) some information and put them in an xhtml page ("eventData.xhtml") with a dataTable (it's essentially a page with a list of events).
Now I've integrated a rowSelection that lead me to a new page called "eventDetail.xhtml". My objective is that in eventDetail.xhtml there is a <p:panelGrid>
composed of different <h:outputLabel>
and <p:inputText>
in which within every <p:inputText>
there are the information proposed in the previous page "eventData.xhtml" (obviously the information about the clicked event). I need that the information are in an <p:inputText>
because in this page it has to be possible to modify the information and update the database: i think that I'll be able to do this last part, but for now i need to visualize the information in these <p:inputText>
(or something that has the same properties).
I've tried to obtain this following different references but now I'm blocked.
The Bean that catch the data from DB is EventData.java:
@Named(value = "eventdata")
@RequestScoped
public class EventData implements Serializable {
private static final long serialVersionUID = 1L;
private Event selectedEvent;
@EJB
private EventManager em;
private List<Event> eventList;
public List<Event> getEventList() {
return eventList;
}
public void setEventList(List<Event> eventList) {
this.eventList = eventList;
}
@PostConstruct
public void init() {
try {
eventList = em.getEvents();
} catch (SQLException ex) {
Logger.getLogger(EventData.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
The page in which there is the table is eventData.xhtml:
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:p="http://primefaces.org/ui">
<h:head>
<title>Calendar</title>
<h:outputStylesheet library="css" name="styles.css" />
</h:head>
<h:body>
<!--<h:form id="form"> -->
<!--<p:growl id="msgs" showDetail="true"/>-->
<p:dataTable id="eventDetail" var="e"
value="#{eventdata.eventList}">
<f:facet name="header">
Event List
</f:facet>
<p:column headerText="Name">
<h:outputText value="#{e.name}"/>
</p:column>
<p:column headerText="Place">
<h:outputText value="#{e.place}"/>
</p:column>
<p:column headerText="Date">
<h:outputText value="#{e.eventdate}"/>
</p:column>
<p:column headerText="Type" style="width:100px;text-align:center">
<h:outputText value="#{e.type}"/>
</p:column>
<p:column headerText="Visibility" style="width:100px;text-align:center">
<h:outputText value="#{e.visibility}"/>
</p:column>
<p:column headerText="Creator">
<h:outputText value="#{e.creator}"/>
</p:column>
<p:column headerText="Edit" style="width:50px;text-align:center">
<p:commandButton action="#{editEventBean.findSelectedEvent(e.id)}" icon="ui-icon-pencil"/>
</p:column>
</p:dataTable>
<!--</h:form>-->
</h:body>
</html>
The page in which there has to be the information about the selected event is editEvent.xhtml:
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:p="http://primefaces.org/ui"
xmlns:f="http://xmlns.jcp.org/jsf/core">
<h:head>
<title>Update Event</title>
</h:head>
<h:body>
The event name is #{editEventBean.selected.name}<br/><br/>
The event place is #{editEventBean.selected.place}<br/><br/>
The event date is #{editEventBean.selected.eventdate}<br/><br/>
The event type is #{editEventBean.selected.type}<br/><br/>
The event visibility is #{editEventBean.selected.visibility}<br/><br/>
<!-- I'm trying to visualize the "Name: name_of_event" and then an inputForm -->
<h:outputFormat value="Name: {0} "><br/>
<f:param value="#{editEventBean.selected.name}"/>
</h:outputFormat>
<h:inputText value="#{editEventBean.event.name}" id="name" />
</h:body>
</html>
Now i have a EditEventBean.java
@Named
@RequestScoped
public class EditEventBean {
private Event event;
private Event selected = new Event();
@EJB
private EventManager em;
private String sqlQuery;
public String save(){
em.saveEvent(event);
return "/eventData?faces-redirect=true";
}
public Event getEvent() {
return event;
}
public void setEvent(Event event) {
this.event = event;
}
public Event getSelected() {
return selected;
}
public void setSelected(Event selected) {
this.selected = selected;
}
public String findSelectedEvent(Integer id) throws SQLException {
this.selected.setId(id);
System.out.println("value of the id that i need " + this.selected.getId());
ResultSet rs = null;
PreparedStatement pst = null;
Connection con = em.getConnection();
sqlQuery = "SELECT name, place, eventdate, type, visibility "
+ "FROM event "
+ "WHERE id=?";
try {
pst = con.prepareStatement(sqlQuery);
pst.setInt(1, this.selected.getId());
pst.execute();
rs = pst.getResultSet();
while (rs.next()) {
selected.setName(rs.getString(1));
selected.setPlace(rs.getString(2));
selected.setEventdate(rs.getString(3));
selected.setType(rs.getString(4));
selected.setVisibility(rs.getString(5));
}
} catch (SQLException e) {
e.printStackTrace();
}
System.out.println("value of the name that i need " + this.selected.getName());
System.out.println("value of the place that i need " + this.selected.getPlace());
System.out.println("value of the date that i need " + this.selected.getEventdate());
System.out.println("value of the type that i need " + this.selected.getType());
System.out.println("value of the visibility that i need " + this.selected.getVisibility());
return "/user/event/editEvent?faces-redirect=true";
}
}
Finally this is my EventManager.java:
@Stateless
public class EventManager {
@PersistenceContext
public EntityManager em;
@Inject
Principal principal;
private static final long serialVersionUID = 1L;
private Event selectedEvent;
@EJB
private UserManager um;
private String sqlQuery;
private String LogUser = "prova";
public void saveEvent(Event event) {
em.persist(event);
}
public void deleteEvent(Event event) {
em.remove(event);
}
public List<Event> getEvents() throws SQLException {
ResultSet rs = null;
PreparedStatement pst = null;
Connection con = getConnection();
System.out.println("value of LogUser before the get: " + LogUser);
LogUser = um.getLoggedUser().getEmail();
System.out.println("value of LogUser after the get: " + LogUser);
sqlQuery = "SELECT event.name, event.place, event.eventdate, event.type, event.visibility, event.creator, event.id "
+ "FROM event, calendarevents "
+ "WHERE event.id=calendarevents.eventid AND calendarevents.useremail=?";
/*String sqlQuery = "Select name, place, eventdate, type, visibility, creator from event";*/
List<Event> records = new ArrayList<Event>();
try {
pst = con.prepareStatement(sqlQuery);
pst.setString(1, LogUser);
pst.execute();
rs = pst.getResultSet();
while (rs.next()) {
Event event = new Event();
event.setName(rs.getString(1));
event.setPlace(rs.getString(2));
event.setEventdate(rs.getString(3));
event.setType(rs.getString(4));
event.setVisibility(rs.getString(5));
event.setCreator(rs.getString(6));
event.setId(rs.getInt(7));
records.add(event);
}
} catch (SQLException e) {
e.printStackTrace();
}
return records;
}
public Connection getConnection() {
Connection con = null;
String url = "jdbc:mysql://localhost:3306/nyreg";
String user = "monty";
String password = "some_pass";
try {
con = DriverManager.getConnection(url, user, password);
System.out.println("Connection completed. ");
} catch (SQLException ex) {
System.out.println(ex.getMessage());
} finally {
}
return con;
}
}
As you can see at the end of the Bean I've put the getter and setter for SelectedEvent and an "eventSelect" that lead to the page eventDetail (but I don't know if this uses what I need), but then I don't know how to implement what I need.
I hope that you'll try to propose a solution similar to my needs, I'll be forever grateful :D
UPDATE 1: I've read the link posted by BalusC and tried to applied the clarification to my case, but I don't catch some things.
I think that in the eventData.xhtml, when I click on Edit i need to save e.id because it's the primary key for the class Event and i need it to obtain the data of the selected event in the next page?
In editEvent.xhtml I don't understand how the viewParam works and in my case when i click on Edit in eventData.xhtml I'm lead to editEvent.xhtml but it is completely white without errors.
Do I need the Converter? Is there a simpler way to: save the e.id of the selected event, go to the page editEvent.xhtml passing this parameter (is something similar done by the convert?) and visualize the information with inputForm and inputText and inputTextarea so that they are editable?
UPDATE 2: This morning I've done a step forward thank to you all :) Now in the eventData.xhtml I've put a commandButton that allow me to pass e.id to the EditEventBean.java that use findSelectedEvent(Integer id) to query the database (then I'll move this part to the EventManager ;) ) to obtain the data related the event with the passed ID. It actually works and in the EditEventBean.java you can see that there are some System.out.println that show me the correct value of name, place, ecc..
Now i want to visualize these data in editEvent.xhtml. First of all I've tried to simply display them in the page using something like "The event name is #{editEventBean.selected.name}", but the page shows only "The event name is" and then nothing more.
I've also tried to use outputFormat with the aim to display something like "Name: name_of_event" and then an inputForm. Then I need that name_of_event is already inside the inputForm. The problem here is that name_of_event is taken from #{editEventBean.selected.name} , that I thought it is the same value printed with the System.out, but here i get a Null. Why? :(