Hello.
I'm trying to add a single test record (a comment item) to my MySQL database.
So here are my sources of JSF2 project.
- main xhtml page:
<h:body>
<h:panelGroup id="mainBlock" layout="block">
<h:panelGroup id="header" layout="block">
<h:graphicImage value="resources/logo.jpg" style=""/>
</h:panelGroup><br/>
<h:panelGroup id="menu" layout="block">
<h:panelGrid columns="1" style="">
<h:outputLabel value="MENU" style=""/>
<br/>
<h:form>
<h:commandLink action="#{navigationBean.requestPage}">
<f:param name="requestedPage" value="news.xhtml"/>
<f:ajax execute="@this" render="@none" />
<f:ajax render=":content"/>
<h:graphicImage value="resources/news.png"/>
</h:commandLink>
<br/><br/>
<h:commandLink action="#{navigationBean.requestPage}">
<f:param name="requestedPage" value="comments.xhtml"/>
<f:ajax execute="@this" render="@none"/>
<f:ajax render=":content"/>
<h:graphicImage value="resources/comments.png"/>
</h:commandLink>
<h:commandLink action="#{navigationBean.requestPage}">
<f:param name="requestedPage" value="login.xhtml"/>
<f:ajax execute="@this" render="@none"/>
<f:ajax render=":content"/>
<h:graphicImage value="resources/login.png" style=""/>
</h:commandLink>
</h:form>
</h:panelGrid>
</h:panelGroup>
<h:panelGroup id="content" layout="block">
<ui:include src="#{navigationBean.viewedPage}"/>
</h:panelGroup>
</h:panelGroup>
</h:body>
- xhtml page where I try to add a comment into DB after clicking the button:
<h:dataTable id="table" value="#{db.commentsList}" var="c" style="">
<h:column>
<f:facet name="header">
User
</f:facet>
#{c.user}
</h:column>
<h:column>
<f:facet name="header">
Comment
</f:facet>
#{c.content}
</h:column>
<h:column>
<f:facet name="header">
Date
</f:facet>
#{c.data}
</h:column>
</h:dataTable>
<br/><br/>
<h:form>
<h:commandButton action="#{db.addComment}" value="Submit"/>
</h:form>
- java code of my bean:
@ManagedBean(name = "db")
public class Database
{
private List<Comment> commentsList;
.....
public void addComment()
{
Connection conn = null;
try
{
Class.forName("com.mysql.jdbc.Driver").newInstance();
conn = DriverManager.getConnection("jdbc:mysql:///jsfproject_db", "root", "pwd");
PreparedStatement ps = conn.prepareStatement("INSERT INTO comments VALUES (null, 'test', cast('2000-01-01' AS date), 'test')");
ps.executeUpdate();
ps = conn.prepareStatement("SELECT user, content, data FROM comments");
ResultSet results = ps.executeQuery();
commentsList = new ArrayList<Comment>();
while (results.next())
{
Comment com = new Comment();
com.user = results.getString("user");
com.content = results.getString("content");
com.data = results.getDate("data");
commentsList.add(com);
}
}
catch (Exception ex)
{
Logger.getLogger(Database.class.getName()).log(Level.SEVERE, null, ex);
} finally
{
try
{
conn.close();
} catch (SQLException ex)
{
Logger.getLogger(Database.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
The double f:ajax tags in commandLink body are to solve a problem, where I had to double click the button to reload content div.
And now here comes similar problem:
After loading the page, the first click of comment adding button never works - nothing happens. I was adding some logging methods into the addComment method and it is not executed at all on first click. I suspect it's caused by my Ajax reloaded div code, because when I launch such button on a plain page, it works good.
Any suggestions how to fix my code? Alternatively are there other ways of realizing panel refreshing instead of using ui:include tag, which might be causing the problem here?