Liny_@NotePad

沉迷ACG中

Hello World, Seam!

YOYO posted @ 2010年4月28日 09:57 in 【Java EE】 with tags Seam jsf EJB , 3840 阅读

嗯(⊙_⊙),用最简单的通讯录做一个Hello World, Seam!吧。

IDE用Eclipse JEE+JBoss Tools(主要是Seam Tools),前端JSF,会话bean和实体bean用EJB3。

功能就是CRUD。

首先在Eclipse里新建一个Seam Project,EAR类型的,起名叫Contacts 吧,在MYSQL里面建立一个叫contacts的数据库。

在SeamContacts-ejb项目里面建立两个包,分别是org.yoyo.contacts.entity和org.yoyo.contacts.session,存放实体Bean和会话Bean。

实体Bean如下:

package org.yoyo.contacts.entity;

import java.io.Serializable;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

import org.hibernate.validator.NotNull;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;

/**
 * 联系人实体
 * @author YOYO
 *
 */
@Entity
@Name("contact")
@Scope(ScopeType.EVENT)
@Table(name="contacts")
public class Contact implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * 姓名
	 */
	private String name;
	
	/**
	 * 电话
	 */
	private String telphone;
	
	/**
	 * 邮箱
	 */
	private String email;

	@Id @NotNull
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getTelphone() {
		return telphone;
	}

	public void setTelphone(String telphone) {
		this.telphone = telphone;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}
	
}

会话Bean分为接口和实现类:

本地接口

package org.yoyo.contacts.session;

import javax.ejb.Local;

/**
 * 地址簿接口
 * 
 * @author YOYO
 * 
 */
@Local
public interface AddressBook {

	/**
	 * 添加联系人
	 * 
	 * @return
	 */
	public String addToBook();
	
	/**
	 * 开始编辑
	 * @return
	 */
	public String beginEdit();
	
	/**
	 * 编辑结束
	 * @return
	 */
	public String edited();
	
	/**
	 * 删除
	 * @return
	 */
	public String delete();

	/**
	 * 获得联系人列表
	 * 
	 * @return
	 */
	public void findContacts();

}

SFSB:

package org.yoyo.contacts.session;

import java.io.Serializable;
import java.util.List;

import javax.ejb.Remove;
import javax.ejb.Stateful;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceContextType;

import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.Begin;
import org.jboss.seam.annotations.Destroy;
import org.jboss.seam.annotations.End;
import org.jboss.seam.annotations.Factory;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Out;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.annotations.datamodel.DataModel;
import org.jboss.seam.annotations.datamodel.DataModelSelection;
import org.yoyo.contacts.entity.Contact;

/**
 * 地址簿会话Bean
 * 
 * @author YOYO
 * 
 */
@Stateful
@Scope(ScopeType.SESSION)
@Name("addressBook")
@SuppressWarnings("unused")
public class AddressBookAction implements Serializable, AddressBook {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * 当前JSF上下文
	 */
	private FacesContext facesContext = FacesContext.getCurrentInstance();

	/**
	 * 联系人
	 */
	@In(required = false)
	private Contact contact;

	/**
	 * 联系人列表
	 */
	@DataModel
	private List<Contact> contacts;

	/**
	 * 当前选择的联系人
	 */
	@DataModelSelection
	@In(required = false)
	@Out(required = false, scope = ScopeType.CONVERSATION)
	private Contact selectedContact;

	/**
	 * 持久化容器
	 */
	@PersistenceContext(type = PersistenceContextType.EXTENDED)
	private EntityManager em;

	/**
	 * 添加联系人
	 */
	public String addToBook() {
		long count = (Long) em.createQuery(
				"SELECT COUNT(*) FROM Contact WHERE name = :name")
				.setParameter("name", contact.getName()).getSingleResult();

		if (count == 0) {
			em.persist(contact);

			findContacts();

			return "home";
		} else {
			facesContext.addMessage(null, new FacesMessage(
					"Name already exists!"));

			return null;
		}
	}

	/**
	 * 开始编辑
	 */
	@Begin
	public String beginEdit() {
		return "edit";
	}

	/**
	 * 编辑结束
	 */
	@End
	public String edited() {
		em.persist(selectedContact);

		findContacts();

		return "home";
	}

	/**
	 * 删除
	 */
	public String delete() {
		em.remove(selectedContact);

		findContacts();

		return "home";
	}

	/**
	 * 获得联系人列表
	 */
	@SuppressWarnings("unchecked")
	@Factory("contacts")
	public void findContacts() {
		contacts = (List<Contact>) em.createQuery("from Contact")
				.getResultList();
	}

	@Remove
	@Destroy
	public void destroy() {

	}

}

前端在SeamContacts项目中,修改WebContent目录下的几个文件

home.xhtml(地址簿列表,进行查询和删除)主要代码:

<h:form>
	<h:dataTable value="#{contacts}" var="currentContact">
		<h:column>
			<f:facet name="header">
				<h:outputText value="Name" />
			</f:facet>
			<h:outputText value="#{currentContact.name}" />
		</h:column>
		<h:column>
			<f:facet name="header">
				<h:outputText value="Telphone" />
			</f:facet>
			<h:outputText value="#{currentContact.telphone}" />
		</h:column>
		<h:column>
			<f:facet name="header">
				<h:outputText value="E-Mail" />
			</f:facet>
			<h:outputText value="#{currentContact.email}" />
		</h:column>
		<h:column>
			<h:commandLink value="Edit" action="#{addressBook.beginEdit}" />
		</h:column>
		<h:column>
			<h:commandLink value="Delete" action="#{addressBook.delete}" />
		</h:column>
	</h:dataTable>
	<h:commandLink action="add" value="Add Contact" />
</h:form>

add.xhtml(添加信息)主要代码:

<h:form>
	<p>add your contact people info:</p>
	<ul class="bullets">
		<li><strong>Name</strong>:<h:inputText value="#{contact.name}" /></li>
		<li><strong>Telphone</strong>:<h:inputText value="#{contact.telphone}" /></li>
		<li><strong>EMail</strong>:<h:inputText value="#{contact.email}" /></li>
		<li><h:commandButton action="#{addressBook.addToBook}" value="Add Contact" /></li>
	</ul>
</h:form>

edit.xhtml(修改信息)主要代码:

<h:form>
	<p>edit your contact people info:</p>
	<ul class="bullets">
		<li><strong>Name</strong>:<h:outputText
			value="#{selectedContact.name}" /></li>
		<li><strong>Telphone</strong>:<h:inputText
			value="#{selectedContact.telphone}" /></li>
		<li><strong>EMail</strong>:<h:inputText
			value="#{selectedContact.email}" /></li>
		<li><h:commandButton action="#{addressBook.edited}"
			value="Edit Contact" /></li>
	</ul>
</h:form>

以及跳转的配置(pages.xml部分代码):

    <page view-id="*">
        <navigation>
            <rule if-outcome="home">
                <redirect view-id="/home.xhtml"/>
            </rule>
            <rule if-outcome="add">
                <redirect view-id="/add.xhtml"/>
            </rule>
            <rule if-outcome="edit">
                <redirect view-id="/edit.xhtml"/>
            </rule>
        </navigation>
    </page>

简单的Seam CRUD到此为止,但是对于其流程怎么走还是有点混乱,待继续研究。


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter