p:dataTable,单击可导航到包含可编辑数据的新页面

时间:2014-12-23 17:14:53

标签: jquery jsf jsf-2 primefaces

我是Java EE,XHTML,JSF等的新手。所以如果我以错误的方式写东西,请不要侮辱我。我已经在这个网站和Google上搜索过但我没有找到解决问题的正确方法。

我创建了一个Bean(“EventData.java”),它从一个数据库(名为nyreg with tables event,calendarevents)获取一些信息并将它们放在一个xhtml页面中(带有dataTable的“eventData.xhtml”)(它本质上是一个包含事件列表的页面)。 现在我已经集成了一个rowSelection,它引导我进入一个名为“eventDetail.xhtml”的新页面。我的目标是在eventDetail.xhtml中有一个<p:panelGrid>由不同的<h:outputLabel><p:inputText>组成,其中每个<p:inputText>内都有上一页中提出的信息“eventData.xhtml”(显然是关于点击事件的信息)。我需要信息在<p:inputText>中,因为在这个页面中必须能够修改信息并更新数据库:我认为我能够做到最后这部分,但是现在我需要可视化这些<p:inputText>中的信息(或具有相同属性的内容)。

我试图在不同的参考文献中获得这个,但现在我被阻止了。

从数据库捕获数据的Bean是 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);
    }
    }
}

有表格的页面是 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>

必须有关于所选事件的信息的页面是 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>

现在我有一个 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";
}


}

最后这是我的 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;
}

}

正如你在Bean的末尾看到的那样,我为SelectedEvent设置了getter和setter,以及一个导致页面eventDetail的“eventSelect”(但我不知道这是否使用了我需要的东西),但是然后我不知道如何实现我需要的东西。

我希望你能尝试提出类似我需要的解决方案,我将永远感激:D


更新1:我已阅读BalusC发布的链接,并尝试将澄清应用于我的案例,但我没有抓到一些内容。

  • 我认为在eventData.xhtml中,当我点击编辑时我需要保存e.id,因为它是类Event的主键,我需要它来获取所选事件的数据。下一页?

  • 在editEvent.xhtml中我不明白viewParam是如何工作的,在我的情况下,当我点击eventData.xhtml中的Edit时,我导致editEvent.xhtml,但它完全是白色而没有错误。

  • 我需要转换器吗?有没有更简单的方法:保存所选事件的e.id,转到页面editEvent.xhtml传递此参数(类似于转换?)并使用inputForm和inputText以及inputTextarea可视化信息,以便它们可以编辑吗?


更新2:今天早上我向前迈了一步谢谢大家:)现在在eventData.xhtml中我放了一个commandButton,允许我将e.id传递给EditEventBean.java使用findSelectedEvent(Integer id)查询数据库(然后我将此部分移动到EventManager;))以获取与传递的ID相关的事件数据。它实际上有效,在EditEventBean.java中你可以看到有一些System.out.println向我显示name,place,ecc的正确值。

现在我想在editEvent.xhtml中可视化这些数据。首先,我尝试使用类似“事件名称为#{editEventBean.selected.name}”的内容在页面中显示它们,但页面仅显示“事件名称为”,然后仅显示。

我还尝试使用outputFormat,目的是显示类似“Name:name_of_event”的内容,然后显示inputForm。然后我需要name_of_event已经在inputForm中。这里的问题是name_of_event取自#{editEventBean.selected.name},我认为它与System.out打印的值相同,但这里我得到一个Null。为什么? :(

0 个答案:

没有答案