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