0

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? :(

gatb27
  • 31
  • 5
  • 2
    Is this acceptable as a duplicate? http://stackoverflow.com/questions/8459903/correct-usage-of-session-scope-in-two-page-master-detail-user-interface/ – BalusC Dec 23 '14 at 17:29
  • @BalusC I'm currently from mobile and i can't read this post completely: do you think that in there there is a description that will help me to solve my problem? :) I'm sorry if I've generated a duplicate but I didn't find this other post, probably because I've used the wrong key words every time I searched due to my inexperiences with these topics. – gatb27 Dec 23 '14 at 17:45
  • You injected an EJB in the provided managed bean though you are playing with plain JDBC things in that managed bean. Why? – Tiny Dec 23 '14 at 18:10
  • gatb27, the link BalusC provided answers your question, but I'm not surprised you didn't find it though, because in that post the person is asking if they can use SessionScoped beans to achieve what you're going for, but BalusC recommends against it and explains the conventional solution. – DavidS Dec 23 '14 at 18:54
  • @Tiny because without the EJB I wasn't able to obtain the email of the logged user that I use in the query to obtain only the events in his calendar. I don't know if it is the most efficient way to implement it, but I based that concept using some examples provided by my professor tutor ! – gatb27 Dec 23 '14 at 19:22
  • Ok thanks @Nimnio for your clarification ;) In an hour I'll be at home and I'll read the post from my computer trying to implement everything :) – gatb27 Dec 23 '14 at 19:24
  • Isn't it appropriate to move the business logic in EJBs then (since you are already using EJBs)? – Tiny Dec 23 '14 at 19:25
  • @tiny probably yes, but I'm doing this project following a basic example with only the registration to the site that i'm developing. I'm sure about the xhtml pages and the entities.java and some Beans like UserBean, RegistrationBean, LoginBean and CreateEvent Bean, but now i'm writing this EventData.java that i think has to be a Bean. How is possible to move the business logic in EJB? :( – gatb27 Dec 23 '14 at 20:05
  • @gatb27 you can start by moving the methods `getConnection` and `getEvents` to the EJB. Then, in your managed bean, declare a variable `List eventList` and initialize it in `@PostConstruct` method using the proper method from the EJB. This is explained in the link posted by BalusC. – Luiggi Mendoza Dec 23 '14 at 21:25
  • Thank @LuiggiMendoza ! I think that I'll do it as soon as I'll be able to make works this part. I know that is important put things in the right place, but for the moment I just want to understand how to write the right things :( I've updated my original post with other code, PLEASE look at it :) – gatb27 Dec 23 '14 at 21:48
  • @LuiggiMendoza I've move getConnection and getEvent to EJB and it works :) (And I've updated the code again with this movement ;) ) – gatb27 Dec 23 '14 at 22:05
  • I've understand that when i click on Edit, in the next page I have in the url something like http://localhost:8080/nyreg/user/event/editEvent.xhtml?id=40 where this id is effectively the ID of the selected event: now that I have this parameter in this new page, how can i put in the inputText some text that are information like place, date, type, ecc... took from the database at the same line as the ID=40 ? :) – gatb27 Dec 23 '14 at 22:36
  • Hi gatb27. You don't need to use a converter if you're just passing around primitive values like ID, but in BalusC's example he uses a converter to get the object associated with the ID. The code you've just posted shouldn't work, as you've tried to set your EditBean#event to an Integer! You must have some way of looking up the Entity associated with the ID you're passing, right? Put that code in the converter. You can get away without using a converter, but you'll still have to write code to look up Entities by ID, so you may as well figure it out. – DavidS Dec 23 '14 at 23:13
  • "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?" That's right-ish. You're using the ID/PK as an HTML query parameter when navigating to the edit page (don't think of it as "saving" the parameter). Either the converter or the EditBean must use this ID to retrieve the event and initialize the input fields. – DavidS Dec 23 '14 at 23:19
  • p.s. You're mixing JPA (EntityManager), EJB, and manual connection handling. That's OK for now, but your life will get easier when you learn more about JPA and EJB. – DavidS Dec 23 '14 at 23:28
  • p.p.s. https://jsf.zeef.com/bauke.scholtz is the best JSF link collection. – DavidS Dec 23 '14 at 23:28
  • Thanks @Nimnio , you have been very clear! I think that now I'll try to pass the e.id with a Button to the EditEventBean, that at the moment of the click has to execute something like "findSelectedEvent" using a query thank to the ID and then save the I'll have the data that I need, but how I can put them in the input form so that a user can modify the field or not and then save the event? :) – gatb27 Dec 24 '14 at 07:42
  • Thanks to all now I'm able to pass e.id to editEventBean and here fin findSelectedEvent(Integer int) i can query the DB and obtain the related data, but i'm not able to display this data in the editEvent.xhtml. I've updated the code in the post, we are near to the final solution only thank to your clarifications :) – gatb27 Dec 24 '14 at 09:07
  • Hi gatb27. The problem could be related to the scope of EditEventBean. You've annotated it RequestScoped, so it's recreated every HTTP request. Try ViewScoped. You'll also need to change EditEventBean#findSelectedEvent. It's returning a String (implicit navigational outcome), but because you want to stay on the same page you should use a postback, so change the return type to 'void'. Good luck. – DavidS Dec 24 '14 at 19:05

0 Answers0