- 浏览: 100841 次
-
文章分类
最新评论
-
jXee:
lgs0626 写道求源码,兄弟给共享下吧 "jee ...
jee6 学习笔记 4 - CRUD 2: View Details, Primefaces row selection -
lgs0626:
求源码,兄弟给共享下吧
jee6 学习笔记 4 - CRUD 2: View Details, Primefaces row selection
jee6 学习笔记 4 - CRUD 3 (finally): Update and Add New
Add new/Update/View Details can be combined altogether into one single screen. This results in the CRUD operations to only two screens. The search screen has the "delete" action as the last column of the datatable.
Here's the updated search screen(it shows primefaces database pagination attributes: paginatorPosition="bottom" rows="5" rowsPerPageTemplate="5,10,20,50"):
Here's the Add/Update/View Details screen(it shows the Primefaces <p:calendar/> widget):
Updated source "/student/studentSearch.xhtml":
Updated backing bean "StudentSearch.java"
Source "/student/studentDetails.xhmtl":
Source backing bean "StudentBean.java":
Updated dao ejb "StudnetDAOImp.java":
Now that it has a search page, a view details/add new/update page and the delete functionality as well. This completes the CRUD experiment based on the "Student" entity in the database.
What to be noted on this experiment is that:
1. Although JSF 2.0 provides a couple of ways of binding http GET parameters, it still looks odd and unnatural if you like. In this example, i used <f:setPropertyActionListener/> to bind a reload-after-add/update/delete parameter "refresh". I'll add a new blog shortly for how JSF2.0 bind http GET parameters.
2. They said that the backing bean of the primefaces "<p:dataTable/>" should be @ViewScoped to make sorting work. My experiment is that it needs to be @SessionScoped.
Here's the updated search screen(it shows primefaces database pagination attributes: paginatorPosition="bottom" rows="5" rowsPerPageTemplate="5,10,20,50"):

Here's the Add/Update/View Details screen(it shows the Primefaces <p:calendar/> widget):

