程序目录:
运行效果:
一、首先导入jar包,导入JSF必要 jar包以及hibernate3.6jar包
(jsf jar稍后提供下载)
二、配置数据源:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0"
xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="newsUnit">
<jta-data-source>java:jboss/datasources/localTrans/FirstTest</jta-data-source>
<properties>
<!-- Properties for Hibernate -->
<property name="hibernate.hbm2ddl.auto" value="update" />
<property name="hibernate.show_sql" value="false" />
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
</properties>
</persistence-unit>
</persistence>
jboss中配置数据源。具体配置——(请关注下篇博客)
三、web.xml中添加 jsf 控制器,这里拦截的是.jsf请求(可以随便写)
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
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
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<display-name>JSF</display-name>
<context-param>
<param-name>javax.faces.CONFIG_FILES</param-name>
<param-value>/WEB-INF/faces-config.xml</param-value>
</context-param>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>
javax.faces.webapp.FacesServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.jsf</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
四、页面如下
index.jsp
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
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
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<display-name>JSF</display-name>
<context-param>
<param-name>javax.faces.CONFIG_FILES</param-name>
<param-value>/WEB-INF/faces-config.xml</param-value>
</context-param>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>
javax.faces.webapp.FacesServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.jsf</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
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" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>添加成功</title>
</head>
<body>
<h1>基于jboss的JSF+EJB+JPA整合成功</h1>
<h2>所有消息</h2>
<f:view>
<h:dataTable width="600px" border="1" value="#{viewAll.newsList }" var="news" rowClasses="odd,even">
<f:facet name="caption" >
<h:outputText value="消息列表" />
</f:facet>
<h:column>
<f:facet name="header">
<h:outputText value="消息ID"></h:outputText>
</f:facet>
<h:outputText value="#{news.id }"></h:outputText>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="消息标题"></h:outputText>
</f:facet>
<h:outputText value="#{news.title }"></h:outputText>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="消息内容"></h:outputText>
</f:facet>
<h:outputText value="#{news.content }"></h:outputText>
</h:column>
</h:dataTable>
</f:view>
</body>
</html>
五、配置 jsf : 在WEB-INF下建立 faces-config.xml
<?xml version="1.0"?>
<!DOCTYPE faces-config PUBLIC
"-//Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.0//EN"
"http://java.sun.com/dtd/web-facesconfig_1_0.dtd">
<faces-config>
<managed-bean>
<managed-bean-name>addNews</managed-bean-name>
<managed-bean-class>com.etoak.controller.AddNewsBean</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>
<managed-bean>
<managed-bean-name>viewAll</managed-bean-name>
<managed-bean-class>com.etoak.controller.ViewAllNews</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>
<navigation-rule>
<from-view-id>/index.jsp</from-view-id>
<navigation-case>
<from-outcome>success</from-outcome>
<to-view-id>/success.jsp</to-view-id>
</navigation-case>
</navigation-rule>
<navigation-rule>
<from-view-id>/success.jsp</from-view-id>
<navigation-case>
<to-view-id>/success.jsp</to-view-id>
</navigation-case>
</navigation-rule>
</faces-config>
六、编写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;
@Local
public 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<News> 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<News> findAll() {
return (List<News>)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;
@Local
public interface NewsService {
public int addNews(String title,String content);
List<News> 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<News> 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<News> newsList;
public void setNewsList(List<News> newsList){
this.newsList = newsList;
}
public List<News> getNewsList(){
return newsService.getAllNews();
}
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/frightingforambition/article/details/48781983