我试图用数据库实现Primefaces Schedule:
<p:outputLabel for="from" value="From:" />
<p:calendar id="from" value="#{schedule.event.startDate}" timeZone="GMT+2" pattern="dd/MM/yyyy HH:mm"/>
<p:outputLabel for="to" value="To:" />
<p:calendar id="to" value="#{schedule.event.endDate}" timeZone="GMT+2" pattern="dd/MM/yyyy HH:mm"/>
<p:outputLabel for="allDay" value="All Day:" />
<h:selectBooleanCheckbox id="allDay" value="#{schedule.event.allDay}" />
<p:commandButton type="reset" value="Reset" />
<p:commandButton id="addButton" value="Save" actionListener="#{schedule.addEvent}" oncomplete="PF('myschedule').update();PF('eventDialog').hide();" />
<p:commandButton value="Delete" oncomplete="PF('myschedule').update(); PF('eventDialog').hide();"
actionListener="#{schedule.deleteEvent}"/>
</h:panelGrid>
</p:dialog>
CDI Bean:
@Resource(name = "jdbc/DefaultDB")
private DataSource ds;
private ScheduleModel lazyEventModel;
private ScheduleEvent event = new DefaultScheduleEvent();
@PostConstruct
public void init()
{
lazyEventModel = new LazyScheduleModel()
{
@Override
public void loadEvents(Date start, Date end)
{
try
{
Connection conn = ds.getConnection();
PreparedStatement ps = null;
try
{
ps = conn.prepareStatement("SELECT * FROM SCHEDULE WHERE START_DATE >= ? AND END_DATE < ?");
ps.setDate(1, new java.sql.Date(start.getTime()));
ps.setDate(2, new java.sql.Date(end.getTime()));
ResultSet rs = ps.executeQuery();
while (rs.next())
{
addEvent(new DefaultScheduleEvent(rs.getString("TITLE"), rs.getDate("START_DATE"), rs.getDate("END_DATE")));
}
}
finally
{
if (ps != null)
{
ps.close();
}
conn.close();
}
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
};
}
public ScheduleModel getEventModel()
{
return lazyEventModel;
}
public ScheduleEvent getEvent()
{
return event;
}
public void setEvent(ScheduleEvent event)
{
this.event = event;
}
public void addEvent(ActionEvent actionEvent) throws Exception
{
String title = event.getTitle();
String description = event.getDescription();
Date startDate = event.getStartDate();
Date endDate = event.getEndDate();
if (event.getId() == null)
{
insertData(title, description, startDate, endDate);
lazyEventModel.addEvent(event);
}
else
{
updateData(title, description, startDate, endDate);
lazyEventModel.updateEvent(event);
}
event = new DefaultScheduleEvent();
}
public void deleteEvent(ActionEvent actionEvent) throws Exception
{
String title = event.getTitle();
delete(title);
lazyEventModel.deleteEvent(event);
}
public void onEventSelect(SelectEvent selectEvent)
{
event = (ScheduleEvent) selectEvent.getObject();
}
public void onDateSelect(SelectEvent selectEvent)
{
event = new DefaultScheduleEvent("", (Date) selectEvent.getObject(), (Date) selectEvent.getObject());
}
public void onEventMove(ScheduleEntryMoveEvent event) throws Exception
{
String title = event.getScheduleEvent().getTitle();
String description = event.getScheduleEvent().getDescription();
Date startDate = event.getScheduleEvent().getStartDate();
Date endDate = event.getScheduleEvent().getEndDate();
updateData(title, description, startDate, endDate);
FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Event moved", "Day delta:" + event.getDayDelta() + ", Minute delta:" + event.getMinuteDelta());
addMessage(message);
}
public void onEventResize(ScheduleEntryResizeEvent event)
{
FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Event resized", "Day delta:" + event.getDayDelta() + ", Minute delta:" + event.getMinuteDelta());
addMessage(message);
}
private void addMessage(FacesMessage message)
{
FacesContext.getCurrentInstance().addMessage(null, message);
}
public void updateData(String title, String description, Date startDate, Date endDate) throws SQLException, Exception
{
Connection conn = ds.getConnection();
PreparedStatement ps = null;
boolean committed = false;
try
{
conn.setAutoCommit(false);
String updateComponentStats = "UPDATE SCHEDULE SET TITLE = ?, START_DATE = ?, END_DATE = ?, DESCRIPTION = ?, LAST_UPDATED = CURRENT_DATE WHERE TITLE = ?";
ps = conn.prepareStatement(updateComponentStats);
ps.setString(1, title);
ps.setDate(2, new java.sql.Date(startDate.getTime()));
ps.setDate(3, new java.sql.Date(endDate.getTime()));
ps.setString(4, description);
ps.setString(5, title);
ps.executeUpdate();
ps.close();
conn.commit();
committed = true;
}
finally
{
if (!committed)
{
conn.rollback();
}
if (ps != null)
{
ps.close();
}
conn.close();
}
}
public void insertData(String title, String description, Date startDate, Date endDate) throws SQLException, Exception
{
Connection conn = ds.getConnection();
PreparedStatement ps = null;
boolean committed = false;
try
{
conn.setAutoCommit(false);
String updateComponentStats = "INSERT INTO SCHEDULE (TITLE, START_DATE, END_DATE, DESCRIPTION, CREATED) "
+ " VALUES (?, ?, ?, ?, CURRENT_DATE)";
ps = conn.prepareStatement(updateComponentStats);
ps.setString(1, title);
ps.setDate(2, new java.sql.Date(startDate.getTime()));
ps.setDate(3, new java.sql.Date(endDate.getTime()));
ps.setString(4, description);
ps.executeUpdate();
ps.close();
conn.commit();
committed = true;
}
finally
{
if (!committed)
{
conn.rollback();
}
if (ps != null)
{
ps.close();
}
conn.close();
}
}
当我使用对话框创建新事件时,我成功创建了前一天的新事件。我插入新事件时将java.util.Date
转换为java.sql.Date
。
你能说清楚如何解决这个问题吗?
答案 0 :(得分:1)
在您的.xhtml文件中,将其添加到其中
<p:schedule clientTimeZone="local"> </p:schedule>