Updated source "/student/studentSearch.xhtml":
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:f="http://java.sun.com/jsf/core" xmlns:h="http://java.sun.com/jsf/html" xmlns:p="http://primefaces.org/ui"> <h:head> <title>student search</title> </h:head> <h:body> <h:form id="searchForm"> <p:panel header="Student Search Form"> <h:panelGrid columns="3"> <h:outputLabel value="Name: "/> <h:inputText value="#{ss.nameFilter}"></h:inputText> <h:commandButton type="submit" value="Search" action="#{ss.findByName}"/> </h:panelGrid> </p:panel> </h:form> <p:panel header="Students Found: #{ss.size}"> <h:form id="dataListForm"> <!-- search result list --> <p:dataTable id="idStuDataTable" var="st" value="#{ss.searchResultList}" paginator="true" paginatorPosition="bottom" rows="5" rowsPerPageTemplate="5,10,20,50"> <p:column headerText="name" sortBy="#{st.name}"> <h:outputText value="#{st.name}" /> </p:column> <p:column headerText="mobile" sortBy="#{st.mobile}"> <h:outputText value="#{st.mobile}" /> </p:column> <p:column headerText="created date" sortBy="#{st.created_date}"> <h:outputText value="#{st.created_date}"> <f:convertDateTime pattern="yyyy-MM-dd HH:mm:ss" timeZone="#{ss.timeZone}"/> </h:outputText> </p:column> <p:column headerText="action" style="width:12%"> <h:commandLink id="idViewDetailsButton" title="View Details" value="details" action="studentDetails.xhtml"> <f:setPropertyActionListener value="#{st}" target="#{stbean.student}" /> </h:commandLink> <p:spacer width="4"/> <h:commandLink id="idDeleteButton" title="Delete Student" value="delete" action="#{stbean.delete}" onclick="return confirm('Are you sure to delete: #{st.name}?')"> <f:setPropertyActionListener value="#{st}" target="#{stbean.student}" /> <f:setPropertyActionListener value="true" target="#{ss.refresh}" /> </h:commandLink> </p:column> </p:dataTable> </h:form> </p:panel> <h:form id="addNewFrm"> <h:commandButton id="newButt" value="Add New Student" action="studentDetails.xhtml"/> </h:form> </h:body> </html>
Updated backing bean "StudentSearch.java"
package com.jxee.action.student; import java.io.Serializable; import java.util.List; import java.util.TimeZone; import javax.ejb.EJB; import javax.faces.bean.ManagedBean; import javax.faces.bean.SessionScoped; import org.apache.log4j.Logger; import com.jxee.ejb.student.StudentDAO; import com.jxee.model.student.Student; @ManagedBean(name="ss") @SessionScoped //@ViewScoped not working with Primefaces <p:dataTable/> sorting. public class StudentSearch implements Serializable { private static final Logger log = Logger.getLogger(StudentSearch.class); private static final String SEARCH_PAGE = "/student/studentSearch.xhtml"; private List<Student> searchResultList; private @EJB StudentDAO dao; private String nameFilter; private int maxRows = 50; /** * flag if cached result list needs reloading, after add/update/delete etc. * set from views: <f:setPropertyActionListener value="true" target="#{ss.refresh}" /> */ private boolean refresh; public boolean isRefresh() { return refresh; } public void setRefresh(boolean refresh) { log.debug("-- setting refresh: " + refresh); this.refresh = refresh; } public String findByName() { log.debug("search student by nameFilter: " + nameFilter); searchResultList = dao.find(this.nameFilter, maxRows); return SEARCH_PAGE; } public String getNameFilter() { return nameFilter; } public void setNameFilter(String afilter) { this.nameFilter = afilter; } public int getMaxRows() { return maxRows; } public void setMaxRows(int maxRows) { this.maxRows = maxRows; } public List<Student> getSearchResultList() { log.debug("--- getting search result list.... isRefresh? " + this.isRefresh()); if(this.isRefresh()) { this.findByName(); this.refresh = false; } return this.searchResultList; } public void setSearchResultList(List<Student> searchResultList) { this.searchResultList = searchResultList; } public int getSize() { if(this.isRefresh()) { // in order to get the correct list size this.findByName(); this.refresh = false; } int size = this.searchResultList != null ? this.searchResultList.size() : 0; log.debug("search result size: " + size); return size; } // use default TimeZone to display time properly public TimeZone getTimeZone() { TimeZone tz = TimeZone.getDefault(); return tz; } }
Source "/student/studentDetails.xhmtl":
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:f="http://java.sun.com/jsf/core" xmlns:h="http://java.sun.com/jsf/html" xmlns:p="http://primefaces.org/ui"> <h:head> <title>save student</title> </h:head> <h:body> <h:form id="formStudent"> <p:panel header="Student Details" style="width:50%"> <h:panelGrid columns="3"> Name: <h:inputText id="sname" value="#{stbean.student.name}"/> <p:message for="sname"/> Age: <h:inputText id="sage" value="#{stbean.student.age}"/> <p:message for="sage"/> Mobile: <h:inputText id="smobile" value="#{stbean.student.mobile}"/> <p:message for="smobile"/> Created Date: <p:calendar id="screateddate" pattern="yyyy-MM-dd HH:mm:ss" value="#{stbean.student.created_date}"/> <p:message for="screateddate"/> <h:inputHidden value="#{stbean.student.id}"/> </h:panelGrid> <p:separator/> <h:commandButton id="saveBut" value="Save" action="#{stbean.save}"> <f:setPropertyActionListener value="true" target="#{ss.refresh}" /> </h:commandButton> <p:spacer width="10"/> <h:commandButton id="cancelBut" value="Cancel" immediate="true" action="studentSearch.xhtml"/> </p:panel> </h:form> </h:body> </html>
Source backing bean "StudentBean.java":
package com.jxee.action.student; import java.io.Serializable; import javax.ejb.EJB; import javax.faces.bean.ManagedBean; import org.apache.log4j.Logger; import com.jxee.ejb.student.StudentDAO; import com.jxee.model.student.Student; @ManagedBean(name="stbean") public class StudentBean implements Serializable { private static final Logger log = Logger.getLogger(StudentBean.class); private static final String SEARCH_PAGE = "/student/studentSearch.xhtml"; private @EJB StudentDAO dao; // property set from Primefaces single row selection private Student student = new Student(); public Student getStudent() { return student; } public void setStudent(Student st) { this.student = st; } public String save() { log.debug("saving student: " + this.student); if(student != null ) { if(student.getId() != null && student.getId() > 0) { log.debug("updating student: " + student); dao.update(student); } else { log.debug("adding new student: " + student); dao.add(student); } } return SEARCH_PAGE; } public String delete() { if(student != null) { log.debug("deleting student: " + student); dao.delete(this.student); } return SEARCH_PAGE; } public String cancel() { return SEARCH_PAGE; } }
Updated dao ejb "StudnetDAOImp.java":
package com.jxee.ejb.student; import java.util.List; import javax.ejb.Stateless; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; import javax.persistence.Query; import org.apache.log4j.Logger; import com.jxee.ejb.UserDAOImp; import com.jxee.model.student.Student; @Stateless public class StudentDAOImp implements StudentDAO { private static final Logger log = Logger.getLogger(UserDAOImp.class); @PersistenceContext(unitName="punit.projee6") private EntityManager em; @SuppressWarnings("unchecked") public List<Student> find(String nameFilter, int max) { String pattern = nameFilter != null ? "%" + nameFilter + "%" : "%"; String sql = "select s from Student s where s.name like :pattern"; Query q = em.createQuery(sql); q.setParameter("pattern", pattern); q.setFirstResult(0); q.setMaxResults(max); return q.getResultList(); } public void add(Student stu) { log.debug("adding new student: " + stu); if(stu.getId() != null) { // set null to avoid exception: // "org.hibernate.PersistentObjectException: detached entity passed to persist" stu.setId(null); } em.persist(stu); } public void update(Student stu) { log.debug("updating student: " + stu); em.merge(stu); } public void delete(Student stu) { Student todel = em.find(Student.class, stu.getId()); log.debug("student loaded to delete: " + todel); em.remove(todel); } }
Now that it has a search page, a view details/add new/update page and the delete functionality as well. This completes the CRUD experiment based on the "Student" entity in the database.
What to be noted on this experiment is that:
1. Although JSF 2.0 provides a couple of ways of binding http GET parameters, it still looks odd and unnatural if you like. In this example, i used <f:setPropertyActionListener/> to bind a reload-after-add/update/delete parameter "refresh". I'll add a new blog shortly for how JSF2.0 bind http GET parameters.
2. They said that the backing bean of the primefaces "<p:dataTable/>" should be @ViewScoped to make sorting work. My experiment is that it needs to be @SessionScoped.
发表评论
-
ActiveMQ and Spring JMS Framework Message Loss
2019-06-28 07:15 29Java Message Service (JMS) prov ... -
how to proxy to k8s web console
2018-06-28 07:16 581### how to access k8s web conso ... -
Call Stored Procedure with JPA 2.1
2018-06-27 10:57 686JPA 2.1 introduces APIs to call ... -
Send response and then process - async processing
2017-10-12 09:35 570If your request processing take ... -
java 8 time api test
2017-08-29 05:40 490public class ParseUtcDateTime ... -
Setup ApiKey in header with Swagger generated client code
2017-08-23 06:41 483@Value("${api.base.path} ... -
Simple tool to monitor jvm memory usage and garbage collection
2016-10-13 06:06 370JDK has built-in tool to moni ... -
Externalize Application Config properties with JBoss 7.1
2017-06-02 12:09 347If you have configuration pro ... -
JPA native query does not support setting list parameters
2014-03-27 06:45 1020you might want to do the ... -
Owning Side and Inverse Side of JPA entity relationships
2013-09-10 07:08 821Entity relationships may be b ... -
avoid setParameter for "order by" in JPQL
2013-03-07 05:55 785you might want to create a JP ... -
JPA Path Expression, operator IN and Collection properties
2013-01-23 16:25 1407If we want to select the Orde ... -
与JEE6/EJB3.1相比, Spring framework 丧失了几乎所有的优势
2013-01-19 13:13 1049The Spring framework was a ma ... -
Simple EasyMock tutorial
2012-12-20 11:57 693http://veerasundar.com/blog/20 ... -
Servlet 3.0 @WebFilter and @WebServlet
2012-12-04 07:09 2717Servlet 3.0 provides new annota ... -
Why JSF2 @ViewScoped not working?
2012-12-03 06:55 1387javax.faces.bean.ViewScoped sai ... -
When to configure an XA datasource?
2012-11-16 12:58 1275If you ever came across this wa ... -
java ee transaction and datasource concepts
2012-11-10 13:48 10561. What is a transaction? A tra ... -
pass params to primefaces confirmation dialog box
2012-09-28 19:30 1346<p:dataTable id="idStuD ... -
Handle Big Dataset with Real Pagination with Primefaces 3.3 LazyDataModel
2012-09-21 13:41 5643If you have millions of record ...
相关推荐
"eclipse-jee-2023-09-R-win32-x86_64.zip" 是Eclipse针对2023年9月发布的一个更新版,专为Windows 64位操作系统设计。 在Windows系统上安装和使用Eclipse-JEE,你需要了解以下关键知识点: 1. **安装步骤**: - ...
eclipse-jee-mars-1-win32-x86_64.7z eclipse-jee-mars-1-win32-x86_64.zip 我打的 7z 压缩包 关于有 Alt + / 不起作用解决办法: window -> General -> Keys -> Content Assist -> Binding: 改为 Alt + / When:...
开发工具 eclipse-jee-mars-2-win32开发工具 eclipse-jee-mars-2-win32开发工具 eclipse-jee-mars-2-win32开发工具 eclipse-jee-mars-2-win32开发工具 eclipse-jee-mars-2-win32开发工具 eclipse-jee-mars-2-win32...
"eclipse-jee-2018-09-win32-x86_64.zip" 是Eclipse针对Windows 32位和64位系统的2018年9月版本的下载包。此版本被称为Eclipse SimRel(Simultaneous Release),意味着它是在Eclipse年度统一发布计划中的一部分,...
在Java企业版(Java EE)6中,`@Asynchronous`注解是一个非常重要的特性,它使得开发者可以方便地在应用程序中实现异步处理。这个注解是Java EE并发编程的一部分,主要应用于EJB(Enterprise JavaBeans)环境,用于...
标题中的“eclipse-jee-oxygen-R-win32-x86_64”指的是Eclipse IDE for Java EE Developers的一个特定版本,Oxygen R,适用于64位的Windows操作系统。Eclipse IDE是广受欢迎的开源集成开发环境(IDE),主要用于Java...
"eclipse-jee-oxygen-3-win32.rar" 是一个针对Windows 32位系统的Eclipse版本,专为Java企业级开发(JEE)设计。这个版本是Eclipse Oxygen系列的第三个发布版,它包含了众多开发者所需的工具和特性,旨在提升开发...
文件名称: eclipse-jee-2019-09-R-win32-x86_64.zip 文件大小: 352 MB (369,940,487 字节) MD5: fc821116680da0bca7bd7a1d9e0b48b7 SHA1: 14be627915d2e11b618434d48ddf9e3ec3849392 SHA256: 3808c394d16153ceb429...
"eclipse-jee-2023-09-R-linux-gtk-x86_64.tar.gz" 文件是Eclipse专为Java企业版(Java EE)开发者设计的2023年9月版本,适用于64位的Linux操作系统。这个版本包含了对Java EE开发所需的全部工具和功能,如Web服务器...
eclipse-jee-2021-12-R-win32-x86_64 eclipse-jee-2021-12-R-win32-x86_64 eclipse-jee-2021-12-R-win32-x86_64
在解压eclipse-jee-2022-06-R-win32-x86_64.zip后,我们会得到一个名为“eclipse”的文件夹,这个文件夹包含了整个IDE的所有组件和配置。启动Eclipse IDE,用户会看到熟悉的界面,包括工作区(Workspace)、透视图...
Eclipse IDE for Enterprise Java and Web Developers (eclipse-jee-2021-12-R-win32-x86_64.zip)适用于Windwos x86_64
eclipse-jee-2023-12-R-win32-x86_64.zip 适用于Windows系统
标题中的“eclipse-jee-2020-09-R-win32-x86_64.zip”指的是Eclipse IDE for Java EE Developers的2020年9月版本,适用于Windows 32位和64位系统的安装包。Eclipse是一款著名的开源集成开发环境(IDE),广泛用于...
标题中的“eclipse-jee-indigo-SR2-win32”指的是Eclipse IDE的一个特定版本,它是专为Java EE(企业级Java)开发者设计的。Eclipse是一个开源的集成开发环境(IDE),广泛用于Java编程,它提供了丰富的工具集来支持...
《Eclipse JEE Neon 3 for Windows:一款高效强大的开发利器》 Eclipse JEE Neon 3 是Eclipse基金会发布的一款专为Java EE开发者设计的集成开发环境(IDE)。这款软件以其强大的功能、高效的性能以及丰富的插件生态...
Eclipse-jee-mars-R-win32-x86_64位官方绿色版.zip是一个针对Windows平台的64位版本的Eclipse集成开发环境(IDE)的压缩包,特别为Java企业级(J2EE)应用程序开发设计。该版本发布于2015年6月30日,是当时Eclipse ...
eclipse-jee-2022-12-R-win32-x86_64,rar压缩包格式,解压即用。 安装教程:https://danbaku.blog.csdn.net/article/details/128600309
根据提供的文件信息,我们可以推断出这份文档与Eclipse IDE for Java有关,特别是版本为2019-09-R的Eclipse JEE(Java EE)版本,适用于win32-x86_64操作系统。下面将围绕这些信息展开相关的知识点。 ### Eclipse ...