my CRUD way, is it right? show yours please!
kaiak Mar 18, 2007 11:15 PMjboss-seam-1.2.0
jboss-4.0.5.GA
jdk1.5.0_0.8
the following is my way to do CRUD,it works!
but I'm not sure if it will cause some potential problems ,like performance.
please show me yours!
I like seam so much!
package org.kaiak.model; import java.io.Serializable; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.Table; import javax.persistence.Version; import org.hibernate.validator.Length; import org.jboss.seam.annotations.Name; @Entity @Name("permission") @Table(name = "t_myor_permission") public class Permission implements Serializable { //seam-gen attributes (you should probably edit these) private Long id; private Integer version; private String name; //add additional entity attributes //seam-gen attribute getters/setters with annotations (you probably should edit) @Id @GeneratedValue public Long getId() { return id; } public void setId(Long id) { this.id = id; } @Version public Integer getVersion() { return version; } private void setVersion(Integer version) { this.version = version; } @Length(max=20) public String getName() { return name; } public void setName(String name) { this.name = name; } }
package org.kaiak.service; import javax.ejb.Local; @Local public interface PermissionManager { public String persist(); public String edit(); public String delete(); public void findPermissions(); public void destroy(); //add additional interface methods here } package org.kaiak.service; import java.util.List; import javax.ejb.Remove; import javax.ejb.Stateful; import javax.persistence.EntityManager; 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.Logger; import org.jboss.seam.annotations.Name; import org.jboss.seam.annotations.Out; import org.jboss.seam.annotations.datamodel.DataModel; import org.jboss.seam.annotations.datamodel.DataModelSelection; import org.jboss.seam.core.FacesMessages; import org.jboss.seam.log.Log; import org.kaiak.model.Permission; @Stateful @Name("permissionManager") public class PermissionManagerBean implements PermissionManager { @Logger private Log log; @In FacesMessages facesMessages; @In EntityManager entityManager; @DataModel private List<Permission> permissionList; @In(required = false) @Out(required = false) private Permission permission; @DataModelSelection private Permission selectedPermission; @Begin @Factory("permissionList") public void findPermissions() { permissionList = entityManager.createQuery( "select perm from Permission perm order by perm.id") .getResultList(); } @End(beforeRedirect = true) public String persist() { entityManager.persist(permission); return "list"; } @End(beforeRedirect = true) public String delete() { permission = selectedPermission; permissionList.remove(permission); entityManager.remove(permission); return "list"; } public String edit() { if (selectedPermission != null) permission = selectedPermission; return "form"; } @Destroy @Remove public void destroy() { } }
permissionList.xhtml <!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <ui:composition xmlns="http://www.w3.org/1999/xhtml" xmlns:s="http://jboss.com/products/seam/taglib" xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:f="http://java.sun.com/jsf/core" xmlns:h="http://java.sun.com/jsf/html" template="layout/template.xhtml"> <ui:define name="body"> <h1>permissionList</h1> <p>Generated list page</p> <h:messages globalOnly="true" styleClass="message"/> <h:outputText value="No permission exists" rendered="#{permissionList.rowCount==0}"/> <h:dataTable id="permissionList" var="p" value="#{permissionList}" rendered="#{permissionList.rowCount>0}"> <h:column> <f:facet name="header">Id</f:facet> #{p.id} </h:column> <h:column> <f:facet name="header">Name</f:facet> <s:link id="permission" value="#{p.name}" action="#{permissionManager.edit}"/> </h:column> <h:column> <f:facet name="header">Action</f:facet> <s:link id="user" value="Delete" action="#{permissionManager.delete}"/> </h:column> </h:dataTable> <div class="actionButtons"><h3><h:outputText value="#{permission.name}"/></h3> <s:button id="done" value="Create permission" action="#{permissionManager.edit}"/> </div> </ui:define> </ui:composition>
permissionForm.xhtml <!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <ui:composition xmlns="http://www.w3.org/1999/xhtml" xmlns:s="http://jboss.com/products/seam/taglib" xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:f="http://java.sun.com/jsf/core" xmlns:h="http://java.sun.com/jsf/html" template="layout/template.xhtml"> <ui:define name="body"> <h1>permissionForm</h1> <p>Generated edit page</p> <h:messages globalOnly="true" styleClass="message"/> <h:form id="perm"> <div class="dialog"> <s:validateAll> <div class="prop"> <span class="name">Name</span> <span class="value"> <s:decorate> <h:inputText id="name" required="true" value="#{permission.name}"/> </s:decorate> </span> </div> </s:validateAll> </div> <div class="actionButtons"> <h:commandButton id="save" value="Save" action="#{permissionManager.persist}"/> <s:button id="done" value="Done" propagation="end" view="/permissionList.xhtml"/> </div> </h:form> </ui:define> </ui:composition>
pages.xml <!DOCTYPE pages PUBLIC "-//JBoss/Seam Pages Configuration DTD 1.2//EN" "http://jboss.com/products/seam/pages-1.2.dtd"> <pages no-conversation-view-id="/home.xhtml" login-view-id="/login.xhtml"> <page view-id="*"> <navigation> <rule if-outcome="home"> <redirect view-id="/home.xhtml"/> </rule> </navigation> </page> <page view-id="/permissionList.xhtml"> <navigation> <rule if-outcome="list"> <redirect view-id="/permissionList.xhtml"/> </rule> <rule if-outcome="form"> <redirect view-id="/permissionForm.xhtml"/> </rule> </navigation> </page> <page view-id="/permissionForm.xhtml"> <navigation> <rule if-outcome="list"> <redirect view-id="/permissionList.xhtml"/> </rule> </navigation> </page> <exception class="org.jboss.seam.framework.EntityNotFoundException"> <redirect view-id="/error.xhtml"> <message>Not found</message> </redirect> </exception> <exception class="javax.persistence.EntityNotFoundException"> <redirect view-id="/error.xhtml"> <message>Not found</message> </redirect> </exception> <exception class="javax.persistence.OptimisticLockException"> <end-conversation/> <redirect view-id="/error.xhtml"> <message>Another user changed the same data, please try again</message> </redirect> </exception> <exception class="org.jboss.seam.security.AuthorizationException"> <redirect> <message>You don't have permission to do this</message> </redirect> </exception> <exception class="org.jboss.seam.security.NotLoggedInException"> <redirect view-id="/login.xhtml"> <message>Please log in first</message> </redirect> </exception> <exception> <redirect view-id="/error.xhtml"> <message>Unexpected error, please try again</message> </redirect> </exception> </pages>