Simple blog in javaEE, part1

2012-10-13 22:50:54

Some notImportant information. 0) Why I wrote this post in English (at least I was trying to write in English), because I want practice my English. 1) If you want learn how to write blog, it's wrong place for you. 2) If you want learn some basics about javaEE, it's good place for you. 3) I use Netbeans 6.8 (o my God, it's too old, but now I don't have a time to reinstall my Envirnoment) 4) Create new Project Java Web->Web Application 4.1) In the last (4 steps) you have to choose for :JavaServel Faces" libraries in 2.0 version and for Hibernate 3:2.5 you have to create new connection. 5) After that, you have to start Tomcat (I use Tomcat 6.0) 6) Now you can click Run Project in your NetBeans. If you see page (http://localhost:8080/Blog/) with default message,it's OK.

Start with Facelets Template 1) First of all, we must add Facelets Template 2) Click New->Facelets Template in the project. If you don't see this option in context menu, choose Other. In "JavaServer Faces" check "Facelets Template" and click Next. 3) Set File Name "layout" and select Layout css with top menu and left bar, next remove index file and create new. Create "Facelets Template Client", set name index and choose

for genereted root tag, in template file select layout.xhtml (file create in previous step) 4) Now you can Run project and check if everything works. If You see that page, everything is ok.

5) Next, we must change some information in web.xml file.

<servlet-name>Faces Servlet</servlet-name>

This is important, if you prefer url without /face/

6) In the layout.xhtml file remove

context and add menu

<ui:insert name='"left"'>Left</ui:insert>
<li><h:outputlink value='"index.xhtml"'>Home Page</h:outputlink></li>
<li><h:outputlink value='"index.xhtml"'>Random Post</h:outputlink></li>
<li><h:outputlink value='"index.xhtml"'>Leave a message</h:outputlink></li>

7) In index.xhtml remove code

<ui:define name='"left"'>

because we create menu in main file.

8) Create database

`name` varchar(255) NOT NULL,
`description` text NOT NULL,
`slug` varchar(120) NOT NULL,
mysq&gt; DESC post; (yes we love console)
| Field       | Type         | Null | Key | Default | Extra          |
| id          | int(11)      | NO   | PRI | NULL    | auto_increment |
| name        | varchar(255) | NO   |     | NULL    |                |
| description | text         | NO   |     | NULL    |                |
| slug        | varchar(120) | NO   |     | NULL    |                |

10) Develop ouer project structure. Add do Source Packages - btbw.blog.config - btbw.blog.controllers - btbw.blog.entity

Ok, we have some html files, we create database with one tabes (post). And now we want get some information (post) form ouer databse.

11) Create New Entity Clases from Database. (Other->Persistance->Entity Casses from Database) On the creator select your databases conection and add "post" table form Available Tables to Selected Tables, on the next step click "Create Persistence Unit..." (important select Hibernate on select opction). On the last step (4.Mapping Options) in Association Featch select "eager" and Select only "Attributes for Regenerating Tables", and click Finish. And Magic happens..........

12) Post.java is very important class if we want use ORM in Java, now you can familarize yourself with this class.

13) Create DBManager.java

package btbw.blog.config;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

 * @author Krzysztof Rychlicki-Kicior
public class DBManager
    private static DBManager instance;
    private EntityManagerFactory emf;

    private DBManager()


    public synchronized static DBManager getManager()
        if(instance == null)
            instance = new DBManager();
        return instance;

    public EntityManagerFactory createEntityManagerFactory()
        if(emf == null)
            emf = Persistence.createEntityManagerFactory("BlogPU");
        return emf;

    public EntityManager createEntityManager()
        return this.createEntityManagerFactory().createEntityManager();

    public void coseEntityManagerFactory()
        if(emf != null)

14) Create PostController.java in btbw.blog.controllers

 * To change this template, choose Tools | Templates
 * and open the template in the editor.

package btbw.blog.controllers;

import btbw.blog.config.DBManager;
import btbw.blog.entity.Post;
import javax.persistence.EntityManager;
import java.util.List;
 * @author Michał Szałkowski - Szalek
public class PostController
    private Post post = new Post();

    public PostController()


    public Post getPost()
        return this.post;

    public void setPost(Post _post)
        this.post = _post;

    public List getList()
        EntityManager em = DBManager.getManager().createEntityManager();
        List list = em.createNamedQuery("Post.findAll").getResultList();
        return list;

15) Create faces-config.xml file in WEB-INF

<?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?>
<faces-config http:="" java.sun.com="" javaee="" ns="" version='"2.0"' web-facesconfig_2_0.xsd&quot;="" xml="" xmlns='"http://java.sun.com/xml/ns/javaee"' xmlns:xsi='"http://www.w3.org/2001/XMLSchema-instance"' xsi:schemalocation='"http://java.sun.com/xml/ns/javaee'>
  1. Last part is modifice index.xhtml
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE composition PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;>

<ui:composition template='"./layout.xhtml"' xmlns:ui='"http://java.sun.com/jsf/facelets"'>
<ui:define name='"top"'>
        My "super" Blog - Home Page.
<ui:define name='"content"'>
<ui:repeat value='"#{postController.list}"' var='"post"'>

[gallery link="file"]