EJB整合JSF簡單的小例子

來源:互聯網
上載者:User

EJB整合JSF簡單的小例子

程式目錄:

運行效果:

一、首先匯入jar包,

匯入JSF必要 jar包以及hibernate3.6jar包
(jsf jar稍後提供下載)

二、配置資料來源:在src下建立/MATA-INF/persistence.xml 檔案
         java:jboss/datasources/localTrans/FirstTest                                                   
jboss中配置資料來源。具體配置——(請關注下篇部落格)三、web.xml中添加 jsf 控制器

這裡攔截的是.jsf請求(可以隨便寫)

  JSF            javax.faces.CONFIG_FILES      /WEB-INF/faces-config.xml                      Faces Servlet                        javax.faces.webapp.FacesServlet                    1                      Faces Servlet          *.jsf             index.jsp    
四、頁面如下

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的JSF+EJB+JPA整合成功 所有訊息 五、配置 jsf : 在WEB-INF下建立 faces-config.xml
           addNews    com.etoak.controller.AddNewsBean    session    viewAll    com.etoak.controller.ViewAllNews    session    /index.jsp            success        /success.jsp        /success.jsp            /success.jsp    
六、編寫ejb實體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;    }   }
七、編寫會eao層(dao層),這裡我們加入service中介層

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{    //依賴注入EntityManager組件    @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();    }}
八、controller層。這裡使用jsf的託管bean.

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();    }}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.