Simple Example of EJB integration with JSF

Source: Internet
Author: User

Simple Example of EJB integration with JSF

Program directory:

Running effect:

1. First import the jar package,

Import necessary JSF jar packages and hibernate3.6jar packages
(Jsf jar will be available for download later)

2. Configure the Data source: Create the/MATA-INF/persistence. xml file under src

  
     
         
    
     java:jboss/datasources/localTrans/FirstTest
          
             
              
              
       
        
       
      
     
       
   
  
Configure the data source in jboss. Specific configuration -- (please follow the blog below) 3. Add the jsf controller to web. xml

The. jsf request is intercepted here (you can write it as needed)


  
    
   
    JSF
         
         
    
     javax.faces.CONFIG_FILES
          
    
     /WEB-INF/faces-config.xml
          
         
             
    
     Faces Servlet
              
    
                   javax.faces.webapp.FacesServlet          
              
    
     1
          
         
             
    
     Faces Servlet
              
    
     *.jsf
         
       
       
    
     index.jsp
      
     
  
4. The page is as follows:

Index. jsp


  
    
   
    JSF
         
         
    
     javax.faces.CONFIG_FILES
          
    
     /WEB-INF/faces-config.xml
          
         
             
    
     Faces Servlet
              
    
                   javax.faces.webapp.FacesServlet          
              
    
     1
          
         
             
    
     Faces Servlet
              
    
     *.jsf
         
       
       
    
     index.jsp
      
     
  

Success. jsp

<%@ page language=java import=java.util.* pageEncoding=UTF-8%><%@ taglib uri=http://java.sun.com/jsf/core prefix=f %><%@ taglib uri=http://java.sun.com/jsf/html prefix=h %>
Jboss-based JSF + EJB + JPA integration successful All messages 5. configuring jsf: creating a WEB-INF under a faces-config.xml

      
     
       
    
     addNews
        
    
     com.etoak.controller.AddNewsBean
        
    
     session
    
   
       
    
     viewAll
        
    
     com.etoak.controller.ViewAllNews
        
    
     session
    
   
       
    
     /index.jsp
        
            
     
      success
             
     
      /success.jsp
         
    
   
       
    
     /success.jsp
        
            
     
      /success.jsp
         
    
   
  
6. Compile the ejb Entity bean:
package com.etoak.entity;import javax.persistence.Column;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.GenerationType;import javax.persistence.Id;import javax.persistence.Table;@Entity@Table(name=news_table)public class News {    @Id    @GeneratedValue(strategy = GenerationType.IDENTITY)    private int id;    @Column(name=news_title,nullable=true)    private String title;    @Column(name=news_content,nullable=true)    private String content;    public int getId() {        return id;    }    public void setId(int id) {        this.id = id;    }    public String getTitle() {        return title;    }    public void setTitle(String title) {        this.title = title;    }    public String getContent() {        return content;    }    public void setContent(String content) {        this.content = content;    }   }
7. Write the eao layer (dao layer). Here we add the service Middle Layer

NewsEao. java

package com.etoak.eao;import java.util.List;import javax.ejb.Local;import com.etoak.entity.News;@Localpublic interface NewsEao {    public News get(Integer id);    public void save(News news);    public void update(News news);    public void delete(Integer id);    public void delete(News news);    public List
  
    findAll();}
  

NewsEaoImpl. java

Package com. etoak. eao; import java. util. list; import javax. ejb. stateless; import javax. persistence. entityManager; import javax. persistence. persistenceContext; import com. etoak. entity. news; @ Stateless (name = newsEao) public class NewsEaoBean implements NewsEao {// dependency injection EntityManager component @ PersistenceContext (unitName = newsUnit) private EntityManager em; @ Override public News get (Integer id) {return em. find (News. class, id) ;}@ Override public void save (News news) {em. persist (news) ;}@ Override public void update (News news) {em. merge (news) ;}@ Override public void delete (Integer id) {em. remove (get (id) ;}@ Override public void delete (News news) {em. remove (news) ;}@ Override public List
  
   
FindAll () {return (List
   
    
) Em. createQuery (select news from News as news). getResultList ();}}
   
  

NewsService. java

package com.etoak.service;import java.util.List;import javax.ejb.Local;import com.etoak.entity.News;@Localpublic interface NewsService {    public int addNews(String title,String content);    List
  
    getAllNews();}
  

NewsServiceImpl. java

package com.etoak.service;import java.util.List;import javax.ejb.EJB;import javax.ejb.Stateless;import javax.ejb.TransactionAttribute;import javax.ejb.TransactionAttributeType;import javax.ejb.TransactionManagement;import javax.ejb.TransactionManagementType;import com.etoak.eao.NewsEao;import com.etoak.entity.News;@Stateless(name=newsService)@TransactionManagement(TransactionManagementType.CONTAINER)public class NewsServiceImpl implements NewsService {    @EJB(beanName=newsEao)    private NewsEao newsEao;    @Override@TransactionAttribute(TransactionAttributeType.REQUIRED)    public int addNews(String title, String content) {        News news = new News();        news.setTitle(title);        news.setContent(content);        newsEao.save(news);        return news.getId();    }    @Override@TransactionAttribute(TransactionAttributeType.REQUIRED)    public List
  
    getAllNews() {        return newsEao.findAll();    }}
  
8. controller layer. The managed bean of jsf is used here.

AddNewsBean. java

package com.etoak.controller;import javax.ejb.EJB;import com.etoak.service.NewsService;public class AddNewsBean {    @EJB(beanName=newsService)    private NewsService newsService;    private String title;    private String content;    public String getTitle() {        return title;    }    public void setTitle(String title) {        this.title = title;    }    public String getContent() {        return content;    }    public void setContent(String content) {        this.content = content;    }    public String addNews(){        newsService.addNews(title, content);        return success;    }}

ViewAllNews. java

package com.etoak.controller;import java.util.List;import javax.ejb.EJB;import com.etoak.entity.News;import com.etoak.service.NewsService;public class ViewAllNews {    @EJB(beanName=newsService)    private NewsService newsService;    private List
  
    newsList;    public void setNewsList(List
   
     newsList){        this.newsList = newsList;    }    public List
    
      getNewsList(){        return newsService.getAllNews();    }}
    
   
  

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.